Explorar o código

整理函数:
GetProcessId和GetProcessWnd

Jeff %!s(int64=4) %!d(string=hai) anos
pai
achega
13845c1127

+ 139 - 11
GameAssist/GameAssist/Assist.cpp

@@ -36,6 +36,91 @@ namespace GAssist
 		return dwProcessID;
 	}
 
+	DWORD GetProcessId(LPCTSTR lpProcessName /* = _T("Game.exe") */)
+	{
+		DWORD dwProcessId = 0;
+		PROCESSENTRY32	pe32 = { 0 };
+
+		HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+		if ( hProcessSnap == NULL ) return 0;
+		
+		// 遍历进程;
+		pe32.dwSize = sizeof(PROCESSENTRY32);
+		for ( BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32) ) {
+			if ( _tcscmp(lpProcessName, pe32.szExeFile) == 0 ) {
+				dwProcessId = pe32.th32ProcessID;
+				break;
+			}
+		}
+		
+		CloseHandle(hProcessSnap);
+
+		return dwProcessId;
+	}
+
+	HWND GetProcessWnd(const DWORD &dwTagetProcessId, LPCTSTR lpTagetWndName /* = _T("大话水浒") */)
+	{
+		DWORD dwCurPorcessId = 0;
+		HWND hTagetProcessWnd = NULL;
+		TCHAR szWndName[MAX_PATH] = { 0 };
+		TCHAR szClassName[MAX_PATH] = { 0 };
+
+		// 取得第一个窗口句柄;
+		for ( HWND hCurWnd = ::GetTopWindow(NULL); hCurWnd != NULL; hCurWnd = ::GetNextWindow(hCurWnd, GW_HWNDNEXT) ) {
+			// 重置为0;
+			dwCurPorcessId = 0;
+			// 通过窗口句柄反查进程pid;
+			DWORD dwThreadId = ::GetWindowThreadProcessId(hCurWnd, &dwCurPorcessId);
+			if ( dwThreadId != 0 ) {
+				// 判断当前进程id是否和目标进程id相同;
+				if ( dwCurPorcessId == dwTagetProcessId ) {
+					if ( lpTagetWndName == NULL ) {
+						hTagetProcessWnd = hCurWnd;
+						break;
+					}
+					else{
+						// 获取窗口名称;
+						::GetWindowText(hCurWnd, szWndName, sizeof(szWndName)/sizeof(TCHAR));
+						// 获取窗口类名;
+						::GetClassName(hCurWnd, szClassName, sizeof(szClassName)/sizeof(TCHAR));
+#ifdef _DEBUG
+						TCHAR szLogMsg[MAX_PATH] = {0};
+						_stprintf_s(szLogMsg, _T("类名:%s, 窗口名:%s,窗口地址:%p \n"), szClassName, szWndName, hCurWnd);
+						OutputDebugString(szLogMsg);
+#endif
+						if ( _tcsstr(szWndName, lpTagetWndName) != NULL ) {
+							hTagetProcessWnd = hCurWnd;
+							break;
+						}
+					}
+				}
+			}
+		}
+
+		// 当前窗口有可能不是进程父窗口;
+		HWND hParentWnd = hTagetProcessWnd;
+		while ( hParentWnd ) {
+			hParentWnd = ::GetParent(hTagetProcessWnd);
+			if ( hParentWnd == NULL )
+				break;
+			hTagetProcessWnd = hParentWnd;
+		}
+
+		return hTagetProcessWnd;
+	}
+
+	HWND GetProcessWnd(LPCTSTR lpProcessName /* = _T("Game.exe") */, LPCTSTR lpTagetWndName /* = _T("大话水浒") */)
+	{
+		HWND hTagetWnd = NULL;
+		DWORD dwProcessId = 0;
+		if ( (dwProcessId = GetProcessId(lpProcessName)) != 0 ) {
+			hTagetWnd = GetProcessWnd(dwProcessId, lpTagetWndName);
+		}
+
+		return hTagetWnd;
+	}
+	
+
 	HWND GetProHwnd(DWORD dwProcessId)
 	{
 		DWORD dwPID = 0;
@@ -184,9 +269,18 @@ namespace GAssist
 		if ( !IsWindow(hwnd) )
 			return;
 		
+		::ShowWindow(hwnd, SW_SHOWNORMAL);
 		::SetForegroundWindow(hwnd); // 窗口前置才能单击成功;
+#if 0 // 使用Post或Send的方式,鼠标是不会显示出来的;
 		::PostMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(pt.x, pt.y));
 		::PostMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(pt.x, pt.y));
+#else
+		POINT ptScreen = pt;
+		ClientToScreen(hwnd, &ptScreen);
+		::SetCursorPos(ptScreen.x, ptScreen.y); // 必须将鼠标定位到目标单击点;
+		mouse_event(MOUSEEVENTF_LEFTDOWN, ptScreen.x, ptScreen.y, 0, 0);
+		mouse_event(MOUSEEVENTF_LEFTUP, ptScreen.x, ptScreen.y, 0, 0);
+#endif
 		OutputDebugString(_T("-------------------发送MouseClick\n"));
 		Sleep(200);
 	}
@@ -196,8 +290,18 @@ namespace GAssist
 		if ( !IsWindow(hwnd) )
 			return;
 
-		::SendMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y));
-		::SendMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
+		::ShowWindow(hwnd, SW_SHOWNORMAL);
+		::SetForegroundWindow(hwnd); // 窗口前置才能单击成功;
+#if 0
+		::PostMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y));
+		::PostMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
+#else
+		POINT ptScreen = {x,y};
+		ClientToScreen(hwnd, &ptScreen);
+		::SetCursorPos(ptScreen.x, ptScreen.y);
+		mouse_event(MOUSEEVENTF_LEFTDOWN, ptScreen.x, ptScreen.y, 0, 0);
+		mouse_event(MOUSEEVENTF_LEFTUP, ptScreen.x, ptScreen.y, 0, 0);
+#endif
 		Sleep(200);
 	}
 
@@ -206,7 +310,7 @@ namespace GAssist
 		if ( !IsWindow(hwnd) )
 			return;
 
-		::SendMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(pt.x, pt.y));
+		::PostMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(pt.x, pt.y));
 		Sleep(200);
 	}
 
@@ -216,18 +320,18 @@ namespace GAssist
 			return;
 
 		//::SendMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, lparam);
-		::SendMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(ptStart.x, ptStart.y));
+		::PostMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(ptStart.x, ptStart.y));
 		Sleep(200);
 
 #if 1
 		// 计算个中间点;
 		POINT ptMid = { (ptStart.x + ptEnd.x) / 2, (ptStart.y + ptEnd.y) / 2};
-		::SendMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(ptMid.x, ptMid.y));
+		::PostMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(ptMid.x, ptMid.y));
 		Sleep(200);
 #endif
 
 		//::SendMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, lparam);
-		::SendMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(ptEnd.x, ptEnd.y));
+		::PostMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(ptEnd.x, ptEnd.y));
 		Sleep(200);
 	}
 
@@ -238,18 +342,18 @@ namespace GAssist
 			return;
 
 		//::SendMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, lparam);
-		::SendMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(ptStart.x, ptStart.y));
+		::PostMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(ptStart.x, ptStart.y));
 		Sleep(200);
 
 #if 1
 		// 计算个中间点;
 		POINT ptMid = { (ptStart.x + ptEnd.x) / 2, (ptStart.y + ptEnd.y) / 2};
-		::SendMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(ptMid.x, ptMid.y));
+		::PostMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(ptMid.x, ptMid.y));
 		Sleep(200);
 #endif
 
 		//::SendMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, lparam);
-		::SendMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(ptEnd.x, ptEnd.y));
+		::PostMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(ptEnd.x, ptEnd.y));
 		Sleep(200);
 		MouseClick(hwnd, ptEnd);
 	}
@@ -259,6 +363,8 @@ namespace GAssist
 		if ( !IsWindow(hwnd) )
 			return;
 		
+		::ShowWindow(hwnd, SW_SHOWNORMAL);
+		::SetForegroundWindow(hwnd);
 #if 0
 		MouseClick(hwnd, ptStart);
 		// 按下鼠标左键;
@@ -267,13 +373,35 @@ namespace GAssist
 
 		::SendMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(ptEnd.x, ptEnd.y));
 		Sleep(200);
-#else
+#endif
+
+#if 1
+		POINT ptScreen = ptStart;
+		ClientToScreen(hwnd, &ptScreen);
+		::SetCursorPos(ptScreen.x, ptScreen.y);
+
 		::PostMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(ptStart.x, ptStart.y));
+		Sleep(1200);
 
 		::PostMessage(hwnd, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(ptEnd.x, ptEnd.y));
-
+		Sleep(200);
 		::PostMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(ptEnd.x, ptEnd.y));
 #endif
+
+#if 0
+		// 能成功拖动游戏里的东西。
+		POINT ptSStart = ptStart;
+		POINT ptSEnd = ptEnd;
+		ClientToScreen(hwnd, &ptSStart);
+		ClientToScreen(hwnd, &ptSEnd);
+		::SetCursorPos(ptSStart.x, ptSStart.y);
+
+		mouse_event(MOUSEEVENTF_LEFTDOWN, ptSStart.x, ptSStart.y, 0, 0);
+		Sleep(1200);
+		mouse_event(MOUSEEVENTF_MOVE, 100, 0, 0, 0);
+		Sleep(500);
+		mouse_event(MOUSEEVENTF_LEFTUP, ptSEnd.x, ptSEnd.y, 0, 0);
+#endif
 	}
 
 	// 截图;

+ 16 - 1
GameAssist/GameAssist/Assist.h

@@ -8,7 +8,7 @@ namespace GAssist
 {
 	//const TCHAR g_szGamePath[MAX_PATH] = _T("E:\\dhsh\\shdata\\Main.exe");
 	const TCHAR g_szGamePath[MAX_PATH] = _T("D:\\tools\\dhsh\\shdata\\Main.exe");
-	extern DWORD FindProcess(IN LPCSTR lpProName);
+	extern DWORD FindProcess(IN LPCSTR lpProName = _T("Game.exe"));
 	extern HWND GetProHwnd(DWORD dwProcessId);
 	extern BOOL CALLBACK EnumChildWindowCallBack(HWND hWnd, LPARAM lParam);
 
@@ -19,6 +19,14 @@ namespace GAssist
 		HWND			hwnd;				// 控件句柄;
 	}GameHwnd, *pGameHwnd;
 
+
+	// 地图属性;
+	typedef struct  _MapProperty_  	{
+		std::string strMapName;				// 地图名称;
+		std::string strMapIcon;				// 地图图标;
+		void *		pMapIcon;				// 地图图标数据;
+	};
+
 	extern std::vector<GameHwnd> g_vtGameHwnd;
 
 	typedef struct __GAC__ {
@@ -48,6 +56,13 @@ namespace GAssist
 
 	// 启动游戏;
 	extern void StartGame(std::string strGameDir, int nStartCount = 5);
+
+	// 根据进程名获取进程pid;
+	extern DWORD GetProcessId(LPCTSTR lpProcessName = _T("Game.exe"));
+	// 根据进程pid获取进程对应程序的窗口句柄;
+	extern HWND GetProcessWnd(const DWORD &dwTagetProcessId, LPCTSTR lpTagetWndName = _T("大话水浒"));
+	extern HWND GetProcessWnd(LPCTSTR lpProcessName = _T("Game.exe"), LPCTSTR lpTagetWndName = _T("大话水浒"));
+
 	// 单击事件;
 	extern void MouseClick(HWND hwnd, POINT pt);
 	extern void MouseClick(HWND hwnd, unsigned int x, unsigned int y);

+ 6 - 0
GameAssist/GameAssist/GameAssist.cpp

@@ -322,6 +322,12 @@ BOOL CGameAssistApp::InitInstance()
 	HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&pCreateDevEnum);
 #endif
 
+	
+#ifdef _DEBUG
+	HWND hGameWnd = GAssist::GetProcessWnd();
+
+	return FALSE;
+#endif
 	// 标准初始化
 	// 如果未使用这些功能并希望减小
 	// 最终可执行文件的大小,则应移除下列

+ 4 - 4
GameAssist/GameAssist/GameAssistDlg.cpp

@@ -187,15 +187,15 @@ void CGameAssistDlg::OnBnClickedOpengame()
 
 	if (1)
 	{
-		Sleep(5000);
+		Sleep(1000);
 		::SetForegroundWindow(hProWnd);
-		GAssist::DragMouse(hProWnd, CPoint(243,138), CPoint(295,190));
+		GAssist::DragMouse(hProWnd, CPoint(173,192), CPoint(295,190));
 	}
 
 	RECT rc = {0,0,500,500};
 	HBITMAP hb = GAssist::CopyDC2Bitmap(hProWnd, &rc);
-	GAssist::SaveBitmap(hb, "D:\\1.bmp");
-	GAssist::SaveHwndToBmpFile(hProWnd, "D:\\2.bmp");
+	GAssist::SaveBitmap(hb, "1.bmp");
+	GAssist::SaveHwndToBmpFile(hProWnd, "2.bmp");
 	return;
 #endif