Files
GdCpp12/source/CDesktop.cpp

121 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "pch.h"
#include "CDesktop.h"
#include "GdCpp.h"
#include <windows.h>
#include <wingdi.h>
CDesktop::CDesktop()
{
INT iNumber = 0;
BOOL bFlag = TRUE;
DISPLAY_DEVICE dd;
ZeroMemory(&dd, sizeof(dd));
dd.cb = sizeof(dd);
DEVMODE devMode;
ZeroMemory(&devMode, sizeof(devMode));
devMode.dmSize = sizeof(devMode);
// FixMe这里只能获取到显卡连接的显示器获取不到USB-C显示器
// 而且双显示器分辨率不同时,有时第二个显示器的宽、高变大。
do
{
bFlag = EnumDisplayDevices(NULL, iNumber, &dd, 0);
bFlag = bFlag && EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &devMode);
if (bFlag) {
RECT r;
r.left= devMode.dmPosition.x;
r.top = devMode.dmPosition.y;
r.right = r.left + devMode.dmPelsWidth;
r.bottom = r.top + devMode.dmPelsHeight;
screen.push_back(r);
iNumber += 1;
}
} while (bFlag);
}
void CDesktop::TestDesktop()
{
auto *desktop = new CDesktop();
auto num = desktop->screen.size();
if (num == 0) {
alog->error("No Screen found!");
return;
}
for (int i = 0; i < num; i++) {
auto &s = desktop->screen[i];
alog->info("Screen {}: {}, {}, {}, {}", s.left, s.top, s.right - s.left, s.bottom - s.top);
}
delete desktop;
}
#include <iostream>
#include <Windows.h>
#include <ShellScalingApi.h>
// 链接Shcore.lib
#pragma comment(lib, "Shcore.lib")
//BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
//{
// MONITORINFOEX monitorInfo;
// monitorInfo.cbSize = sizeof(MONITORINFOEX);
// GetMonitorInfo(hMonitor, &monitorInfo);
//
// UINT dpiX, dpiY;
// if (GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY) == S_OK) {
// std::cout << "Monitor " << monitorInfo.szDevice << " DPI: " << dpiX << std::endl;
// }
//
// return TRUE;
//}
// 回调函数用于获取每个显示器的信息
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMonitor, &monitorInfo);
DISPLAY_DEVICE displayDevice;
displayDevice.cb = sizeof(DISPLAY_DEVICE);
EnumDisplayDevices(monitorInfo.szDevice, 0, &displayDevice, 0);
std::wcout << L"Display Device Name: " << displayDevice.DeviceName << std::endl;
std::wcout << L"Monitor Name: " << monitorInfo.szDevice << std::endl;
std::cout << "Monitor Resolution: " << monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left << "x"
<< monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top << std::endl;
std::cout << "Monitor Position: (" << monitorInfo.rcMonitor.left << ", " << monitorInfo.rcMonitor.top << ")"
<< std::endl;
// DPI信息获取
UINT dpiX, dpiY;
if (GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY) == S_OK) {
std::cout << "Monitor DPI: " << "x=" << dpiX << ", y=" << dpiY << std::endl;
}
std::cout << std::endl;
return TRUE;
}
void TestGetDpi() {
// 获取系统的DPI
int dpiX = GetDpiForSystem();
std::cout << "System DPI: " << dpiX << std::endl;
// 获取主显示器桌面的DPI
HWND hWndDesktop = GetDesktopWindow();
if (hWndDesktop != NULL) {
HMONITOR hMonitor = MonitorFromWindow(hWndDesktop, MONITOR_DEFAULTTONEAREST);
UINT dpiX, dpiY;
if (GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY) == S_OK) {
std::cout << "Desktop DPI: " << dpiX << std::endl;
}
}
std::cout << std::endl;
// 遍历所有显示器的DPI
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, 0);
}