Как получить разрешение экрана монитора от hWnd? - PullRequest
15 голосов
/ 28 января 2010

Как получить разрешение экрана монитора от hWnd?

Я использую hWnd, потому что окно может быть расположено на любом из нескольких мониторов.

т.е. верхняя / левая координаты hWnd находятся на мониторе с разрешением экрана 800 x 600.

Я программирую на языке PL / B, и он позволяет вызывать Windows API.

Какие оконные API можно использовать?

Ответы [ 5 ]

21 голосов
/ 01 июня 2012

Вот пример кода на C ++, который работает для меня:

HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &info);
int monitor_width = info.rcMonitor.right - info.rcMonitor.left;
int monitor_height = info.rcMonitor.bottom - info.rcMonitor.top;
20 голосов
/ 28 января 2010

Функция user32 MonitorFromWindow позволяет передавать hwnd и возвращает дескриптор монитора, на котором он включен (или по умолчанию - подробности см. В связанной статье MSDN). После этого вы можете вызвать GetMonitorInfo , чтобы получить MONITORINFO struct , которая содержит RECT, детализирующий его разрешение.

Дополнительные сведения см. В разделе Справочник по нескольким экранам в MSDN.

Я бы добавил пример кода, но я не знаю язык, на который вы ссылались, и я не знаю, насколько полезен пример кода C # для вас. Если вы думаете, что это поможет, дайте мне знать, и я быстро напишу что-нибудь.

5 голосов
/ 31 декабря 2011

Также есть GetSystemMetrics, проверьте его на MSDN

2 голосов
/ 13 октября 2016

Вот код C #, который получает разрешение (в DPI) через P / Invoke:

public static void GetWindowDpi(IntPtr hwnd, out int dpiX, out int dpiY)
{
    var handle = MonitorFromWindow(hwnd, MonitorFlag.MONITOR_DEFAULTTOPRIMARY);

    GetDpiForMonitor(handle, MonitorDpiType.MDT_EFFECTIVE_DPI, out dpiX, out dpiY);
}

/// <summary>
/// Determines the function's return value if the window does not intersect any display monitor.
/// </summary>
[SuppressMessage("ReSharper", "IdentifierTypo")]
[SuppressMessage("ReSharper", "UnusedMember.Local")]
private enum MonitorFlag : uint
{
    /// <summary>Returns NULL.</summary>
    MONITOR_DEFAULTTONULL = 0,
    /// <summary>Returns a handle to the primary display monitor.</summary>
    MONITOR_DEFAULTTOPRIMARY = 1,
    /// <summary>Returns a handle to the display monitor that is nearest to the window.</summary>
    MONITOR_DEFAULTTONEAREST = 2
}

[DllImport("user32.dll")]
private static extern IntPtr MonitorFromWindow(IntPtr hwnd, MonitorFlag flag);

[SuppressMessage("ReSharper", "IdentifierTypo")]
[SuppressMessage("ReSharper", "UnusedMember.Local")]
private enum MonitorDpiType
{
    /// <summary>
    /// The effective DPI.
    /// This value should be used when determining the correct scale factor for scaling UI elements.
    /// This incorporates the scale factor set by the user for this specific display.
    /// </summary>
    MDT_EFFECTIVE_DPI = 0,
    /// <summary>
    /// The angular DPI.
    /// This DPI ensures rendering at a compliant angular resolution on the screen.
    /// This does not include the scale factor set by the user for this specific display.
    /// </summary>
    MDT_ANGULAR_DPI = 1,
    /// <summary>
    /// The raw DPI.
    /// This value is the linear DPI of the screen as measured on the screen itself.
    /// Use this value when you want to read the pixel density and not the recommended scaling setting.
    /// This does not include the scale factor set by the user for this specific display and is not guaranteed to be a supported DPI value.
    /// </summary>
    MDT_RAW_DPI = 2
}

[DllImport("user32.dll")]
private static extern bool GetDpiForMonitor(IntPtr hwnd, MonitorDpiType dpiType, out int dpiX, out int dpiY);
0 голосов
/ 05 января 2013
RECT windowsize;    // get the height and width of the screen
GetClientRect(hwnd, &windowsize);

int srcheight = windowsize.bottom;
int srcwidth = windowsize.right;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...