擷取視窗控制代碼

來源:互聯網
上載者:User

標籤:des   blog   http   使用   strong   os   

1、使用FindWindow函數擷取視窗控制代碼

樣本:使用FindWindow函數擷取視窗控制代碼,然後獲得視窗大小和標題,並且移動視窗到指定位置。

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

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

樣本:

[html] view plaincopy
  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函數得到所有的子視窗

樣本:

[html] view plaincopy
  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;  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.