Jeff пре 5 година
родитељ
комит
09108717eb

+ 15 - 8
GameAssist/GameAssist/Assist.cpp

@@ -10,6 +10,8 @@
 
 namespace GAssist
 {
+	int frameWidth = 2;
+	int titleBarHeight = 25;
 	// 根据路径名查找进程,返回进程ID;
 	DWORD FindProcess(IN LPCSTR lpProName)
 	{
@@ -181,7 +183,7 @@ namespace GAssist
 	}
 	
 	// 使用WM_LBUTTONDBLCLK完全无效;
-	void MouseClick(HWND hwnd, POINT pt)
+	void MouseClick(HWND hwnd, POINT pt, BOOL bClientRect)
 	{
 		if ( !IsWindow(hwnd) )
 			return;
@@ -193,7 +195,8 @@ namespace GAssist
 		::PostMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(pt.x, pt.y));
 #else// 建议使用这种方式,可以看到鼠标;
 		POINT ptScreen = pt;
-		ClientToScreen(hwnd, &ptScreen);
+		if ( bClientRect )
+			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);
@@ -666,7 +669,7 @@ namespace GAssist
 		return false;
 	}
 
-	BOOL OpenImage(std::string strImag, cv::Mat img)
+	BOOL OpenImage(std::string strImag, cv::Mat &img)
 	{
 		if (!PathFileExists(strImag.c_str()))
 			return FALSE;
@@ -681,7 +684,7 @@ namespace GAssist
 		return TRUE;
 	}
 
-	BOOL SetImgThreshold(cv::Mat srcImg, cv::Mat thresholdImg, long nThresholdVal, long nMaxThresholdVal, int type)
+	BOOL SetImgThreshold(cv::Mat &srcImg, cv::Mat &thresholdImg, long nThresholdVal, long nMaxThresholdVal, int type)
 	{
 		if (srcImg.data == NULL)
 			return FALSE;
@@ -745,10 +748,14 @@ namespace GAssist
 		cv::Point lv_MatchLoc = cv::Point(0, 0);
 		cv::minMaxLoc(matchImg, &lv_nMinVal, &lv_nMaxVal, &lv_nMinLoc, &lv_nMaxLoc);
 
-		rc.left = lv_nMaxLoc.x;
-		rc.top = lv_nMaxLoc.y;
-		rc.right = rc.left + matchImg.cols;
-		rc.bottom = rc.top + matchImg.rows;
+		cv::Rect roi = cv::Rect(lv_nMaxLoc.x, lv_nMaxLoc.y, tempImg.cols, tempImg.rows);
+		cv::Mat roiImg = srcImg(roi);
+		cv::imwrite("G:\\bin\\GameAssist\\match.bmp", roiImg);
+
+		rc.left = lv_nMaxLoc.x - frameWidth;
+		rc.top = lv_nMaxLoc.y - titleBarHeight;
+		rc.right = rc.left + tempImg.cols;
+		rc.bottom = rc.top + tempImg.rows;
 
 		// 是否大于最低匹配度;
 		return (lv_nMaxVal >= lowestMatchValue ? TRUE : FALSE);

+ 5 - 3
GameAssist/GameAssist/Assist.h

@@ -6,6 +6,8 @@
 
 namespace GAssist
 {
+	extern int titleBarHeight;
+	extern int frameWidth;
 	//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);
@@ -49,7 +51,7 @@ namespace GAssist
 	// 启动游戏;
 	extern void StartGame(std::string strGameDir, int nStartCount = 5);
 	// 单击事件;
-	extern void MouseClick(HWND hwnd, POINT pt);
+	extern void MouseClick(HWND hwnd, POINT pt, BOOL bClientRect = TRUE);
 	extern void MouseClick(HWND hwnd, unsigned int x, unsigned int y);
 	extern void MouseDbClick(HWND, POINT pt);
 	// 移动鼠标;
@@ -68,8 +70,8 @@ namespace GAssist
 	void SaveHwndToBmpFile(HWND hWnd, LPCTSTR lpszPath);
 	bool IsCoveredByOtherWindow(HWND  hWnd);
 
-	BOOL OpenImage(std::string strImag, cv::Mat img);
-	BOOL SetImgThreshold(cv::Mat srcImg, cv::Mat thresholdImg, long nThresholdVal, long nMaxThresholdVal, int type = 0);
+	BOOL OpenImage(std::string strImag, cv::Mat &img);
+	BOOL SetImgThreshold(cv::Mat &srcImg, cv::Mat &thresholdImg, long nThresholdVal, long nMaxThresholdVal, int type = 0);
 	BOOL GetImgMatchtemplate(std::string strSrcImg, std::string strTempImg, RECT &rc, double lowestMatchValue = 0.900);
 };
 

+ 2 - 0
GameAssist/GameAssist/GameAssist.vcxproj

@@ -212,6 +212,7 @@
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClCompile Include="Assist.cpp" />
+    <ClCompile Include="CAction.cpp" />
     <ClCompile Include="CVideoCapture.cpp" />
     <ClCompile Include="GameAssist.cpp" />
     <ClCompile Include="GameAssistDlg.cpp" />
@@ -225,6 +226,7 @@
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="Assist.h" />
+    <ClInclude Include="CAction.h" />
     <ClInclude Include="CVideoCapture.h" />
     <ClInclude Include="GameAssist.h" />
     <ClInclude Include="GameAssistDlg.h" />

+ 6 - 0
GameAssist/GameAssist/GameAssist.vcxproj.filters

@@ -33,6 +33,9 @@
     <ClCompile Include="VideoDXGICaptor.cpp">
       <Filter>源文件</Filter>
     </ClCompile>
+    <ClCompile Include="CAction.cpp">
+      <Filter>源文件</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="Assist.h">
@@ -59,6 +62,9 @@
     <ClInclude Include="VideoDXGICaptor.h">
       <Filter>头文件</Filter>
     </ClInclude>
+    <ClInclude Include="CAction.h">
+      <Filter>头文件</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="Assist.ini">

+ 59 - 9
GameAssist/GameAssist/GameAssistDlg.cpp

@@ -207,21 +207,67 @@ void CGameAssistDlg::OnBnClickedOpengame()
 		// 打开世界地图;
 		GAssist::SendKey(hProWnd, VK_TAB);Sleep(100);
 		// Click
-		GAssist::MouseClick(hProWnd, CPoint(533, 100));Sleep(100);
+		GAssist::MouseClick(hProWnd, CPoint(600, 100));Sleep(200);
 		// 设置剪切板:职业大挑战;
-		GAssist::SetClipboardString("职业训导大师");Sleep(100);
+		GAssist::SetClipboardString("职业训导大师");Sleep(200);
 		// Ctrl+V
-		GAssist::SendKey(hProWnd, 0x56, TRUE);Sleep(100);
+		GAssist::SendKey(hProWnd, 0x56, TRUE);Sleep(200);
 		// DbClick
-		GAssist::MouseDbClick(hProWnd, CPoint(485, 162));Sleep(100);
+		GAssist::MouseDbClick(hProWnd, CPoint(485, 162));Sleep(200);
 		// 退出世界地址;
-		GAssist::SendKey(hProWnd, VK_TAB);Sleep(100);
+		GAssist::SendKey(hProWnd, VK_TAB);Sleep(200);
+
+		// 截图;
+		GAssist::SaveHwndToBmpFile(hProWnd, "G:\\bin\\GameAssist\\Game.bmp");
+
+		// 选择进入;
+		RECT rc;
+		BOOL bMatch = FALSE;
+		int trySize = 20;
+		while ( !(bMatch = GAssist::GetImgMatchtemplate(_T("G:\\bin\\GameAssist\\Game.bmp"), _T("G:\\bin\\GameAssist\\zyxdds-wytztm.bmp"), rc)) ) {
+			Sleep(3000);
+			// 截图;
+			GAssist::SaveHwndToBmpFile(hProWnd, "G:\\bin\\GameAssist\\Game.bmp");
+			if ( !trySize-- ) break;
+		}
+
+		if (bMatch) {
+			TRACE2("坐标 x=%d,y=%d\n", rc.left, rc.top);
+			TRACE2("坐标2 x=%d,y=%d\n", (rc.right + rc.left) / 2, (rc.top + rc.bottom) / 2);
+			// 单击:我要挑战他们;
+			GAssist::MouseClick(hProWnd, CPoint((rc.right+rc.left)/2, (rc.top+rc.bottom)/2), TRUE);
+
+			// 再单击:我准备好了;
+			GAssist::SaveHwndToBmpFile(hProWnd, "G:\\bin\\GameAssist\\Game.bmp");
+			if (GAssist::GetImgMatchtemplate(_T("G:\\bin\\GameAssist\\Game.bmp"), _T("G:\\bin\\GameAssist\\zyxdds-wzbhl.bmp"), rc)) {
+				GAssist::MouseClick(hProWnd, CPoint((rc.right + rc.left) / 2, (rc.top + rc.bottom) / 2), TRUE);
+
+				if (1) {// 挑战医师;
+					// 打开世界地图;
+					GAssist::SendKey(hProWnd, VK_TAB); Sleep(100);
+					// Click
+					GAssist::MouseClick(hProWnd, CPoint(600, 100)); Sleep(200);
+					// 设置剪切板:职业大挑战;
+					GAssist::SetClipboardString("职业训导大师"); Sleep(200);
+					// Ctrl+V
+					GAssist::SendKey(hProWnd, 0x56, TRUE); Sleep(200);
+					// DbClick
+					GAssist::MouseDbClick(hProWnd, CPoint(485, 162)); Sleep(200);
+					// 退出世界地址;
+					GAssist::SendKey(hProWnd, VK_TAB); Sleep(200);
+
+					// 截图;
+					GAssist::SaveHwndToBmpFile(hProWnd, "G:\\bin\\GameAssist\\Game.bmp");
+				}
+			}
+		}
+		
 	}
 
-	RECT rc = {0,0,500,500};
-	HBITMAP hb = GAssist::CopyDC2Bitmap(hProWnd, &rc);
-	GAssist::SaveBitmap(hb, "1.bmp");
-	GAssist::SaveHwndToBmpFile(hProWnd, "2.bmp");
+	//RECT rc = {0,0,500,500};
+	//HBITMAP hb = GAssist::CopyDC2Bitmap(hProWnd, &rc);
+	//GAssist::SaveBitmap(hb, "1.bmp");
+	//GAssist::SaveHwndToBmpFile(hProWnd, "2.bmp");
 	return;
 #endif
 
@@ -367,4 +413,8 @@ void CGameAssistDlg::OnBnClickedButton2()
 // 		vdx.CaptureImage(pData, len);
 // 		vdx.Deinit();
 // 	}
+	DWORD dwPid = GAssist::FindProcess(_T("Game.exe"));
+	HWND hProWnd = GAssist::GetProMainHwnd(dwPid);
+	// 截图;
+	GAssist::SaveHwndToBmpFile(hProWnd, "Game.bmp");
 }

+ 20 - 0
README.md

@@ -75,3 +75,23 @@
 
 ### 5、图像处理-Opencv
 
+
+## 三、指令
++ 
+
+### 类型:鼠标指令
++ 单击、右击、双击、移动、拖动;参数=目标点
+
+### 类型:剪切板指令
++ 设置字符串;参数=字符串
+
+### 类型:键盘指令
++ 按键:Ctrl/Alt/Shift + key
++ 粘贴;
+
+### 类型:图像处理指令
++ 模板匹配
++ 
+
+### 类型:百度OCR
++ 文字识别