首页 > 解决方案 > 添加编辑窗口后,win32 GUI 无法正确加载

问题描述

当我在 Visual Studio 中构建程序时,它需要很长时间才能构建并且无法正确显示,在此过程中,它会在控制台中加载和卸载许多看似 dll 文件的内容。如果我删除以程序开头的一行CreateWindowW(L"EDIT"将完美运行。我已经阅读了文档,但我找不到它有什么问题。任何帮助表示赞赏。

这是完整的代码

#ifndef UNICODE
#define UNICODE
#endif 
#include <windows.h>
#include<iostream>

#define file_menu_new 1
#define help_menu 2
#define file_menu_open 3
#define file_menu_exit 4

void AddMenus(HWND hwind);
HMENU hMenu;
void AddControls(HWND hwnd);

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,_In_ PSTR szCmdLine, _In_ int iCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[] = L"Sample Window Class";

    WNDCLASS wc = { };

    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    //regesters class above with operateing system 
    RegisterClass(&wc);

    // Create the window.
  
    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,            // Window style

        // Size and position
        500, 200, 800, 500,//WS_DEFAULT OR SOMETHING LIKE THAT

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );

    if (hwnd == NULL)
    {
        return 0;
    }


    ShowWindow(hwnd, iCmdShow);

    // Run the message loop.

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }


    
    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_COMMAND:
        {
            switch (wParam)
            {
            case file_menu_new:
                MessageBeep(MB_OK);//this creates a sound
                break;
            case file_menu_exit:
                DestroyWindow(hwnd);
                break;
            case file_menu_open:
                MessageBeep(MB_ICONINFORMATION);
            default:
                break;
            }
        }
        case WM_CREATE:
        {
            AddControls(hwnd);
            AddMenus(hwnd);
            break;
        }
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);



            FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));

            EndPaint(hwnd, &ps);
            break;
        }
    
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

void AddMenus(HWND hwind)
{
    hMenu = CreateMenu();//main menu bar
    HMENU hFileMenu = CreateMenu();//this is a drop down menu for the file part of main menu


    //AppendMenu(hMenu, MF_STRING, 1, L"File");//1 is the identity of this main menu
    AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"File");//this is how you make it pop up another menu
    AppendMenu(hFileMenu, MF_STRING, file_menu_new, L"New");//adds stuff to hFileMenu with the id of 1
    AppendMenu(hFileMenu, MF_STRING, file_menu_open, L"Open");//adds stuff to hFileMenu with the id of 3
    AppendMenu(hFileMenu, IMFT_SEPARATOR, NULL, NULL);//creates a seperator or a line under open
    AppendMenu(hFileMenu, MF_STRING, file_menu_exit, L"Exit");

    AppendMenu(hMenu, MF_STRING, help_menu, L"Help");//2 is the identity of this main menu


    //sets the hMenu to the hwind menu or the main menu
    SetMenu(hwind, hMenu);
}

void AddControls(HWND hwnd)
{
    CreateWindowW(L"static", L"Enter Text here: ", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER | WS_EX_TRANSPARENT,
        200, 100, 100, 50, hwnd, NULL, NULL, NULL);
    CreateWindowW(L"EDIT", L"Enter Text here: ", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
        200, 160, 100, 50, hwnd, NULL, NULL, NULL);
}

标签: c++windowswinapiwin32gui

解决方案


添加wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);和删​​除WM_PAINT. 如果您没有设置类背景,则需要处理WM_ERASEBKGND或以其他方式验证背景颜色绘制。


推荐阅读