GameAssistDlg.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // GameAssistDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "GameAssist.h"
  5. #include "GameAssistDlg.h"
  6. //#include "VideoDXGICaptor.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
  11. class CAboutDlg : public CDialog
  12. {
  13. public:
  14. CAboutDlg();
  15. // 对话框数据
  16. enum { IDD = IDD_ABOUTBOX };
  17. protected:
  18. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
  19. // 实现
  20. protected:
  21. DECLARE_MESSAGE_MAP()
  22. };
  23. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  24. {
  25. }
  26. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  27. {
  28. CDialog::DoDataExchange(pDX);
  29. }
  30. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  31. END_MESSAGE_MAP()
  32. // CGameAssistDlg 对话框
  33. CGameAssistDlg::CGameAssistDlg(CWnd* pParent /*=NULL*/)
  34. : CDialog(CGameAssistDlg::IDD, pParent)
  35. {
  36. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  37. }
  38. void CGameAssistDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. }
  42. BEGIN_MESSAGE_MAP(CGameAssistDlg, CDialog)
  43. ON_WM_SYSCOMMAND()
  44. ON_WM_PAINT()
  45. ON_WM_QUERYDRAGICON()
  46. //}}AFX_MSG_MAP
  47. ON_BN_CLICKED(BTN_OPENGAME, &CGameAssistDlg::OnBnClickedOpengame)
  48. ON_BN_CLICKED(IDC_BUTTON2, &CGameAssistDlg::OnBnClickedButton2)
  49. END_MESSAGE_MAP()
  50. // CGameAssistDlg 消息处理程序
  51. BOOL CGameAssistDlg::OnInitDialog()
  52. {
  53. CDialog::OnInitDialog();
  54. // 将“关于...”菜单项添加到系统菜单中。
  55. // IDM_ABOUTBOX 必须在系统命令范围内。
  56. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  57. ASSERT(IDM_ABOUTBOX < 0xF000);
  58. CMenu* pSysMenu = GetSystemMenu(FALSE);
  59. if (pSysMenu != NULL)
  60. {
  61. BOOL bNameValid;
  62. CString strAboutMenu;
  63. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  64. ASSERT(bNameValid);
  65. if (!strAboutMenu.IsEmpty())
  66. {
  67. pSysMenu->AppendMenu(MF_SEPARATOR);
  68. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  69. }
  70. }
  71. // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  72. // 执行此操作
  73. SetIcon(m_hIcon, TRUE); // 设置大图标
  74. SetIcon(m_hIcon, FALSE); // 设置小图标
  75. // TODO: 在此添加额外的初始化代码
  76. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  77. }
  78. void CGameAssistDlg::OnSysCommand(UINT nID, LPARAM lParam)
  79. {
  80. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  81. {
  82. CAboutDlg dlgAbout;
  83. dlgAbout.DoModal();
  84. }
  85. else
  86. {
  87. CDialog::OnSysCommand(nID, lParam);
  88. }
  89. }
  90. // 如果向对话框添加最小化按钮,则需要下面的代码
  91. // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
  92. // 这将由框架自动完成。
  93. void CGameAssistDlg::OnPaint()
  94. {
  95. if (IsIconic())
  96. {
  97. CPaintDC dc(this); // 用于绘制的设备上下文
  98. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  99. // 使图标在工作区矩形中居中
  100. int cxIcon = GetSystemMetrics(SM_CXICON);
  101. int cyIcon = GetSystemMetrics(SM_CYICON);
  102. CRect rect;
  103. GetClientRect(&rect);
  104. int x = (rect.Width() - cxIcon + 1) / 2;
  105. int y = (rect.Height() - cyIcon + 1) / 2;
  106. // 绘制图标
  107. dc.DrawIcon(x, y, m_hIcon);
  108. }
  109. else
  110. {
  111. CDialog::OnPaint();
  112. }
  113. }
  114. //当用户拖动最小化窗口时系统调用此函数取得光标
  115. //显示。
  116. HCURSOR CGameAssistDlg::OnQueryDragIcon()
  117. {
  118. return static_cast<HCURSOR>(m_hIcon);
  119. }
  120. void CGameAssistDlg::OnBnClickedOpengame()
  121. {
  122. #if 1 // 获取游戏标题;
  123. DWORD dwPid = GAssist::FindProcess(_T("Game.exe"));
  124. HWND hProWnd = GAssist::GetProMainHwnd(dwPid);
  125. //HWND hProWnd = GAssist::GetProMainHwnd(5824);
  126. TCHAR szWinText[MAX_PATH] = {0};
  127. ::GetWindowText(hProWnd, szWinText, MAX_PATH);
  128. #if 0
  129. Sleep(1000);
  130. GAssist::MouseMove(hProWnd, CPoint(205,150));
  131. Sleep(2000);
  132. GAssist::MouseMove(hProWnd, CPoint(100,100), CPoint(250,250));
  133. Sleep(2000);
  134. GAssist::MouseMove(hProWnd, CPoint(100,100), CPoint(350,350));
  135. #endif
  136. if (0)
  137. {
  138. Sleep(1000);
  139. GAssist::MouseClick(hProWnd, CPoint(205,150));
  140. Sleep(1000);
  141. GAssist::MouseClick(hProWnd, CPoint(235,150));
  142. Sleep(1000);
  143. GAssist::MouseClick(hProWnd, CPoint(275,150));
  144. }
  145. if (0)
  146. {
  147. Sleep(1000);
  148. ::SetForegroundWindow(hProWnd);
  149. GAssist::DragMouse(hProWnd, CPoint(173,192), CPoint(295,190));
  150. }
  151. if (0){
  152. GAssist::MouseDbClick(hProWnd, CPoint(476, 158));
  153. }
  154. if (0)
  155. {
  156. GAssist::SendKey(hProWnd, VK_TAB);
  157. }
  158. if ( 1 )
  159. {// 小窗口-职业挑战;
  160. // 打开世界地图;
  161. GAssist::SendKey(hProWnd, VK_TAB);Sleep(100);
  162. // Click
  163. GAssist::MouseClick(hProWnd, CPoint(533, 100));Sleep(100);
  164. // 设置剪切板:职业大挑战;
  165. GAssist::SetClipboardString("职业训导大师");Sleep(100);
  166. // Ctrl+V
  167. GAssist::SendKey(hProWnd, 0x56, TRUE);Sleep(100);
  168. // DbClick
  169. GAssist::MouseDbClick(hProWnd, CPoint(485, 162));Sleep(100);
  170. // 退出世界地址;
  171. GAssist::SendKey(hProWnd, VK_TAB);Sleep(100);
  172. }
  173. RECT rc = {0,0,500,500};
  174. HBITMAP hb = GAssist::CopyDC2Bitmap(hProWnd, &rc);
  175. GAssist::SaveBitmap(hb, "1.bmp");
  176. GAssist::SaveHwndToBmpFile(hProWnd, "2.bmp");
  177. return;
  178. #endif
  179. // TODO: 在此添加控件通知处理程序代码
  180. if ( !PathFileExists(GAssist::g_szGamePath) )
  181. {
  182. MessageBox("游戏程序不存在", "温馨提示");
  183. return;
  184. }
  185. // 设置辅助程序工作目录为游戏目录;
  186. //SetCurrentDirectory(_T("E:\\dhsh\\shdata\\"));
  187. SetCurrentDirectory(_T("D:\\tools\\dhsh\\shdata\\"));
  188. #if 1
  189. ShellExecute(NULL, "open", GAssist::g_szGamePath, NULL, NULL, SW_SHOWNORMAL);//SW_HIDE无用,因为会自动结;
  190. Sleep(500); // Main.exe设置了陷阱(自己再开启了一个进程,结束上一个进程),需要等5秒;
  191. #else
  192. SHELLEXECUTEINFO stuExecInfo = { 0 };
  193. DWORD dwExitCode = STILL_ACTIVE;
  194. //CString strCommandLine = _T("open");
  195. stuExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  196. stuExecInfo.lpFile = GAssist::g_szGamePath;
  197. stuExecInfo.lpParameters = "open";
  198. stuExecInfo.nShow = SW_SHOWNORMAL;
  199. stuExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
  200. if (!ShellExecuteEx(&stuExecInfo))
  201. {
  202. DWORD dwError = GetLastError();
  203. CString strError = _T("");
  204. strError.Format(_T("执行ShellExecuteEx失败,错误码:%d"),dwError);
  205. AfxMessageBox(strError);
  206. return ;
  207. }
  208. HANDLE hProcess = stuExecInfo.hProcess;
  209. if ( hProcess != NULL )
  210. {
  211. WaitForSingleObject(hProcess,INFINITE);
  212. CloseHandle(hProcess);
  213. }
  214. #endif
  215. DWORD dwPID = GAssist::FindProcess(_T("Main.exe"));
  216. #if 1
  217. EnumWindows(GAssist::EnumChildWindowCallBack, dwPID);
  218. if ( GAssist::g_vtGameHwnd.size() )
  219. {
  220. GAssist::GameHwnd* gp = NULL;
  221. GAssist::GameHwnd* gbmin = NULL;
  222. GAssist::GameHwnd* gbentry = NULL;
  223. for ( std::vector<GAssist::GameHwnd>::iterator it = GAssist::g_vtGameHwnd.begin(); it != GAssist::g_vtGameHwnd.end(); it++ )
  224. {
  225. if ( _tcsicmp(it->strWinText.c_str(), _T("最小化")) == 0 )
  226. {
  227. gbmin = &*it;
  228. }
  229. if ( _tcsicmp(it->strWinText.c_str(), _T("大话水浒")) == 0 )
  230. {
  231. gp = &*it;
  232. }
  233. if ( _tcsicmp(it->strWinText.c_str(), _T("进入游戏")) == 0 )
  234. {
  235. gbentry = &*it;
  236. }
  237. }
  238. if ( gp && gbentry && gbmin)
  239. {
  240. //::SendMessage(gp->hwnd, WM_LBUTTONDOWN, (WPARAM)gb->dwId, 0);
  241. //::SendMessage(gp->hwnd, WM_LBUTTONUP, (WPARAM)gb->dwId, 0);
  242. // 发送点击事件;
  243. // 先最小化;
  244. ::SendMessage(gp->hwnd,WM_COMMAND,gbmin->dwId,NULL);
  245. // 再进入;
  246. ::SendMessage(gp->hwnd,WM_COMMAND,gbentry->dwId,NULL);
  247. Sleep(1500);
  248. dwPID = GAssist::FindProcess(_T("Game.exe"));
  249. if ( dwPID )
  250. {
  251. GAssist::g_vtGameHwnd.clear();
  252. EnumWindows(GAssist::EnumChildWindowCallBack, dwPID);
  253. HWND hwnd = GAssist::GetProMainHwnd(dwPID);
  254. if ( hwnd )
  255. {
  256. TCHAR szName[MAX_PATH] = {0};
  257. ::GetWindowText(hwnd, szName, MAX_PATH);
  258. CRect rc;
  259. ::GetWindowRect(hwnd, &rc);
  260. TCHAR szLogMsg[MAX_PATH] = {0};
  261. _stprintf_s(szLogMsg, _T("窗口标题:%s, [%d,%d, %d,%d], width=%d, height=%d\n"), szName, rc.top, rc.left, rc.right, rc.bottom, rc.Width(), rc.Height());
  262. OutputDebugString(szLogMsg);
  263. if ( 1 )
  264. { // 800x600
  265. // 发送按钮消息;进入游戏;
  266. GAssist::MouseClick(hwnd, 585, 116);
  267. // 发送按钮消息;下一步;
  268. GAssist::MouseClick(hwnd, 536, 481);
  269. // 发送按钮消息;电信一区;
  270. GAssist::MouseClick(hwnd, 229, 342);
  271. // 发送按钮消息;忘忧谷;
  272. GAssist::MouseClick(hwnd, 335, 244);
  273. // 发送按钮消息;下一步;
  274. GAssist::MouseClick(hwnd, 559, 516);
  275. }
  276. else
  277. {
  278. // 640x480
  279. // 发送按钮消息;进入游戏;
  280. GAssist::MouseClick(hwnd, 440, 75);
  281. // 发送按钮消息;下一步;
  282. GAssist::MouseClick(hwnd, 468, 454);
  283. // 发送按钮消息;电信一区;
  284. GAssist::MouseClick(hwnd, 165, 241);
  285. // 发送按钮消息;忘忧谷;
  286. GAssist::MouseClick(hwnd, 289, 195);
  287. // 发送按钮消息;下一步;
  288. GAssist::MouseClick(hwnd, 494, 469);
  289. }
  290. }
  291. }
  292. }
  293. }
  294. #endif
  295. }
  296. void CGameAssistDlg::OnBnClickedButton2()
  297. {
  298. // TODO: 在此添加控件通知处理程序代码
  299. // VideoDXGICaptor vdx;
  300. // if (vdx.Init())
  301. // {
  302. // INT len = 1024 * 1024 * 1024;
  303. // BYTE* pData = new BYTE[len];
  304. // vdx.CaptureImage(pData, len);
  305. // vdx.Deinit();
  306. // }
  307. }