【问题标题】:How to create a window in dark mode using WinApi/Win32 in C++如何在 C++ 中使用 WinApi/Win32 在暗模式下创建窗口
【发布时间】:2022-12-06 03:16:49
【问题描述】:

Result of my Code 创建按钮代码:

// Create Button
dwStyle = (WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON);
HWND hwndButton = CreateWindow(
    L"BUTTON",                                             // Predefined class; Unicode assumed
    L"OK",                                                 // Button text
    dwStyle,                                               // Styles
    10,                                                    // x position
    10,                                                    // y position
    70,                                                    // Button width
    30,                                                    // Button height
    hwndMain,                                              // Parent window
    NULL,                                                  // No menu.
    (HINSTANCE)GetWindowLongPtr(hwndMain, GWLP_HINSTANCE), // Instance handle
    NULL                                                   // Pointer not needed.
);

我的期望是按钮和文本框也将处于暗模式。如果没有办法自动执行此操作,如何手动执行此操作?例如,如何使用我想要的颜色从头开始创建按钮?

【问题讨论】:

    标签: c++ winapi window darkmode


    【解决方案1】:

    要在 C++ 中使用 Win32 API 创建暗模式窗口,您可以使用 SetWindowTheme 函数并指定“DarkMode_Explorer”主题。您将在创建窗口之后和显示它之前使用此函数。这是一个例子:

    // Create the window HWND hwnd = CreateWindow(...);

    // Set the window theme to "DarkMode_Explorer" SetWindowTheme(hwnd, L"DarkMode_Explorer", NULL);

    // Show the window ShowWindow(hwnd, SW_SHOW);

    您还可以使用 SetWindowCompositionAttribute 函数为窗口启用深色模式。此函数允许您设置 WCA_USEDARKMODECOLORS 属性,该属性告诉系统对窗口使用深色模式颜色。这是一个例子:

    // Create the window HWND hwnd = CreateWindow(...);

    // Enable dark mode for the window BOOL useDarkMode = TRUE; SetWindowCompositionAttribute(hwnd, WCA_USEDARKMODECOLORS, &useDarkMode, sizeof(useDarkMode));

    // Show the window ShowWindow(hwnd, SW_SHOW);

    请记住,这些功能仅适用于 Windows 10 版本 1809(2018 年 10 月更新)及更高版本。

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 2011-04-27
      • 2016-07-30
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 2016-06-12
      相关资源
      最近更新 更多