Win32 API窗口的标题区域出现空白

后端开发 2026-07-09

我目前使用C 开发一个库,用来为OpenGL创建一个Win32窗口,但据我的研究,这本不应该发生的问题却出现了:标题本应显示的区域却留有空白。这到底是怎么回事?

The window.c file is the following:

#include "window.h"

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    struct WindowClass* window = (struct WindowClass*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    switch (msg)
    {
        case WM_NCCREATE: {
            CREATESTRUCT* cs = (CREATESTRUCT*)lParam;
            SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)cs->lpCreateParams);
            return TRUE;
        }

        case WM_CLOSE:
            if (window) window->shouldClose = true;
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}

bool init() {
    WNDCLASSW wc = {
        .style = CS_OWNDC,
        .lpfnWndProc = WndProc,
        .hInstance = GetModuleHandle(NULL),
        .lpszClassName = L"WindowClass",
        .hCursor = LoadCursor(NULL, IDC_ARROW),
        .hbrBackground = NULL
    };

    if (!RegisterClassW(&wc)) return false;

    return true;
}

struct WindowClass* createWindow(const wchar_t* title, int width, int height) {
    struct WindowClass* out = malloc(sizeof(struct WindowClass));

    HWND hwnd = CreateWindowExW(0, L"WindowClass", title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, GetModuleHandleW(NULL), out);

    if (!hwnd) {
        free(out);
        return NULL;
    }

    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);

    HDC hdc = GetDC(hwnd);

    PIXELFORMATDESCRIPTOR pfd = {
        .nSize = sizeof(PIXELFORMATDESCRIPTOR),
        .nVersion = 1,
        .dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
        .iPixelType = PFD_TYPE_RGBA,
        .cColorBits = 32,
        .cDepthBits = 24,
        .cStencilBits = 8,
        .iLayerType = PFD_MAIN_PLANE
    };

    int pf = ChoosePixelFormat(hdc, &pfd);
    SetPixelFormat(hdc, pf, &pfd);

    HGLRC hglrc = wglCreateContext(hdc);
    wglMakeCurrent(hdc, hglrc);

    out->handle = hwnd;
    out->device = hdc;
    out->render = hglrc;
    out->shouldClose = false;

    return out;
}

void update() {
    MSG msg;
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

void closeWindow(struct WindowClass* window) {
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(window->render);
    ReleaseDC(window->handle, window->device);
    DestroyWindow(window->handle);
    free(window);
}


void end() {
    UnregisterClassW(L"WindowClass", GetModuleHandle(NULL));

}

And the window.h file contains the following:

#include <stdbool.h>
#include <Windows.h>
#include <windowsx.h>

struct WindowClass{
    HWND handle;
    HDC device;
    HGLRC render;
    bool shouldClose;
};

struct WindowClass* createWindow(const wchar_t* title, int width, int height);
void closeWindow(struct WindowClass* window);

bool init();
void end();

void update();

The main.c I used to test my library is as following:

#include "window.h"

int main() {
    if (!init()) return 1;
    struct WindowClass* window = createWindow(L"Window", 800, 600);
    while (true) {
        update();
        if (window->shouldClose) break;
    }
    closeWindow(window);
    end();
    return 0;
}

解决方案

在你的 WndProc 处理程序中,你还必须在 WM_NCCREATE 时调用默认处理程序 DefWindowProcW,否则Windows将无法设置窗口标题。

  SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)cs->lpCreateParams);
+ return DefWindowProcW(hwnd, msg, wParam, lParam);
- return TRUE;

成功显示的窗口标题

有关在何时使用 DefWindowProcW,请参阅 此问题

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章