2013-02-01 13 views
3

我想從Windows API獲取監視數據。 GetSystemMetrics()命令以像素爲單位返回錯誤的寬度。 據微軟的網站上,這是因爲我需要SetProcessDPIAware()Windows API MONITORINFO結構

這意味着我最好能夠創建一個application manifest我不明白。

在尋找同等級別的替代品時,我找到了multiple display monitors functions and structs。我必須通過HMONITOR訪問我想要的矩形結構,但得到HMONITOR是我遇到問題的地方。

MonitorFromWindow(hwnd,MONITOR_DEFAULTTOPRIMARY) 這個命令是出於scope-奇怪,因爲GetMonitorInfo() [我需要HMONITOR用於]不會引起任何問題。我已經有windows.hwindowsx.h包括在內。我錯過了一個圖書館或者是什麼問題?

在另外一張紙條上,看到它後,很明顯,使顯示器使用者可以調整也可能很好。 SM_CMONITORS應該返回一個計數,但我想知道如何將這些數字轉換爲我需要獲取監視器特定信息的HMONITOR數據。

::編輯::

我把編輯在這裏,因爲「評論」功能不爲我提供足夠的空間來放置,其中要求

另外的代碼片段,我使用GNU GCC與MinGW

#include <iostream>//using these libraries 
#include <Windowsx.h> 
#include <windows.h> 

using namespace std; 

int main() 
{ 
    //should print screen width in pixels 

    LPMONITORINFO target; 
     //create a monitor info struct to store the data to 
    HMONITOR Hmon = MonitorFromWindow(hwnd,MONITOR_DEFAULTTOPRIMARY); 
     //create a handle to the main monitor 
     //(should start at top left of screen with (0,0) as apposed to other monitors i believe) 
     //if i could gather aditional info on what monitors are available that might be   useful 
    GetMonitorInfo(Hmon, target); 
     //Get the necessary data and store it to target 

    cout << "bottom of selected monitor in pixels: " << target->rcMonitor.bottom 
     << "Top of the selected monitor" << target->rcMonitor.top 
     << "right extreme of selected monitor" << target->rcMonitor.right 
     << "left extreme of selected monitor" << target->rcMonitor.left; 

    return 0; 
} 
+0

您能否給出一個簡短的完整代碼示例來重現您的問題? – chris

+0

您正在嘗試使用哪種編譯器版本? – Maximus

+0

是的;謝謝。我在問題中添加了附加信息,因爲它不適合分配的評論大小。 – user1964975

回答

6

如果要使用Windows 95/Windows NT 4之後出現的功能,則必須在編譯之前指定WINVER。

的Windows 2000是WINVER0x0500,所以編譯行需要以看到MONITOR_DEFAULTTOPRIMARY不斷增加-DWINVER=0x500

您需要分配一個MONITORINFO結構,而不是一個指針MONITORINFO結構,並intialize的cbSize場,以便Windows知道什麼信息來填充,這樣在你的代碼:

MONITORINFO target; 
target.cbSize = sizeof(MONITORINFO); 

HMONITOR hMon = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY); 
GetMonitorInfo(Hmon, &target); 

,然後顯示使用而不是

target.rcMonitor 

target->rcMonitor 

使用SetProcessDPIAware()是Windows Vista的一項功能,因此需要將WINVER設置爲0x0600,但MinGW附帶的標頭看起來並不是Windows Vista的完整標頭集合 - 該函數定義缺失,但是存在於Windows 7 SDK頭文件中(我手邊沒有Windows Vista SDK來檢查它)。

因此,使用清單似乎比拉動更新的API更容易。

監視器手柄意味着是監視器的不透明表示 - 即您獲得的價值不應該用於其他監視器功能以外的任何其他功能。如果您想要漫步監視器結構,則應使用EnumDisplayMonitors函數和適當的回調例程。

+0

*你必須在編譯之前指定WINVER * [更好的是,讓'sdkddkver.h'設置它](http://blogs.msdn.com/b/oldnewthing/archive/2007/04/11/2079137.aspx) 。它適用於Vista及更高版本。 –

+0

謝謝;因爲你建議我可以使用應用程序清單,我將嘗試學習如何做。我第一次聽說有人正在閱讀這個項目的Windows API,所以最後一個有用的小知識將是一個資源鏈接。我找不到任何教程或信息可能會幫助我把兩個和兩個放在一起。再一次,謝謝大家;那個答案真的很有用。 – user1964975

+0

@ ta.speot.is我正在使用獨立的MinGW,它沒有包含該文件。與Cygwin一起發佈的MinGW似乎擁有該文件,這是一個更爲明智的解決方案 – Petesh