我对这个领域有点陌生,所以…我做了下面的代码,据我所知,在WM_PAINT我正在创建一条线,但是我看不到这条线,因为窗口根本不显示。我有一个线程,调用WM_PAINT每2秒,但每次没有显示一旦运行程序。
thanks in advance .
#include <windows.h>
#include <stdio.h>
DWORD WINAPI StartThread1(LPVOID LPElm);
//---------------------------------------------------------------------------
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;
}
你不应该自己发送WM_PAINT
。你需要使用InvalidateRect
。你还需要用一些来绘制 -你需要使用SelectObject
来选择一个有效的笔对象到DC。
WM_PAINT
消息是系统在重绘窗口时发送的,所以您不需要自己发送消息。如果你想重新绘制窗口,使用InvalidateRect
。如果在程序中使用Thread
,则应该使用ExitProcess(ThreadID)
退出线程,然后执行以下代码。钢笔的默认颜色是黑色,所以你可以看到它。在您的例子中,您的线程没有退出,因此程序停止在那里。这窗户永远也刷不上了。您可以在程序中使用MessageBox()
来测试当前运行的位置