【问题标题】:How to resize window after adding menubar添加菜单栏后如何调整窗口大小
【发布时间】:2021-10-18 02:17:12
【问题描述】:

当我向窗口添加菜单栏时,窗口没有相应地调整大小,因为顶部被菜单栏隐藏。我想在考虑菜单栏高度的同时调整窗口大小。这是我尝试过的。

/* g++ test.cpp -o test -Wl,-subsystem,windows */

#include <Windows.h>

#define ID_OPEN 0
#define ID_EXIT 1
#define ID_ABOUT 2

const int width = 500;
const int height = 400;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szWndClassName[] = TEXT("hellowin");
    HWND hWnd;
    MSG msg;
    WNDCLASS wndclass;

    //Step 1: Registering the Window Class
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szWndClassName;

    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("This program only works in Windows NT or greater!"), TEXT("Hey dude!"), MB_OK | MB_ICONERROR);
        return 1;
    }

    // Step 2: Creating the Window
    hWnd = CreateWindow(szWndClassName, /* window class name */
                        TEXT("Hey!"), /* window title (or caption) */
                        WS_OVERLAPPEDWINDOW, /* window style */
                        CW_USEDEFAULT, /* initial x position */
                        CW_USEDEFAULT, /* initial y position */
                        width, /* initial window width */
                        height, /* initial window height */
                        NULL, /* parent window handle */
                        NULL, /* window menu handle */
                        hInstance, /* program instance handle */
                        NULL); /* creation parameters */

    if (hWnd == NULL)
    {
        MessageBox(NULL, TEXT("Window Creation Failed!"), TEXT("Error!"),
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hWnd, iCmdShow);
    UpdateWindow(hWnd);

    // Step 3: The Message Loop (heart)
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

// Step 4: the Window Procedure (brain)
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
    {
        HMENU hMenuBar = CreateMenu();
        HMENU hFile = CreatePopupMenu();
        HMENU hHelp = CreatePopupMenu();

        AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR) hFile, "File");
        AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR) hHelp, "Help");

        AppendMenu(hFile, MF_STRING, ID_OPEN, "Open");
        AppendMenu(hFile, MF_STRING, ID_EXIT, "Exit");

        AppendMenu(hHelp, MF_STRING, ID_ABOUT, "About");

        SetMenu(hWnd, hMenuBar);

        PMENUBARINFO pmbi;

        GetMenuBarInfo(hWnd, OBJID_MENU, 0, pmbi);

        RECT menuRect;
        RECT windRect;

        GetWindowRect(pmbi->hwndMenu, &menuRect);
        GetWindowRect(hWnd, &windRect);

        int width = menuRect.right - menuRect.left;
        int height = (menuRect.bottom - menuRect.top) + (windRect.bottom - windRect.top);

        SetWindowPos(hWnd,
                     0,
                     0,
                     0,
                     width,
                     height,
                     SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);

        break;
    }
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
        RECT rect;

        GetClientRect(hWnd, &rect);
        DrawText(hdc, TEXT("Hello World!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        EndPaint(hWnd, &ps);

        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }

    return 0;
}

我尝试检索菜单栏和窗口的矩形并像这样计算新尺寸

        int width = menuRect.right - menuRect.left;
        int height = (menuRect.bottom - menuRect.top) + (windRect.bottom - windRect.top);

不幸的是,代码崩溃了。我不知道我做错了哪一部分。有什么想法吗?

【问题讨论】:

标签: c++ winapi win32gui


【解决方案1】:

pmbi 是一个未初始化的指针变量。因此,将其传递给 GetMenuBarInfo 可能是导致您崩溃的原因。如果这没有崩溃,那么尝试访问 pmbi-&gt;hwndMenu 可能会崩溃。

相反,你想要:

MENUBARINFO mbi;
mbi.cbSize = sizeof (mbi);  // see documentation
GetMenuBarInfo(hWnd, OBJID_MENU, 0, &mbi);
...

告诉GetMenuBarInfo 填写(基于堆栈的)变量mbi

然后将pmbi-&gt;hwndMenu 替换为mbi.hwndMenu,这应该可以解决您的问题。

【讨论】:

  • 感谢您的回答。即使我修复了我的代码,窗口仍然没有调整大小......
  • 我认为这是因为您正试图在 WM_CREATE 处理程序中调整窗口大小。尝试在创建窗口返回的CreateWindowEx 调用之后执行此操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 2012-12-16
  • 2018-05-09
相关资源
最近更新 更多