浏览代码

修改GetProcHwnd的问题。

Jeff 5 年之前
父节点
当前提交
44dcc90959

+ 14 - 6
GameAssist/GameAssist/Assist.cpp

@@ -33,6 +33,9 @@ namespace GAssist
 	{
 		DWORD dwPID = 0;
 		HWND hwndRet = NULL;
+		TCHAR szName[MAX_PATH] = {0};
+		TCHAR szClass[MAX_PATH] = {0};
+		TCHAR szLogMsg[MAX_PATH] = {0};
 		// 取得第一个窗口句柄
 		HWND hwndWindow = ::GetTopWindow(NULL);
 		// 遍历出结果窗口;
@@ -45,16 +48,21 @@ namespace GAssist
 				if (dwPID == dwProcessId) {
 					// 进程ID相等,则记录窗口句柄
 					hwndRet = hwndWindow;
-					break;	//存在Bug:有些进程有多个分离的窗口;
+					//break;	//存在Bug:有些进程有多个分离的窗口;
+					::GetWindowText(hwndRet, szName, sizeof(szName) / sizeof(TCHAR));
+					::GetClassName(hwndRet, szClass, sizeof(szClass) / sizeof(TCHAR)); // 窗口类
+					//TRACE3(_T("%s %s %s"), szClass, szName, "\n");
+					_stprintf_s(szLogMsg, _T("类名:%s, 窗口名:%s,窗口地址:%p \n"), szClass, szName, hwndRet);
+					OutputDebugString(szLogMsg);
+					if ( _tcsstr(szName, _T("大话水浒")) )
+						break;
 				}
 			}
 			// 取得下一个窗口句柄
 			hwndWindow = ::GetNextWindow(hwndWindow, GW_HWNDNEXT);
 		}
 
-		TCHAR szName[MAX_PATH] = {0};
-		TCHAR szClass[MAX_PATH] = {0};
-		TCHAR szLogMsg[MAX_PATH] = {0};
+		
 		// 上面取得的窗口,不一定是最上层的窗口,需要通过GetParent获取最顶层窗口
 		HWND hwndWindowParent = NULL;
 		// 循环查找父窗口,以便保证返回的句柄是最顶层的窗口句柄
@@ -190,8 +198,8 @@ namespace GAssist
 		if ( !lpRect || IsRectEmpty(lpRect) )
 			return NULL;
 
-		//HDC hWndDC = ::GetWindowDC(hWnd);
-		HDC hWndDC = ::GetDC(hWnd);
+		HDC hWndDC = ::GetWindowDC(hWnd);
+		//HDC hWndDC = ::GetDC(hWnd);
 		// 创建兼容DC;
 		HDC hMemDC = CreateCompatibleDC(hWndDC);
 		CRect rc(lpRect->top, lpRect->left, lpRect->right, lpRect->bottom);

+ 1 - 1
GameAssist/GameAssist/GameAssist.cpp

@@ -313,7 +313,7 @@ BOOL CGameAssistApp::InitInstance()
 
 	AfxEnableControlContainer();
 
-#if 1
+#if 0
 	DirectShowDemo();
 	std::vector<std::string> vtDevices;
 	listDevices(vtDevices);

+ 18 - 7
GameAssist/GameAssist/GameAssist.rc

@@ -16,13 +16,11 @@
 #undef APSTUDIO_READONLY_SYMBOLS
 
 /////////////////////////////////////////////////////////////////////////////
-// 中文(中华人民共和国) resources
+// 中文(简体,中国) resources
 
 #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
-#ifdef _WIN32
 LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
 #pragma code_page(936)
-#endif //_WIN32
 
 #ifdef APSTUDIO_INVOKED
 /////////////////////////////////////////////////////////////////////////////
@@ -72,6 +70,7 @@ END
 // remains consistent on all systems.
 IDR_MAINFRAME           ICON                    "res\\GameAssist.ico"
 
+
 /////////////////////////////////////////////////////////////////////////////
 //
 // Dialog
@@ -97,7 +96,8 @@ BEGIN
     DEFPUSHBUTTON   "确定",IDOK,209,179,50,14
     PUSHBUTTON      "取消",IDCANCEL,263,179,50,14
     CTEXT           "TODO: 在此放置对话框控件。",IDC_STATIC,10,96,300,8
-    PUSHBUTTON      "打开游戏",BTN_OPENGAME,7,7,50,14
+    PUSHBUTTON      "打开",BTN_OPENGAME,7,7,50,14
+    PUSHBUTTON      "Button2",IDC_BUTTON2,8,28,50,14
 END
 
 
@@ -146,7 +146,7 @@ END
 //
 
 #ifdef APSTUDIO_INVOKED
-GUIDELINES DESIGNINFO 
+GUIDELINES DESIGNINFO
 BEGIN
     IDD_ABOUTBOX, DIALOG
     BEGIN
@@ -167,17 +167,28 @@ END
 #endif    // APSTUDIO_INVOKED
 
 
+/////////////////////////////////////////////////////////////////////////////
+//
+// AFX_DIALOG_LAYOUT
+//
+
+IDD_GAMEASSIST_DIALOG AFX_DIALOG_LAYOUT
+BEGIN
+    0
+END
+
+
 /////////////////////////////////////////////////////////////////////////////
 //
 // String Table
 //
 
-STRINGTABLE 
+STRINGTABLE
 BEGIN
     IDS_ABOUTBOX            "关于 GameAssist(&A)..."
 END
 
-#endif    // 中文(中华人民共和国) resources
+#endif    // 中文(简体,中国) resources
 /////////////////////////////////////////////////////////////////////////////
 
 

+ 100 - 2
GameAssist/GameAssist/GameAssistDlg.cpp

@@ -64,6 +64,7 @@ BEGIN_MESSAGE_MAP(CGameAssistDlg, CDialog)
 	ON_WM_QUERYDRAGICON()
 	//}}AFX_MSG_MAP
 	ON_BN_CLICKED(BTN_OPENGAME, &CGameAssistDlg::OnBnClickedOpengame)
+	ON_BN_CLICKED(IDC_BUTTON2, &CGameAssistDlg::OnBnClickedButton2)
 END_MESSAGE_MAP()
 
 
@@ -156,7 +157,8 @@ HCURSOR CGameAssistDlg::OnQueryDragIcon()
 void CGameAssistDlg::OnBnClickedOpengame()
 {
 #if 1 // 获取游戏标题;
-	HWND hProWnd = GAssist::GetProHwnd(GAssist::FindProcess(_T("Game.exe")));
+	DWORD dwPid = GAssist::FindProcess(_T("Game.exe"));
+	HWND hProWnd = GAssist::GetProHwnd(dwPid);
 	//HWND hProWnd = GAssist::GetProHwnd(5824);
 	TCHAR szWinText[MAX_PATH] = {0};
 	::GetWindowText(hProWnd, szWinText, MAX_PATH);
@@ -164,7 +166,7 @@ void CGameAssistDlg::OnBnClickedOpengame()
 	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::SaveHwndToBmpFile(hProWnd, "D:\\2.bmp");
 	return;
 #endif
 
@@ -296,3 +298,99 @@ void CGameAssistDlg::OnBnClickedOpengame()
 	}
 #endif
 }
+
+
+void CGameAssistDlg::OnBnClickedButton2()
+{
+	// TODO: 在此添加控件通知处理程序代码
+	 //创建DirectDraw
+	if (FAILED(DirectDrawCreate(NULL, &ddraw1, NULL)))
+	{
+		TRACE(/ "Couldn/'t create DirectDraw object.//n/");
+	}
+	if (FAILED(ddraw1->QueryInterface(IID_IDirectDraw2, (LPVOID*)&ddraw2)))
+	{
+		TRACE(/ "Couldn/'t query the interface.//n/");
+	}
+	if (ddraw2)
+	{
+		ddraw2->SetCooperativeLevel(GetSafeHwnd(), DDSCL_NORMAL);
+	}
+	if (ddraw1)
+	{
+		ddraw1->Release();
+		ddraw1 = NULL;
+	}
+
+	//创建主表面
+	HRESULT r;
+
+	DDSURFACEDESC desc;
+	desc.dwSize = sizeof(desc);
+	desc.dwFlags = DDSD_CAPS;
+	desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
+	r = ddraw2->CreateSurface(&desc, &primsurf, 0);
+	if (r != DD_OK)
+	{
+		AfxMessageBox(/ "Create DirectX Surface failed//n /");
+		PostMessage(WM_CLOSE);
+	}
+	r = ddraw2->CreateClipper(0, &clipper, 0);
+	if (r != DD_OK)
+	{
+		AfxMessageBox(/ "CreateClipper() fialed//n /");
+		PostMessage(WM_CLOSE);
+	}
+	r = clipper->SetHWnd(0, GetSafeHwnd());
+	if (r != DD_OK)
+	{
+		AfxMessageBox(/ "SetHWnd() failed//n /");
+		PostMessage(WM_CLOSE);
+	}
+	r = primsurf->SetClipper(clipper);
+	if (r != DD_OK)
+	{
+		AfxMessageBox(/ "SetClipper() fialed//n /");
+		PostMessage(WM_CLOSE);
+	}
+
+	//创建后台表面
+	ZeroMemory(&desc, sizeof(desc));
+	desc.dwSize = sizeof(desc);
+	desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
+	desc.dwWidth = ::GetSystemMetrics(SM_CXSCREEN);
+	desc.dwHeight = ::GetSystemMetrics(SM_CYSCREEN);
+	desc.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
+
+	r = ddraw2->CreateSurface(&desc, &backsurf, 0);
+	//收集图象参数
+	DDSURFACEDESC ddsd;
+
+	ZeroMemory(&ddsd, sizeof(ddsd));
+	ddsd.dwSize = sizeof(ddsd);
+	backsurf->GetSurfaceDesc(&ddsd);
+	BitCount = ddsd.ddpfPixelFormat.dwRGBBitCount;
+	lPitch = ddsd.lPitch;
+	dwWidth = ddsd.dwWidth;
+	dwHeight = ddsd.dwHeight;
+	dwRBitMask = ddsd.ddpfPixelFormat.dwRBitMask;
+	dwGBitMask = ddsd.ddpfPixelFormat.dwGBitMask;
+	dwBBitMask = ddsd.ddpfPixelFormat.dwBBitMask;
+	//确定16Bit色时的修正(16Bit色时有两种显示格式565和555)
+	RMove = 8;//这里先假定为565格式
+	GMove = 3;
+	BMove = 3;
+	if (BitCount == 16)
+	{
+		if (dwGBitMask == 992)
+		{
+			//555格式
+			RMove = 7;
+			GMove = 2;
+		}
+		if (dwGBitMask == 1984)
+			//555格式
+			BMove = 2;
+	}
+
+}

+ 1 - 0
GameAssist/GameAssist/GameAssistDlg.h

@@ -31,4 +31,5 @@ protected:
 	DECLARE_MESSAGE_MAP()
 public:
 	afx_msg void OnBnClickedOpengame();
+	afx_msg void OnBnClickedButton2();
 };

+ 4 - 3
GameAssist/GameAssist/resource.h

@@ -1,6 +1,6 @@
 //{{NO_DEPENDENCIES}}
-// Microsoft Visual C++ generated include file.
-// Used by GameAssist.rc
+// Microsoft Visual C++ 生成的包含文件。
+// 供 GameAssist.rc 使用
 //
 #define IDM_ABOUTBOX                    0x0010
 #define IDD_ABOUTBOX                    100
@@ -9,6 +9,7 @@
 #define IDR_MAINFRAME                   128
 #define IDC_BUTTON1                     1000
 #define BTN_OPENGAME                    1000
+#define IDC_BUTTON2                     1001
 
 // Next default values for new objects
 // 
@@ -16,7 +17,7 @@
 #ifndef APSTUDIO_READONLY_SYMBOLS
 #define _APS_NEXT_RESOURCE_VALUE        129
 #define _APS_NEXT_COMMAND_VALUE         32771
-#define _APS_NEXT_CONTROL_VALUE         1001
+#define _APS_NEXT_CONTROL_VALUE         1002
 #define _APS_NEXT_SYMED_VALUE           101
 #endif
 #endif