標籤:des blog http 使用 strong os
1、使用FindWindow函數擷取視窗控制代碼
樣本:使用FindWindow函數擷取視窗控制代碼,然後獲得視窗大小和標題,並且移動視窗到指定位置。
[html] view plaincopy
- #include <Windows.h>
- #include <stdio.h>
- #include <string.h>
- #include <iostream.h>
-
- int main(int argc, char* argv[])
- {
- //根據視窗名擷取QQ遊戲登入視窗控制代碼
- HWND hq=FindWindow(NULL,"QQ2012");
-
- //得到QQ視窗大小
- RECT rect;
- GetWindowRect(hq,&rect);
- int w=rect.right-rect.left,h=rect.bottom-rect.top;
- cout<<"寬:"<<w<<" "<<"高:"<<h<<endl;
-
- //移動QQ視窗位置
- MoveWindow(hq,100,100,w,h,false);
-
- //得到桌面視窗
- HWND hd=GetDesktopWindow();
- GetWindowRect(hd,&rect);
- w=rect.right-rect.left;
- h=rect.bottom-rect.top;
- cout<<"寬:"<<w<<" "<<"高:"<<h<<endl;
-
- return 0;
- }
2、使用EnumWindows和EnumChildWindows函數以及相對的回呼函數EnumWindowsProc和EnumChildWindowsProc擷取所有頂層視窗以及它們的子視窗(有些視窗做了特殊處理,比如QQ是不能通過這個方法獲得的)
樣本:
[html] view plaincopy
- #include "stdafx.h"
- #include <Windows.h>
- #include <stdio.h>
- #include <tchar.h>
- #include <string.h>
- #include <iostream.h>
-
- //EnumChildWindows回呼函數,hwnd為指定的父視窗
- BOOL CALLBACK EnumChildWindowsProc(HWND hWnd,LPARAM lParam)
- {
- char WindowTitle[100]={0};
- ::GetWindowText(hWnd,WindowTitle,100);
- printf("%s\n",WindowTitle);
-
- return true;
- }
-
- //EnumWindows回呼函數,hwnd為發現的頂層視窗
- BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM lParam)
- {
- if (GetParent(hWnd)==NULL && IsWindowVisible(hWnd) ) //判斷是否頂層視窗並且可見
- {
- char WindowTitle[100]={0};
- ::GetWindowText(hWnd,WindowTitle,100);
- printf("%s\n",WindowTitle);
-
- EnumChildWindows(hWnd,EnumChildWindowsProc,NULL); //擷取父視窗的所有子視窗
- }
-
- return true;
- }
-
- int main(int argc, _TCHAR* argv[])
- {
- //擷取螢幕上所有的頂層視窗,每發現一個視窗就調用回呼函數一次
- EnumWindows(EnumWindowsProc ,NULL );
-
- return 0;
- }
3、使用GetDesktopWindow和GetNextWindow函數得到所有的子視窗
樣本:
[html] view plaincopy
- #include "stdafx.h"
- #include <Windows.h>
- #include <stdio.h>
- #include <tchar.h>
- #include <string.h>
- #include <iostream.h>
-
- int main(int argc, _TCHAR* argv[])
- {
- //得到桌面視窗
- HWND hd=GetDesktopWindow();
-
- //得到螢幕上第一個子視窗
- hd=GetWindow(hd,GW_CHILD);
- char s[200]={0};
-
- //迴圈得到所有的子視窗
- while(hd!=NULL)
- {
- memset(s,0,200);
- GetWindowText(hd,s,200);
- /*if (strstr(s,"QQ2012"))
- {
- cout<<s<<endl;
- SetWindowText(hd,"My Windows");
- }*/
- cout<<s<<endl;
-
- hd=GetNextWindow(hd,GW_HWNDNEXT);
- }
-
- return 0;
- }