windows獲取窗口句柄


 

windows獲取窗口句柄

1、使用FindWindow函數獲取窗口句柄

示例:使用FindWindow函數獲取窗口句柄,然后獲得窗口大小和標題,並且移動窗口到指定位置。

 1  
 2 #include <Windows.h>  
 3 #include <stdio.h>  
 4 #include <string.h>  
 5 #include <iostream.h>  
 6   
 7 int main(int argc, char* argv[])  
 8 {  
 9     //根據窗口名獲取QQ游戲登錄窗口句柄  
10     HWND hq=FindWindow(NULL,"QQ2012");    
11   
12     //得到QQ窗口大小  
13     RECT rect;    
14     GetWindowRect(hq,&rect);      
15     int w=rect.right-rect.left,h=rect.bottom-rect.top;  
16     cout<<"寬:"<<w<<" "<<"高:"<<h<<endl;  
17       
18     //移動QQ窗口位置  
19     MoveWindow(hq,100,100,w,h,false);  
20       
21     //得到桌面窗口  
22     HWND hd=GetDesktopWindow();  
23     GetWindowRect(hd,&rect);      
24     w=rect.right-rect.left;  
25     h=rect.bottom-rect.top;  
26     cout<<"寬:"<<w<<" "<<"高:"<<h<<endl;  
27       
28     return 0;  
29 }  

 

2、使用EnumWindows和EnumChildWindows函數以及相對的回調函數EnumWindowsProc和EnumChildWindowsProc獲取所有頂層窗口以及它們的子窗口(有些窗口做了特殊處理,比如QQ是不能通過這個方法獲得的)

示例:

 

 

 1 #include "stdafx.h"  
 2 #include <Windows.h>  
 3 #include <stdio.h>  
 4 #include <tchar.h>  
 5 #include <string.h>  
 6 #include <iostream.h>  
 7   
 8 //EnumChildWindows回調函數,hwnd為指定的父窗口  
 9 BOOL CALLBACK EnumChildWindowsProc(HWND hWnd,LPARAM lParam)  
10 {  
11     char WindowTitle[100]={0};      
12     ::GetWindowText(hWnd,WindowTitle,100);  
13     printf("%s\n",WindowTitle);  
14       
15     return true;     
16 }  
17   
18 //EnumWindows回調函數,hwnd為發現的頂層窗口  
19 BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM lParam)  
20 {  
21     if (GetParent(hWnd)==NULL && IsWindowVisible(hWnd) )  //判斷是否頂層窗口並且可見  
22     {  
23         char WindowTitle[100]={0};      
24         ::GetWindowText(hWnd,WindowTitle,100);  
25         printf("%s\n",WindowTitle);  
26   
27         EnumChildWindows(hWnd,EnumChildWindowsProc,NULL); //獲取父窗口的所有子窗口  
28     }  
29       
30     return true;     
31 }  
32   
33 int main(int argc, _TCHAR* argv[])  
34 {  
35     //獲取屏幕上所有的頂層窗口,每發現一個窗口就調用回調函數一次  
36     EnumWindows(EnumWindowsProc ,NULL );  
37   
38     return 0;  
39 }  

3、使用GetDesktopWindow和GetNextWindow函數得到所有的子窗口
示例:

 1 #include "stdafx.h"  
 2 #include <Windows.h>  
 3 #include <stdio.h>  
 4 #include <tchar.h>  
 5 #include <string.h>  
 6 #include <iostream.h>  
 7   
 8 int main(int argc, _TCHAR* argv[])  
 9 {     
10     //得到桌面窗口  
11     HWND hd=GetDesktopWindow();  
12   
13     //得到屏幕上第一個子窗口  
14     hd=GetWindow(hd,GW_CHILD);  
15     char s[200]={0};  
16   
17     //循環得到所有的子窗口  
18     while(hd!=NULL)  
19     {  
20         memset(s,0,200);  
21         GetWindowText(hd,s,200);  
22         /*if (strstr(s,"QQ2012"))  
23         {  
24             cout<<s<<endl;  
25             SetWindowText(hd,"My Windows");  
26         }*/  
27         cout<<s<<endl;  
28           
29         hd=GetNextWindow(hd,GW_HWNDNEXT);  
30     }  
31   
32     return 0;  
33 } 

 


 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM