WIN32 API窗口应用程序视网膜高DPI屏幕上的屏幕上的问题



主题:简单的C Win32基于单窗口应用程序。请参阅下面的代码。计算机是带有Windows 10的MacBook Retina。

问题:在标题栏(窗口的非客户端区域(中最小化/最大化/关闭按钮在鼠标悬停事件上表现不正确。仅当鼠标光标移动时,每个按钮都会突出显示,而应一直突出显示按钮,直到鼠标指针离开按钮的区域为止。

问题:问题是什么?win10清单?

代码:

#include <Windows.h>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR msgGreeting[] = _T("Hello World from MyWindowsApp!");
    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        TextOut(hdc, 10, 10, msgGreeting, (int)_tcsclen(msgGreeting));
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }
    return 0;
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // 1. Initialize the application
    // 2. Display the main window
    WNDCLASSEX wndClassWindowMain;
    wndClassWindowMain.cbSize = sizeof(WNDCLASSEX);
    wndClassWindowMain.style = CS_VREDRAW | CS_HREDRAW;
    wndClassWindowMain.lpfnWndProc = WndProc;
    wndClassWindowMain.cbClsExtra = 0;
    wndClassWindowMain.cbWndExtra = 0;
    wndClassWindowMain.hInstance = hInstance;
    wndClassWindowMain.hIcon = LoadIcon(wndClassWindowMain.hInstance, IDI_APPLICATION);
    wndClassWindowMain.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClassWindowMain.hbrBackground = (HBRUSH)(COLOR_WINDOW);
    wndClassWindowMain.lpszMenuName = NULL;
    wndClassWindowMain.lpszClassName = TEXT("MyMainWindowClass");
    wndClassWindowMain.hIconSm = LoadIcon(wndClassWindowMain.hInstance, IDI_APPLICATION);
    if (0 == RegisterClassEx(&wndClassWindowMain))
    {
        MessageBox(NULL
            , _T("Call to RegisterClassEx failed!")
            , _T("MyWindowsApplication")
            , MB_ICONERROR | MB_OK);
        return FALSE;
    }
    HWND hwndWindowMain = CreateWindowEx(WS_EX_APPWINDOW
        , _T("MyMainWindowClass")
        , _T("My Window")
        , WS_OVERLAPPEDWINDOW
        , 100, 100
        , 640, 480
        , NULL
        , NULL
        , hInstance
        , NULL);
    if (NULL == hwndWindowMain)
    {
        MessageBox(NULL
            , _T("Call to CreateWindowEx failed!")
            , _T("MyWindowsApplication")
            , MB_ICONERROR | MB_OK);
        return FALSE;
    }
    ShowWindow(hwndWindowMain, SW_SHOWDEFAULT);
    UpdateWindow(hwndWindowMain);
    // 3. Go to the message retrieval-and-dispatch loop
    MSG msg;
    BOOL bRet;
    while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
    {
        if (bRet == -1)
        {
            // handle the error and possibly exit
            return FALSE;
        }
        else
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return (int)msg.wParam;
}

解决方案:

问题是从上面的示例中介绍了该应用程序的DPI(点(意识。为了使本机C /Win32 API代码与不同的屏幕兼容(包括4K和Retina(,您需要做的是遵循以下步骤:

  1. 包括适当的标题#include <ShellScalingApi.h>
  2. 告诉链接器编译的功能代码在哪里#pragma comment(lib,"Shcore.lib")
  3. 最后在WinMain中添加以下调用SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);

voila!

现在在视网膜上,我看到我的应用窗口适当地缩放。最小化/最大化/关闭按钮按预期工作。

进一步阅读在这里:https://msdn.microsoft.com/en-us/library/windows/desktop/dn302122(v = vs.85(.aspx

相关内容

  • 没有找到相关文章

最新更新