Hi,
I have currently implemented a checkers game using win32 as gui. The game works perfectly.But there is a small problem i am facing. When the EXE is launched and moved around the desktop (i.e. apart from the position it was originally launched at numerous times) or when another application window is moved around it , it ends up corrupting the entire display (i.e. ever thing turns like Snow white's theme , every thing appears white-washed).On closing the application , things get back to normal. Here is a section of WM_PAINT , i reckon some where here is the culprit.
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
int i,j;
////DrawBoard
RECT rect;
int x=0,int y=0;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
rect.top =x;
rect.left=y;
rect.bottom=x+50;
rect.right=y+50;
if((i+j)%2==0)
FillRect(hdc, &rect, CreateSolidBrush(RGB(255,255,255)));
if((i+j)%2==1)
FillRect(hdc, &rect, CreateSolidBrush(RGB(0,0,0)));
y=y+50;
}
x=x+50;
y=0;
}
////Draw pieces
x=0;y=0;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
rect.top =x;
rect.left=y;
rect.bottom=x+50;
rect.right=y+50;
switch(board[j])
{
case WHITE:
SelectObject(hdc,CreateSolidBrush(RGB(0,0,255)));
Ellipse(hdc,rect.top,rect.left,rect.bottom,rect.right);
break;
case BLACK:
SelectObject(hdc,CreateSolidBrush(RGB(255,0,0)));
Ellipse(hdc,rect.top,rect.left,rect.bottom,rect.right);
break;
case BKING:
SelectObject(hdc,CreateSolidBrush(RGB(255,0,0)));
Ellipse(hdc,rect.top,rect.left,rect.bottom,rect.right);
SelectObject(hdc,CreateSolidBrush(RGB(0,0,0)));
Ellipse(hdc,rect.top+10,rect.left+10,rect.bottom-10,rect.right-10);
break;
case WKING:
SelectObject(hdc,CreateSolidBrush(RGB(0,0,255)));
Ellipse(hdc,rect.top,rect.left,rect.bottom,rect.right);
SelectObject(hdc,CreateSolidBrush(RGB(0,0,0)));
Ellipse(hdc,rect.top+10,rect.left+10,rect.bottom-10,rect.right-10);
default :
break;
}
y=y+50;
}
x=x+50;
y=0;
}
EndPaint(hwnd,&ps);
return(0);
} break;
and here is my window , creation / message loop.
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&winclass))
return(0);
if (!(hwnd = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"Checkers!", // title
WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
0,0, // initial x,y
406,448, // initial width, height
NULL, // handle to parent
LoadMenu(hinstance,MAKEINTRESOURCE(IDR_MENU1)), // handle to menu
hinstance,// instance of this application
NULL)))
return(0);
ShowWindow(hwnd,ncmdshow);
UpdateWindow(hwnd);
while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Sleep(10);
} // end while
return(msg.wParam);
} // end WinMain
I have made the window as WS_POPUP as i didnot want the user to resize and create a bigger mess. I have put in sleep as the application was trying to burn the cpu using 100% of it. In the game , on mouse capture , i am invalidating the entire client area using getClientRect / InvalidateRect.
Any help is apprecited .
Thanks!
"I think there is a world market for maybe five computers." -- Thomas Watson, Chairman of IBM, 1943