MFC 将指定的窗口激活并显示在桌面最前端,为什么我的没效果?

cao789632145 2015-07-29 02:12:22
无论我的指定窗口是在正常显示的,还是最小化,或是最小化在托盘,都不行
我的代码,如下:
typedef void (WINAPI *PROCSWITCHTOTHISWINDOW) (HWND, BOOL);
PROCSWITCHTOTHISWINDOW SwitchToThisWindow;
HMODULE hUser32 = GetModuleHandle("user32");
SwitchToThisWindow = ( PROCSWITCHTOTHISWINDOW)
GetProcAddress(hUser32, "SwitchToThisWindow");
SwitchToThisWindow(gethwnd, TRUE);
gethwnd就是我指定的那个应用程序的窗口句柄
倘若,我将SwitchToThisWindow(gethwnd, TRUE)的TRUE改为FALSE,桌面上只有指定的应用程序窗口和我自己写的程序窗口,那么最前端界面就会在这两个窗口间切换,相当于AIT+Tab键

另外,
::SetWindowPos(gethwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE |SWP_NOSIZE);

::SetForegroundWindow(gethwnd);
::SetActiveWindow(gethwnd);
::SetFocus(gethwnd);

我都试过了,还是没效果
...全文
1539 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
/*HANDLE m_hMutex=CreateMutex(NULL,FALSE,"ES TEST.exe");//限制只允许打开一个进程
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
ExitProcess(0);
return FALSE;
}*/
baoyz 2015-07-30
  • 打赏
  • 举报
回复
这样找窗口句柄。
HWND SearchWindow(CString strWinName)
{
	//获得桌面窗口
	CWnd* pDesktopWnd = CWnd::GetDesktopWindow();
	
	//获得第一个子窗口
	CWnd* pWnd = pDesktopWnd->GetWindow(GW_CHILD);
	CString strClassName = _T("");
	CString strWindowText= _T("");
	strWinName.MakeUpper();
	while (pWnd != NULL) 
	{		
		//获得窗口标题
		::GetWindowText(pWnd->GetSafeHwnd(), strWindowText.GetBuffer(256), 256);
		strWindowText.ReleaseBuffer();
		strWindowText.MakeUpper();
		if (strWindowText.Find(strWinName) != -1)
		{
			//获得窗口类名
			::GetClassName(pWnd->GetSafeHwnd(), strClassName.GetBuffer(256), 256);
			strClassName.ReleaseBuffer();
			return pWnd->GetSafeHwnd();
		}
		//继续下一个子窗口 
		pWnd = pWnd->GetWindow(GW_HWNDNEXT);
	}
	return 0;
}
baoyz 2015-07-30
  • 打赏
  • 举报
回复
试了一下你得到的gethwnd是个错误的值。所以肯定不会激活窗口。
笨笨仔 2015-07-29
  • 打赏
  • 举报
回复
拜托用下图方法将你的程序放在里面,这样看着太累了
cao789632145 2015-07-29
  • 打赏
  • 举报
回复
引用 2 楼 WindsonZhL 的回复:
您的具体情况和代码不明,不好肯定的回答。 关于那几个方法,SetFocus是用于控件设置键盘焦点的,SetActiveWindow是用于子窗口的。 SetForegroundWindow可以实现要求的效果,但只用于主窗口。 SetWindowPos应该是适用的,第二个参数应传HWND_TOP,HWND_TOPMOST是永久置顶。 或者直接调用BringWindowToTop。
我可以贴出所有代码,下面这个函数,找到YodaoDict.exe的进程ID,然后通过GetHwndByProcessId去获取句柄 int CSendMessageDlg::Iswxrunning() //判断YodaoDict.exe是否在运行 { int flag=0; HANDLE hsnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if(INVALID_HANDLE_VALUE==hsnap) { //cout<<"sorry create snap of the processes failed!"<<endl; return 2; } else { PROCESSENTRY32 pe; pe.dwSize=sizeof(PROCESSENTRY32); int b=::Process32First(hsnap,&pe); while(b) { //cout<<"进程名:"<<pe.szExeFile<<" 进程ID:"<<pe.th32ProcessID<<endl; // if (strcmp(pe.szExeFile,"WxBox.exe")==0 || strcmp(pe.szExeFile,"WeChat.exe")==0) if (strcmp(pe.szExeFile,"YodaoDict.exe")==0) { flag=1; //SetForegroundWindow(); gethwnd=GetHwndByProcessId(pe.th32ParentProcessID); //通过进程ID获取句柄 break; } b=::Process32Next(hsnap,&pe); } } ::CloseHandle(hsnap); return flag; } GetHwndByProcessId的函数如下 typedef struct { HWND hWnd; DWORD dwPid; }WNDINFO; BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) { WNDINFO* pInfo = (WNDINFO*)lParam; DWORD dwProcessId = 0; GetWindowThreadProcessId(hWnd, &dwProcessId); if(dwProcessId == pInfo->dwPid) { pInfo->hWnd = hWnd; return FALSE; } return TRUE; } HWND GetHwndByProcessId(DWORD dwProcessId) { WNDINFO info = {0}; info.hWnd = NULL; info.dwPid = dwProcessId; EnumWindows(EnumWindowsProc, (LPARAM)&info); return info.hWnd; }
cao789632145 2015-07-29
  • 打赏
  • 举报
回复
引用 1 楼 schlafenhamster 的回复:
备注 This function is typically called to maintain window z-ordering. Although you can access this function by using LoadLibrary and GetProcAddress combined in Windows versions prior to Windows XP, the function is not accessible using the standard Include file and library linkage. The header files included in Windows XP Service Pack 1 (SP1) and Windows Server 2003 document this function and make it accessible using the appropriate Include file and library linkage. However, this function is deprecated and not intended for general use. It is recommended that you do not use it in new programs because it might be altered or unavailable in subsequent versions of Windows.
我的电脑是win 7 企业版,是非常平常的版本,怎么会不兼容呢?
  • 打赏
  • 举报
回复
您的具体情况和代码不明,不好肯定的回答。 关于那几个方法,SetFocus是用于控件设置键盘焦点的,SetActiveWindow是用于子窗口的。 SetForegroundWindow可以实现要求的效果,但只用于主窗口。 SetWindowPos应该是适用的,第二个参数应传HWND_TOP,HWND_TOPMOST是永久置顶。 或者直接调用BringWindowToTop。
schlafenhamster 2015-07-29
  • 打赏
  • 举报
回复
备注 This function is typically called to maintain window z-ordering. Although you can access this function by using LoadLibrary and GetProcAddress combined in Windows versions prior to Windows XP, the function is not accessible using the standard Include file and library linkage. The header files included in Windows XP Service Pack 1 (SP1) and Windows Server 2003 document this function and make it accessible using the appropriate Include file and library linkage. However, this function is deprecated and not intended for general use. It is recommended that you do not use it in new programs because it might be altered or unavailable in subsequent versions of Windows.

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧