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

WinAPI控制台子系统自定义控制按钮开发故障求助

自定义控制台窗口的最小化/最大化/关闭按钮问题

问题说明

我开发了一款控制台操控应用,已移除控制台标题栏,想要实现自定义的最小化、最大化、关闭按钮。但使用Phind、ChatGPT、Bing、Bard均未能解决问题:现有代码创建的按钮显示在控制台窗口外,且点击事件完全无效,目前考虑改用ImGui。

现有代码示例

代码示例1:移除控制台标题栏并调整窗口

#include <windows.h>

void MoveToCenter()
{
    RECT rectClient, rectWindow;
    HWND hWnd = GetConsoleWindow();
    GetClientRect(hWnd, &rectClient);
    GetWindowRect(hWnd, &rectWindow);
    int posx, posy;
    posx = GetSystemMetrics(SM_CXSCREEN) / 2 - (rectWindow.right - rectWindow.left) / 2;
    posy = GetSystemMetrics(SM_CYSCREEN) / 2 - (rectWindow.bottom - rectWindow.top) / 2;

    MoveWindow(hWnd, posx, posy, rectClient.right - rectClient.left, rectClient.bottom - rectClient.top, TRUE);
}

int main(int argc, char* argv[])
{
    HWND hwnd = GetConsoleWindow();

    SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU)); 
    SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED); 
    SetLayeredWindowAttributes(hwnd, 0, (255 * 70) / 100, LWA_ALPHA); 
    SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);

    MoveToCenter();

    return 0;
}

代码示例2:创建按钮的尝试

#include <windows.h>
#include <iostream>

int main()
{
    // 创建按钮
    HWND button = CreateWindow(
        "BUTTON",          // 预定义控件类
        "Press me",        // 按钮文本
        WS_VISIBLE | BS_DEFPUSHBUTTON,  // 控件样式
        100,               // 窗口x坐标
        100,               // 窗口y坐标
        100,               // 按钮宽度
        30,                // 按钮高度
        NULL,              // 无父窗口
        NULL,              // 无菜单
        GetModuleHandle(NULL), // 实例句柄
        NULL               // WM_CREATE消息无附加参数
    );


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

    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 03:22:02