#pragma once #include <windows.h> struct WININFO { HWND hParent; char szTitle[260]; char szClassName[260]; char szFilePath[260]; RECT WinRT; RECT ClientRT; BOOL IsVisible; BOOL IsMax; BOOL IsMin; }; class WINDOW { private: POINT savedpt; public: HWND hWnd; POINT pt; //构造与析构 WINDOW(); ~WINDOW(); //获取型函数 BOOL FindWindow(char *szWndTitle); BOOL IsWindow(); void GetCurrentActive(); void GetWindowByCursor(); void GetWindowInfo(WININFO *WinInfo); //设置类函数 void SetMaxSize(); void SetMinSize(); void SetTitle(char *szTitle); void SetTopMost(); void SetPos(int x,int y,int cx,int cy); void SetVisible(BOOL bVisible); void SetActive(); //转换函数 void ClientToScreen(); //操作函数 void Close(); void MouseMove(int x,int y); void MouseTo(int x,int y); void MouseSave(); void MouseRestore(); void MouseRelativeMove(int cx,int cy); //发送和提交字符、字符串 void PostChar(char ch); void SendChar(char ch); void PostStr(char *str); void SendStr(char *str); }; WINDOW::WINDOW() { this->hWnd=NULL; pt.x=pt.y=0; this->savedpt.x=this->savedpt.y=0; } WINDOW::~WINDOW() { CloseHandle(this->hWnd); } //查找窗口(窗口名) BOOL WINDOW::FindWindow(char *szWndTitle) { this->hWnd=::FindWindow(NULL,szWndTitle); if (this->hWnd) return TRUE; else return FALSE; } //是否是窗口() BOOL WINDOW::IsWindow() { if (::IsWindow(this->hWnd)) return TRUE; else return FALSE; } //获取当前激活窗口句柄() void WINDOW::GetCurrentActive() { this->hWnd=::GetForegroundWindow(); } //根据当前鼠标位置获取窗口句柄() void WINDOW::GetWindowByCursor() { POINT pt; GetCursorPos(&pt); this->hWnd=::WindowFromPoint(pt); } //获取窗口信息(WININFO变量) void WINDOW::GetWindowInfo(WININFO *WinInfo) { WinInfo->hParent=::GetParent(this->hWnd); ::GetWindowText(this->hWnd,WinInfo->szTitle,260); ::GetClassName(this->hWnd,WinInfo->szClassName,260); ::GetWindowRect(this->hWnd,&WinInfo->WinRT); ::GetClientRect(this->hWnd,&WinInfo->ClientRT); DWORD PID; GetWindowThreadProcessId(this->hWnd,&PID); HANDLE hProcess=::OpenProcess(PROCESS_ALL_ACCESS,FALSE,PID); HMODULE hPsapi=LoadLibrary("psapi.dll"); typedef DWORD (WINAPI *GetModuleFileNameExFunc)(HANDLE hProcess,HMODULE hModule,LPTSTR lpFilename,DWORD nSize); GetModuleFileNameExFunc GetModuleFileNameEx=(GetModuleFileNameExFunc)GetProcAddress(hPsapi,"GetModuleFileNameExA"); GetModuleFileNameEx(hProcess,0,WinInfo->szFilePath,260); CloseHandle(hProcess); if (::IsWindowVisible(this->hWnd)) WinInfo->IsVisible=TRUE; else WinInfo->IsVisible=FALSE; if (::IsIconic(this->hWnd)) WinInfo->IsMin=TRUE; else WinInfo->IsMin=FALSE; if (::IsZoomed(this->hWnd)) WinInfo->IsMax=TRUE; else WinInfo->IsMax=FALSE; } //最大化窗口() void WINDOW::SetMaxSize() { ::ShowWindow(this->hWnd,SW_MAXIMIZE); } //最小化窗口() void WINDOW::SetMinSize() { ::ShowWindow(this->hWnd,SW_MINIMIZE); } //设置窗口标题(标题字符串) void WINDOW::SetTitle(char *szTitle) { ::SetWindowText(this->hWnd,szTitle); } //窗口置顶() void WINDOW::SetTopMost() { RECT rect; ::GetWindowRect(this->hWnd,&rect); ::SetWindowPos(this->hWnd,HWND_TOPMOST,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW); } //激活窗口() void WINDOW::SetActive() { ::SetForegroundWindow(this->hWnd); } //设置位置(X坐标,Y坐标,CX,CY) void WINDOW::SetPos(int x,int y,int cx=-1,int cy=-1) { RECT rect; ::GetWindowRect(this->hWnd,&rect); if (cx==-1 && cy==-1) ::SetWindowPos(this->hWnd,HWND_NOTOPMOST,x,y,rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW); else if (x==-1 && y==-1) ::SetWindowPos(this->hWnd,HWND_NOTOPMOST,rect.left,rect.top,cx,cy,SWP_SHOWWINDOW); else ::SetWindowPos(this->hWnd,HWND_NOTOPMOST,x,y,cx,cy,SWP_SHOWWINDOW); } //是否可见(是否可见) void WINDOW::SetVisible(BOOL bVisible) { if (bVisible) ::ShowWindow(this->hWnd,SW_SHOWNORMAL); else ::ShowWindow(this->hWnd,SW_HIDE); } //关闭窗口(句柄) void WINDOW::Close() { SendMessage(this->hWnd,WM_CLOSE,0,0); } //客户到屏幕坐标() void WINDOW::ClientToScreen() { this->pt.x=0; this->pt.y=0; ::ClientToScreen(this->hWnd,&pt); } //保存鼠标位置() void WINDOW::MouseSave() { this->ClientToScreen(); ::GetCursorPos(&this->savedpt); this->savedpt.x-=this->pt.x; this->savedpt.y-=this->pt.y; } //恢复鼠标位置() void WINDOW::MouseRestore() { this->ClientToScreen(); ::SetCursorPos(this->pt.x+this->savedpt.x,this->pt.y+this->savedpt.y); } //移动鼠标(int x,int y) void WINDOW::MouseMove(int x,int y) { this->MouseSave(); ::mouse_event(MOUSEEVENTF_MOVE,x,y,0,0); } //设置鼠标位置(int x,int y) void WINDOW::MouseTo(int x,int y) { this->MouseSave(); ::SetCursorPos(this->pt.x+x,this->pt.y+y); } //设置相对位置(int cx,int cy) void WINDOW::MouseRelativeMove(int cx,int cy) { this->MouseSave(); ::SetCursorPos(this->pt.x+this->savedpt.x+cx,this->pt.y+this->savedpt.y+cy); } //提交字符(字符) void WINDOW::PostChar(char ch) { ::PostMessage(this->hWnd,WM_KEYDOWN,(WPARAM)(int)ch,0); } //发送字符(字符) void WINDOW::SendChar(char ch) { ::SendMessage(this->hWnd,WM_KEYDOWN,(WPARAM)(int)ch,0); ::SendMessage(this->hWnd,WM_KEYUP,(WPARAM)(int)ch,0); } //提交字符串(字符串) void WINDOW::PostStr(char *str) { for (int i=0;i<strlen(str);i++) { if (str[i]>0x60 && str[i]<0x7B) this->PostChar(str[i]-0x20); else this->PostChar(str[i]); } } //发送字符串(字符串) void WINDOW::SendStr(char *str) { for (int i=0;i<strlen(str);i++) { if (str[i]>0x60 && str[i]<0x7B) this->SendChar(str[i]-0x20); else this->SendChar(str[i]); } }
转载于:https://www.cnblogs.com/littleevil/archive/2012/05/29/2523154.html