MFC 获取窗口句柄
生活随笔
收集整理的這篇文章主要介紹了
MFC 获取窗口句柄
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、使用FindWindow函數獲取窗口句柄
示例:使用FindWindow函數獲取窗口句柄,然后獲得窗口大小和標題,并且移動窗口到指定位置。
#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是不能通過這個方法獲得的)
示例:
#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函數得到所有的子窗口
示例:
#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; }?
總結
以上是生活随笔為你收集整理的MFC 获取窗口句柄的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刷油保温的小软件
- 下一篇: 干货:怎么提高科技成果转移转化成效?