窗口未显示WM_PAINT中的行

时间:2011-09-11 09:58:52

标签: winapi wm-paint

我是这个地区的新手,所以...... 我做了下面的代码,据我所知,在WM_PAINT我正在创建一条线,但是我看不到这一行,因为窗口根本没有显示。 我有一个每隔2秒调用WM_PAINT的线程,但每次运行程序时都没有显示任何内容。

先谢谢。

#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;
}

2 个答案:

答案 0 :(得分:2)

您不应该自己发送WM_PAINT。您需要使用InvalidateRect。您还需要使用绘制 - 您需要使用SelectObject在DC中选择有效的笔对象。

答案 1 :(得分:0)

重绘窗口时,系统会发送

WM_PAINT消息,因此您无需自行发送消息。如果要重新绘制窗口,请使用InvalidateRect。如果在程序中使用Thread,则应使用ExitProcess(ThreadID)退出该线程,然后执行以下代码。笔的默认颜色为黑色,因此您可以看到它。在你的情况下,你的线程没有退出,所以程序停在那里。窗户永远不会画画。您可以在程序中使用MessageBox()来测试现在运行的位置。