簡體   English   中英

WinAPI窗口立即關閉

[英]WinAPI Window Closes Instantly

我一直在嘗試使用WINAPI嘗試學習它,但是我創建的窗口會立即關閉。 如您所見,當按下W鍵或按下左鍵時,它將關閉程序,但是在不按下任何鍵的情況下運行該程序仍會關閉。

#include <windows.h>
#include <windowsx.h>


// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
    UINT message,
    WPARAM wParam,
    LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    // the handle for the window, filled by a function
    HWND hWnd;
    // this struct holds information for the window class
    WNDCLASSEX wc;

    // clear out the window class for use
    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    // fill in the struct with the needed information
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = L"WindowClass1";

    // register the window class
    RegisterClassEx(&wc);

    // create the window and use the result as the handle
    hWnd = CreateWindowEx(NULL,
        L"WindowClass1",    // name of the window class
        L"Game",   // title of the window
        WS_OVERLAPPEDWINDOW,    // window style
        1,    // x-position of the window
        1,    // y-position of the window
        1800,    // width of the window
        1000,    // height of the window
        NULL,    // we have no parent window, NULL
        NULL,    // we aren't using menus, NULL
        hInstance,    // application handle
        NULL);    // used with multiple windows, NULL

    // display the window on the screen
    ShowWindow(hWnd, nCmdShow);

    // enter the main loop:

    // this struct holds Windows event messages
    MSG msg;

    // wait for the next message in the queue, store the result in 'msg'
    while (GetMessage(&msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&msg);

        // send the message to the WindowProc function
        DispatchMessage(&msg);
    }

    // return this part of the WM_QUIT message to Windows
    return msg.wParam;
}

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // sort through and find what code to run for the message given
    switch (message)
    {
        // this message is read when the window is closed

    case WM_MOUSEMOVE:
    {

        // Retrieve mouse screen position
        int x = (short)LOWORD(lParam);
        int y = (short)HIWORD(lParam);

        // Check to see if the left button is held down:
        bool leftButtonDown = wParam & MK_LBUTTON;

        // Check if right button down:
        bool rightButtonDown = wParam & MK_RBUTTON;

        if (leftButtonDown == true)
        {
            //left click
            //example lets close the program when press w
            PostQuitMessage(0);
            return 0;
        }


    }

    case WM_KEYDOWN:
    {                        
        switch (wParam)
        {
        case 'W':

            //w pressed
            //example lets close the program when press w
            PostQuitMessage(0);
            return 0;
        }
    }

    case WM_DESTROY:
    {
        // close the application entirely
        PostQuitMessage(0);
        return 0;
    }
    default:
        break;
    }

    // Handle any messages the switch statement didn't
    return DefWindowProc(hWnd, message, wParam, lParam);
}

您在交換機中缺少一些break語句,因此,例如,如果收到WM_MOUSEMOVE消息並且leftButtonDown != true ,則執行將落入WM_KEYDOWN等。

最終,您會看到case WM_DESTROY: ,它將為您Post一個可愛的QuitMessage

順便說一句,通過在調試器中逐條語句逐步執行,很容易發現這一點。

您的switch語句沒有break

你最終被放逐

PostQuitMessage(0);

您可以執行以下操作:

case WM_FOO:
{
  if ( bar ) {
      return 0;
  }
  break;
}

不要通過WM_MOUSEMOVE消息檢測到點擊,而是使用WM_MOUSEDOWN

問題是您的代碼可能是通過單擊某些東西來啟動的,因此,當窗口獲得其第一條WM_MOUSEMOVE消息時,實際上仍會按下該按鈕。 代碼的運行速度比手指快多了 ..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM