当hwnd值通过时,为什么GetMessage函数返回-1?

时间:2017-09-03 06:08:23

标签: visual-studio visual-c++ windows-applications

在这段代码中,我使用LineTo函数绘制线条。当我运行下面的代码GetMessage函数返回-1值。我不知道为什么会发生这种情况。我搜索在某些地方,如果hwnd已经被销毁,那么GetMessage函数将返回-1。当我将GetMessage的第二个参数传递为NULL时,我的代码运行正常。我正在使用Visual Studio 2010。

#include<Windows.h>
#include<stdio.h>
long int count_paint =0;
LRESULT CALLBACK my_WindowsProc_1(HWND hwnd, UINT msgkdjaks, WPARAM w, LPARAM l)
{
    HDC hdc;
    PAINTSTRUCT ps;

    switch(msgkdjaks)
    {
    case WM_PAINT:
        hdc = BeginPaint(hwnd,&ps);
        MoveToEx(hdc,30,30,NULL);
        LineTo(hdc,210,125);
        EndPaint(hwnd,&ps);
        break;
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hwnd,msgkdjaks,w,l);
}
int WINAPI WinMain(HINSTANCE hCur, HINSTANCE hPre, LPSTR lpr, int cmd_arg)
{
    MSG msg;
    HWND hwnd1,hwnd2;
    LPCTSTR class_name=L"My_Window_class";
    int x=0;
    WNDCLASSEX wc1;
    wc1.cbSize=sizeof(WNDCLASSEX);
    wc1.style=0;
    wc1.lpfnWndProc=my_WindowsProc_1;
    wc1.cbClsExtra=0;
    wc1.cbWndExtra=0;
    wc1.hInstance=hCur;
    wc1.hIcon=LoadIcon(NULL, IDI_APPLICATION);
    wc1.hCursor=LoadCursor(NULL,IDC_ARROW);
    wc1.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
    wc1.lpszMenuName=NULL;
    wc1.lpszClassName=class_name;
    wc1.hIconSm=LoadIcon(NULL,IDI_APPLICATION);

    if(!RegisterClassEx(&wc1))
    {
        return 10;
    }

    hwnd1 = CreateWindow(class_name, L"This is my window1",WS_OVERLAPPEDWINDOW, 
        200,200,300,300,NULL,NULL,hCur,NULL);
    if(hwnd1==NULL)
    {
        return 20;
    }
    ShowWindow(hwnd1 , cmd_arg);
    UpdateWindow(hwnd1);

    BOOL bRet;
    while((bRet = GetMessage( &msg, hwnd1, 0, 0 )) != 0)
    {
        if (bRet == -1){

            return -20;
        }
        else{
            TranslateMessage(&msg);//
            DispatchMessage(&msg);
        }
    }
    return 0;
}

0 个答案:

没有答案