Code:
#include <windows.h>
LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);
DWORD WINAPI ThreadProc(LPVOID);
volatile HWND hWindow;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASS WndClass;
WndClass.style = 0;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.hInstance = hInstance;
WndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
WndClass.lpszMenuName = 0;
WndClass.lpszClassName = "WinProg";
RegisterClass(&WndClass);
HWND hButton;
hWindow = CreateWindow("WinProg","Window",
WS_OVERLAPPEDWINDOW,0,0,400,400,NULL,NULL,
hInstance,NULL);
hButton = CreateWindow("BUTTON","Start Thread 2",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10,200,200,20,hWindow,(HMENU)1,hInstance,NULL);
hButton = CreateWindow("BUTTON","End Thread 2",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10,240,200,20,hWindow,(HMENU)2,hInstance,NULL);
ShowWindow(hWindow, nCmdShow);
UpdateWindow(hWindow);
MSG Message;
while(GetMessage(&Message, NULL,0,0))
{
DispatchMessage(&Message);
}
return (Message.wParam);
}
DWORD WINAPI ThreadProc(LPVOID pvoid)
{
int value = 2;
RECT border;
GetClientRect(hWindow,&border);
while(value == 2)
{
HDC hdc1;
hdc1 = GetDC(hWindow);
Rectangle(hdc1,border.left+50,border.top +50,
border.right - 50,border.bottom - 200);
ReleaseDC(hWindow,hdc1);
}
return 1;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT uiMessage,
WPARAM wParam, LPARAM lParam)
{
static HANDLE hThread;
switch(uiMessage)
{
case WM_COMMAND:
if(HIWORD(wParam) == BN_CLICKED)
{
if(LOWORD(wParam) == 1)
{
DWORD dwThreadParam = 1;
DWORD dwThreadID;
hThread = CreateThread(NULL,0,ThreadProc,
&dwThreadParam,0, &dwThreadID);
}
if(LOWORD(wParam) == 2)
{
TerminateThread(hThread,0);
WaitForSingleObject(hThread,INFINITE);
InvalidateRect(hWnd,0,TRUE);
UpdateWindow(hWnd);
}
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
TerminateThread(hThread,0);
return 0;
default:
return DefWindowProc(hWnd,uiMessage,
wParam, lParam);
}
}
Notice that border is now a rect of the client are of the window...I then size this a little smaller to create the rect to draw...