2010-01-28 47 views
15

Làm thế nào để có được độ phân giải màn hình màn hình từ hWnd?Làm thế nào để có được độ phân giải màn hình màn hình từ hWnd?

Tôi đang sử dụng hWnd vì cửa sổ có thể nằm trên bất kỳ một trong nhiều màn hình.

ví dụ đầu hWnd/trái phối hợp là trên Màn hình có độ phân giải màn hình 800 x 600.

tôi chương trình bằng một ngôn ngữ được gọi là PL/B và nó cho phép gọi Windows API.

API cửa sổ nào có thể được sử dụng?

Trả lời

17

Chức năng user32MonitorFromWindow cho phép bạn chuyển sang chế độ hwnd và trả về tay cầm cho màn hình đang bật (hoặc mặc định - xem bài viết MSDN được liên kết để biết chi tiết). Với điều đó, bạn có thể gọi GetMonitorInfo để truy xuất MONITORINFO struct chứa RECT nêu chi tiết độ phân giải của nó.

Xem phần Multiple Screens Reference của MSDN để biết thêm chi tiết.

Tôi muốn thêm mã mẫu nhưng tôi không biết ngôn ngữ bạn đã tham chiếu và tôi không biết mã ví dụ C# hữu ích như thế nào đối với bạn. Nếu bạn nghĩ rằng nó sẽ giúp đỡ, hãy cho tôi biết và tôi sẽ mã hóa một cái gì đó thật nhanh.

+0

Cảm ơn bạn rất nhiều Erik. Tôi đã có thể chuyển đổi nó sang PL/B bằng cách sử dụng các tài liệu tham khảo bạn cung cấp. –

+0

Tuyệt vời, tôi rất vui khi biết điều đó. =) –

4

Có GetSystemMetrics quá, check it out trên MSDN

+3

Đúng, tuy nhiên việc sử dụng MonitorFromWindow sẽ hoạt động tốt hơn trong trường hợp người dùng cuối có nhiều màn hình. – sidewinderguy

+0

@sidewinderguy RECT rcWork; biểu thị trong cấu trúc MONITORINFO? Không nhận được ý nghĩa của "hình chữ nhật khu vực làm việc của màn hình hiển thị" trên MSDN – control

16

Dưới đây là một mã C++ ví dụ mà làm việc cho tôi:

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; 
+0

Tôi thích phương pháp này. Nó làm việc cho tôi trong C++ là tốt. –

-1
RECT windowsize; // get the height and width of the screen 
GetClientRect(hwnd, &windowsize); 

int srcheight = windowsize.bottom; 
int srcwidth = windowsize.right; 
+0

Đây là kích thước cửa sổ, không phải kích thước màn hình. – NateS

2

Dưới đây là một số mã C# mà được độ phân giải (trong DPI) qua P/Gọi:

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); 
Các vấn đề liên quan