簡體   English   中英

SetWindowPos()跨進程DPI感知

[英]SetWindowPos() cross-process DPI aware

我正在創建一個程序,使用SetWindowPos()從另一個進程移動/調整窗口大小。 我自己的程序是PROCESS_PER_MONITOR_DPI_AWARE 其他程序可以是PROCESS_DPI_UNAWAREPROCESS_SYSTEM_DPI_AWAREPROCESS_PER_MONITOR_DPI_AWARE

因為我自己的程序是PROCESS_PER_MONITOR_DPI_AWARE ,所以傳遞給SetWindowPos()的坐標是物理坐標。 我現在想要做的是將客戶區域的大小調整為 邏輯坐標中的特定大小。

我試圖做的是

  1. 獲取窗口所在的監視器的DPI作為screenDPI
  2. 獲取目標窗口的DPI作為windowDPI
  3. 獲取scaleFactor作為screenDPI / windowDPI
  4. 通過scaleFactor縮放所需的客戶區大小
  5. 通過從窗口rect大小中減去當前客戶端矩形大小來計算窗口框架的額外大小。

這在很大程度上起作用,但是當我使用具有不同顯示縮放的兩個屏幕時,那么

  • 如果我將窗口從一個屏幕移動到下一個屏幕,則窗口框架大小的計算將關閉。
  • 對於使用PROCESS_SYSTEM_DPI_AWARE的應用程序,當窗口位於輔助屏幕(與使用120dpi的主屏幕相比使用96dpi)時,這會失敗。 這與窗口框架大小無關,我還不確定為什么它會失敗,但是目標xy坐標會按比例放大,以便窗口移出屏幕。
  • 如果由於調整大小,窗口的中心會改變屏幕,會發生什么? 那么screenDPI將不再正確,對吧? 我該如何處理這個案子?

我知道還有函數AdjustWindowRectExForDpi ,但不知怎的,我無法讓它正常工作。 我應該傳遞給它的dpi值是多少? 目標屏幕的dpi,目標窗口的dpi還是我自己程序的dpi? 此外,此功能僅在Windows 10以后可用,因此如何在較舊的Windows客戶端上處理它?

我將不勝感激。 謝謝!

我應該傳遞給它的dpi值是多少? 目標屏幕的dpi,目標窗口的dpi還是我自己程序的dpi?

您需要從一個屏幕移動到下一個屏幕的窗口的DPI。

代碼示例:

#include <Windows.h>

LRESULT CALLBACK startup_window_procedure(HWND window, UINT message, WPARAM w_param, LPARAM l_param)
{
    switch (message)
    {
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }

    case WM_DPICHANGED:
    {
        // Resize the window
        RECT* new_rect = reinterpret_cast<RECT*>(l_param);

        if (!SetWindowPos(window, nullptr, new_rect->left, new_rect->top, new_rect->right - new_rect->left, new_rect->bottom - new_rect->top, SWP_NOZORDER | SWP_NOACTIVATE))
        {
            return 1;
        }

        return 0;
    }
    }

    return DefWindowProcW(window, message, w_param, l_param);
}

int CALLBACK wWinMain(HINSTANCE instance, HINSTANCE prev_instance, PWSTR cmd_line, int cmd_show)
{
    constexpr auto window_class_name = L"example_dialog";
    constexpr auto window_style = WS_OVERLAPPEDWINDOW;

    // Enable per-monitor DPI-awareness version 2
    if (!SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2))
    {
        return 1;
    }

    // Create the window
    WNDCLASSEXW window_class;
    window_class.cbSize = sizeof(window_class);
    window_class.style = CS_HREDRAW | CS_VREDRAW;
    window_class.lpfnWndProc = startup_window_procedure;
    window_class.cbClsExtra = 0;
    window_class.cbWndExtra = 0;
    window_class.hInstance = instance;
    window_class.hIcon = nullptr;
    window_class.hCursor = nullptr;
    window_class.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
    window_class.lpszMenuName = nullptr;
    window_class.lpszClassName = window_class_name;
    window_class.hIconSm = nullptr;

    if (!RegisterClassExW(&window_class))
    {
        return 1;
    }

    HWND window = CreateWindowExW(0, window_class_name, L"Example window", window_style, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, nullptr, nullptr, instance, nullptr);

    if (!window)
    {
        return 1;
    }

    UINT dpi = GetDpiForWindow(window);
    float scaling_factor = static_cast<float>(dpi) / 96;
    // Actually set the appropriate window size
    RECT scale;
    scale.left = 0;
    scale.top = 0;
    scale.right = static_cast<LONG>(300 * scaling_factor);
    scale.bottom = static_cast<LONG>(150 * scaling_factor);

    if (!AdjustWindowRectExForDpi(&scale, window_style, false, 0, dpi))
    {
        return 1;
    }

    if (!SetWindowPos(window, nullptr, 0, 0, scale.right - scale.left, scale.bottom - scale.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE))
    {
        return 1;
    }

    ShowWindow(window, SW_SHOWNORMAL);

    // Message loop
    MSG message;
    int result;

    while ((result = GetMessageW(&message, nullptr, 0, 0)) != 0)
    {
        if (result == -1)
        {
            return 1;
        }
        else
        {
            TranslateMessage(&message);
            DispatchMessageW(&message);
        }
    }

    return static_cast<int>(message.wParam);
}

窗口可以從一個屏幕移動到下一個屏幕,並成功重新計算窗口大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM