VC Windows API應用之GetDesktopWindow ——獲得桌面所有窗口句柄的方法
Windows API
Windows 這個多作業系統除了協調應用程序的執行、分配內存、管理資源…之外, 它同時也是一個很大的服務中心,調用這個服務中心的各種服務(每一種服務就是一個函數),可以幫應用程式達到開啟視窗、描繪圖形、使用周邊設備等目的,由於這些函數服務的對象是應用程序(Application), 所以便稱之為 Application Programming Interface,簡稱 API 函數。WIN32 API也就是Microsoft Windows 32位平台的應用程序編程接口。
GetDesktopWindow
函數功能:該函數返回桌面窗口的句柄。桌面窗口覆蓋整個屏幕。桌面窗口是一個要在其上繪制所有的圖標和其他窗口的區域。
函數原型:HWND GetDesktopWindow(VOID)
參數:無。
返回值:函數返回桌面窗口的句柄。
速查:Windows NT:3.1以上版本;Windows:95以上版本:;
頭文件:Winuser.h;庫文件:user32.lib。
【聲明】
vb
Public Declare Function GetDesktopWindow Lib “user32” Alias “GetDesktopWindow” () As Long
vb_net
Public Declare Function GetDesktopWindow Lib “user32” Alias “GetDesktopWindow” () As Integer
c#
[DllImport(“user32.dll”, EntryPoint = “GetDesktopWindow”, CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetDesktopWindow();
【說明】
獲得代表整個屏幕的一個窗口(桌面窗口)句柄
【返回值】
Long,桌面窗口的句柄
獲得桌面所有窗口句柄的方法
創建項目
文件->新建->項目…
編寫方法
// GetDesktopWindow.cpp : 定義控制台應用程序的入口點。 #include "stdafx.h" #define _AFXDLL #include <afxwin.h> // 何問起 hovertree.com //錯誤 1 error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. //Please #define _AFXDLL or do not use /MD[d] e:\programfilesx86\microsoftvisualstudio10\vc\atlmfc\include\afx.h 24 1 GetDesktopWindow int _tmain(int argc, _TCHAR* argv[]) { //1.先獲得桌面窗口 CWnd* pDesktopWnd = CWnd::GetDesktopWindow(); //2.獲得一個子窗口 CWnd* pWnd = pDesktopWnd->GetWindow(GW_CHILD); //3.循環取得桌面下的所有子窗口 while(pWnd != NULL) { //獲得窗口類名 CString strClassName = _T(""); ::GetClassName(pWnd->GetSafeHwnd(),strClassName.GetBuffer(256),256); //獲得窗口標題 CString strWindowText = _T(""); ::GetWindowText(pWnd->GetSafeHwnd(),strWindowText.GetBuffer(256),256); //繼續下一個子窗口 pWnd = pWnd->GetWindow(GW_HWNDNEXT); } return 0; }