window isn't showing line made in WM_PAINT
I'm kinda new to this area, so.. I made the following code, as far as i understand, in WM_PAINT i'm creating a line, however i can't see this line, because the window isn't displaying at all. i have a thread that calls WM_PAINT every 2 seconds, but every time nothing is displayed once running the program.
thanks in advanced.
#include <windows.h>
#include <stdio.h>
DWORD WINAPI StartThread1(LPVOID LPElm);
//---------------------------------开发者_如何学Go------------------------------------------
HWND hWnd;
LPCTSTR ClsName = L"WndMsg";
LPCTSTR WindowCaption = L"Windows and Controls Messages";
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
WNDCLASSEX WndClsEx;
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProc;
WndClsEx.cbClsExtra = NULL;
WndClsEx.cbWndExtra = NULL;
WndClsEx.hInstance = hInstance;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = ClsName;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&WndClsEx);
hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
ClsName,
WindowCaption,
WS_OVERLAPPEDWINDOW,
100,
120,
640,
480,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
DWORD ThreadId1, ThreadId2;
HANDLE HandleThread1 = CreateThread(0,0,StartThread1,0,0,&ThreadId1);
while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT Ps;
switch(Msg)
{
case WM_CREATE:
//MessageBox(NULL, L"The window is being created", WindowCaption, MB_OK);
break;
case WM_SHOWWINDOW:
break;
case WM_PAINT:
hDC = BeginPaint(hWnd, &Ps);
MoveToEx(hDC, 0, 0, NULL);
LineTo(hDC, 10, 10);
EndPaint(hWnd, &Ps);
break;
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
//---------------------------------------------------------------------------
DWORD WINAPI StartThread1(LPVOID LPElm)
{
int n = 5000,i,j;
for (i=0; i<n; i++)
{
SendMessage(hWnd,WM_PAINT,NULL,NULL);
Sleep(2000);
/*for (j=0;j<10;j++)
a[j] = (rand() % 100);
printf("\n");*/
}
return 0;
}
You should not send WM_PAINT
yourself. You need to use InvalidateRect
. You also need to be drawing with something - you need to use SelectObject
to select a valid pen object into the DC.
WM_PAINT
message is sent by the system when the window is redrawn, so you don't need to send the message yourself. If you want to redraw the window use InvalidateRect
. If you use Thread
in your program, you should exit the thread with ExitProcess(ThreadID)
, then the following code is been executed. The default color for the pen is black, so you can see it. In your case, your thread didn't exit, so the program stops there. The window is never going to paint. You can use MessageBox()
in your program to test where is running now.
精彩评论