以下在ATL下实现:
.h头文件中添加
#include <vector>
#define WND_TITLE_LEN 256
typedef struct{ HWND hwnd; char wndTitle[WND_TITLE_LEN];}WINDOW_INFO;typedef std::vector <WINDOW_INFO> WNDLISTTYPE;private:
WNDLISTTYPE m_vector_winList; static BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lp);public: HWND FindStrInTitle(CString str);CPP文件实现功能:EnumWindows遍历窗口
BOOL CALLBACK CAddressBookDlg::MyEnumWindowsProc(HWND hwnd, LPARAM lp){
WNDLISTTYPE *wndList; wndList = (WNDLISTTYPE*)lp; char buffer[WND_TITLE_LEN]; WINDOW_INFO wndInfo; ::GetWindowText(hwnd, buffer, WND_TITLE_LEN); wndInfo.hwnd = hwnd; strcpy(wndInfo.wndTitle, buffer); wndList->push_back(wndInfo); return true;}HWND CAddressBookDlg::FindStrInTitle(CString str){ WINDOW_INFO wndInfo; CString csTmp; m_vector_winList.clear(); EnumWindows(MyEnumWindowsProc, (LPARAM)&m_vector_winList); for (unsigned int i = 0; i<m_vector_winList.size(); i++) { wndInfo = m_vector_winList[i]; csTmp = wndInfo.wndTitle; if (csTmp.Find(str) >= 0) { return wndInfo.hwnd; } } return NULL;}
调用:
CAddressBookDlg findWnd;
HWND hwndForward = findWnd.FindStrInTitle("转发:"); if (hwndForward){AfxMessageBox("转发窗口已打开");
}else{
AfxMessageBox("转发窗口未打开");
}