You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Win32中EnableWindow禁用按钮时的异常现象

Win32禁用按钮绘制异常问题

开发了一个简单的Win32应用程序,创建按钮后立即调用<windows.h>中的EnableWindow(FALSE)函数将其禁用。当按钮尺寸大于文本时运行正常,但按钮尺寸小于文本时,整个窗口的客户区会出现拷贝屏幕内容的异常情况。

效果展示

  • 正常效果:
    按钮尺寸大于文本(正常)

  • 异常效果:
    按钮尺寸小于文本(异常)

复现代码

#include <Windows.h>

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

int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR    lpCmdLine, _In_ int       nCmdShow)
{
    WNDCLASSEX wcex = { 0 };

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.lpfnWndProc = WndProc;
    wcex.hInstance = hInstance;
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.lpszClassName = L"MYWINDOWCLASS";
    
    if (RegisterClassExW(&wcex) == 0)
        return -1;

    HWND hWnd = CreateWindowW(L"MYWINDOWCLASS", L"MYWINDOW", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

    if (!hWnd)
        return -1;

    HWND buttonHWnd = CreateWindowW(L"BUTTON", L"MYWINDOW", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
        100, 100, 40, 30, hWnd, nullptr, hInstance, nullptr);

    if (!buttonHWnd)
        return -1;

    EnableWindow(buttonHWnd, FALSE);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

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

    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

内容的提问来源于stack exchange,提问作者Theofanis kouniakis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 15:13:08