控制台子系统中的WinAPI创建按钮

问题描述 投票:0回答:1

我有操纵控制台的应用程序,我想制作自定义控制台按钮(最小化最大化关闭)我删除了标题栏,但是 phind,chatgpt,bing,bard,不能做我想做的事 代码:

#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;
}
#include <windows.h>
#include <iostream>

int main()
{
    // Create a button
    HWND button = CreateWindow(
        "BUTTON",          // predefined class
        "Press me",        // button text
        WS_VISIBLE | BS_DEFPUSHBUTTON,  // styles
        100,               // x position
        100,               // y position
        100,               // button width
        30,                // button height
        NULL,              // no parent window
        NULL,              // no menu
        GetModuleHandle(NULL), // instance handle
        NULL               // nothing to pass to WM_CREATE
    );


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

    return 0;
}

那个在控制台外部绘制按钮,但我所有点击它的尝试都是无用的,它只是不起作用,我已经在考虑imgui了

c++ windows winapi
1个回答
0
投票

有趣的想法吗? 为此,您的按钮必须类似于控制台的父按钮,并且必须具有 WM_CHILD 样式。哪个给:

#include <windows.h>

int main()
{
    HWND hWnd = GetConsoleWindow();

    HWND button = CreateWindow(
        "BUTTON",          // predefined class
        "Press me",        // button text
        WS_VISIBLE | BS_DEFPUSHBUTTON | WS_CHILD,  // styles
        100,               // x position
        100,               // y position
        100,               // button width
        30,                // button height
        hWnd,              // parent window
        NULL,              // no menu
        GetModuleHandle(NULL), // instance handle
        NULL               // nothing to pass to WM_CREATE
    );

    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.