Click to See Complete Forum and Search --> : [RESOLVED] Difficulty Plotting Pixels


jroeser
February 14th, 2010, 12:46 PM
I'm having difficulty plotting pixels on a black window. I'm using Visual C++ 6.0, and seem can't seem to figure out why my program won't plot these pixels. Basically, I use a for loop to plot roughly a 1000 pixels each with a different color. Here is my code:


#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//defines
#define WINDOW_CLASS_NAME "WINCLASSONE"

//some globals here

//WindosProc
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;

//handle our messages now
switch(msg)
{
case WM_CREATE:
{
return(0);
} break;

case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return(0);
} break;

case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;

default: break;

}

//as for any of the other messages we didn't get....
return (DefWindowProc(hwnd, msg, wparam, lparam));
}

//WinMain here
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
WNDCLASSEX winclass1;
HWND hwnd; //generic window handle
MSG msg; //generic message

winclass1.cbSize = sizeof(WNDCLASSEX);
winclass1.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;

winclass1.lpfnWndProc = WindowProc;
winclass1.cbClsExtra = 0;
winclass1.cbWndExtra = 0;
winclass1.hInstance = hinstance;
winclass1.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass1.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass1.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass1.lpszMenuName = NULL;
winclass1.lpszClassName = WINDOW_CLASS_NAME;
winclass1.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&winclass1))
return(0);

if (!(hwnd = CreateWindowEx(NULL, WINDOW_CLASS_NAME, "A Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0,
400,400,
NULL,
NULL,
hinstance,
NULL)))
return(0);


while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

//pixel practice
HDC hdc;
hdc=GetDC(hwnd);
for (int index=0; index < 1000; index++)
{
int x = rand()%400;
int y = rand()%400;

COLORREF color = RGB(rand()%255, rand()%255, rand()%255);
SetPixel(hdc, x, y, color);
}
ReleaseDC(hwnd, hdc);


return(msg.wParam);
}

I suspect that it is something to do with the the whole GetDC/ReleaseDC/BeginPaint/EndPaint deal, but I'm not sure where the error lies. My program compiles without any errors as well! I know that I'm not even close to understanding the Windows API as well as I should be, but I hope to get better with it through practice. I greatly appreciate any help in advance!

hoxsiew
February 14th, 2010, 01:27 PM
Whoa there. You should be doing your painting in the message loop (WM_PAINT), not AFTER the message loop exits!.

jroeser
February 14th, 2010, 10:28 PM
Doh! Don't I feel stupid! Thank you, I cut and pasted my code into WM_PAINT and it worked right away. I'm still getting a feel for how this Windows stuff works. Thanks for the help!

hoxsiew
February 15th, 2010, 08:13 AM
Also, note that the WM_PAINT section already has a DC for you to paint on. You can eliminate some of your code and just use the paint DC. Always do all your painting between the BeginPaint() and EndPaint().