通过SetForegroundWindow移到前台的窗口并没有输入焦点,怎样才能真正激活

GameWeaverDummy 2005-05-09 08:51:09
我发现通过上面那个函数移到前台的窗口不是真正激活的(标题栏灰色的),且该窗口没有输入焦点,请问怎样才能让后台的进程移到前台且拥有输入焦点?
...全文
463 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
anlywei 2005-05-10
  • 打赏
  • 举报
回复
学习
UDX协议 2005-05-10
  • 打赏
  • 举报
回复
怎么乱成这样了。晕
UDX协议 2005-05-10
  • 打赏
  • 举报
回复
HWND hwndForeground = GetForegroundWindow();
if(hwndForeground )
{
DWORD dwThreadIdAttachTo = GetWindowThreadProcessId(hwndForeground, NULL);
DWORD dwThreadIdAttachToB = GetWindowThreadProcessId(g_hMainWnd, NULL);
if(dwThreadIdAttachTo != dwThreadIdAttachToB)
{
AttachThreadInput(dwThreadIdAttachToB, dwThreadIdAttachTo, TRUE);
BringWindowToTop(g_hMainWnd);
SetForegroundWindow(g_hMainWnd);
SetActiveWindow(g_hMainWnd);
GetWindowThreadProcessId(g_hMainWnd, NULL);

AttachThreadInput(dwThreadIdAttachToB, dwThreadIdAttachTo, FALSE);
}
}
当前输入焦点是区分进程的。setforwindow只对当前进程有效。
horisly 2005-05-09
  • 打赏
  • 举报
回复
你点击按钮后,窗口就会隐藏ShowWindow(hwnd,SW_HIDE); 然后Sleep 5秒。再以不激活形式showwindow ==>ShowWindow(hwnd,SW_SHOWNOACTIVATE);然后通过 ::SetForegroundWindow((HWND)hwnd); 激活窗口
horisly 2005-05-09
  • 打赏
  • 举报
回复
这是我得测试程序:

#include<windows.h>

HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
BOOL CALLBACK MyEnumWindowsProc( HWND hwnd, LPARAM lParam );

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){
static TCHAR strApp[] = "WIN32";
static TCHAR strCaption[] = "WIN32 Programer";
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.lpszClassName = strApp;
wc.lpszMenuName = NULL;

if(!RegisterClass(&wc))
return FALSE;
HWND hWnd = CreateWindow(strApp,strCaption,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT
,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
if(!hWnd)
return FALSE;
hInst = hInstance;
ShowWindow(hWnd,nShowCmd);
UpdateWindow(hWnd);

while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg); //WM_KEYDOWN , WM_KEYUP 被翻译为 WM_CHAR
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam){
static HWND HbtWnd;
DWORD dwID ;
switch(uMsg){
case WM_CREATE:
HbtWnd = CreateWindow(TEXT("button"),"EnumWindow",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,0,0,100,30,hWnd,NULL,hInst,NULL);
return 0;
case WM_COMMAND:
if((HWND)lParam == HbtWnd){
GetWindowThreadProcessId(hWnd, &dwID);
EnumWindows((WNDENUMPROC)MyEnumWindowsProc, (LPARAM)dwID);
return 0;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}


BOOL CALLBACK MyEnumWindowsProc( HWND hwnd, LPARAM lParam )
{
DWORD dwID ;
GetWindowThreadProcessId(hwnd, &dwID);
if(dwID == (DWORD)lParam)
{
MessageBox(NULL,"SetForegroundWindow","TIPS",MB_OK);
ShowWindow(hwnd,SW_HIDE);
Sleep(5000);
ShowWindow(hwnd,SW_SHOWNOACTIVATE);
::SetForegroundWindow((HWND)hwnd);
return FALSE;
}

return TRUE ;
}

horisly 2005-05-09
  • 打赏
  • 举报
回复
我调试时发现::SetForegroundWindow((HWND)hwnd);这一句执行了好几次。为什么会这样?
我想让它只执行一次,怎么办?

===》》
如果EnumWindowsProc返回TRUE的话,它会继续枚举的,返回FALSE的话就停止枚举。
==========================
添加return FALSE ;后,
::SetForegroundWindow((HWND)hwnd);这一句不起作用了:后台运行的进程没有显示到前台来
====>>
可以啊,我试过行的
GameWeaverDummy 2005-05-09
  • 打赏
  • 举报
回复
csdn没救了
这么久才几个人回复
GameWeaverDummy 2005-05-09
  • 打赏
  • 举报
回复
up
GameWeaverDummy 2005-05-09
  • 打赏
  • 举报
回复
添加return FALSE ;后,
::SetForegroundWindow((HWND)hwnd);这一句不起作用了:后台运行的进程没有显示到前台来
不信你自己试试看
vcmute 2005-05-09
  • 打赏
  • 举报
回复
BOOL CALLBACK MyEnumWindowsProc( HWND hwnd, LPARAM lParam )
{
DWORD dwID ;
GetWindowThreadProcessId(hwnd, &dwID);
if(dwID == (DWORD)lParam)
{
::SetForegroundWindow((HWND)hwnd);
return FALSE ;
}

return TRUE ;
}
GameWeaverDummy 2005-05-09
  • 打赏
  • 举报
回复
大家帮我看看这段代码好吗?
BOOL CALLBACK MyEnumWindowsProc( HWND hwnd, LPARAM lParam )
{
DWORD dwID ;
GetWindowThreadProcessId(hwnd, &dwID);
if(dwID == (DWORD)lParam)
{
::SetForegroundWindow((HWND)hwnd);
}

return TRUE ;
}

void CXX::OnLbnDblclkListGet()
{
。。。。。。
EnumWindows((WNDENUMPROC)MyEnumWindowsProc, (LPARAM)dwID);
}

我调试时发现::SetForegroundWindow((HWND)hwnd);这一句执行了好几次。为什么会这样?
我想让它只执行一次,怎么办?
jerry 2005-05-09
  • 打赏
  • 举报
回复
The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. The system assigns a slightly higher priority to the thread that created the foreground window than it does to other threads.

根据这段函数说明, 应该是有的,可能你当时在做什么,就焦点又跑掉了
GameWeaverDummy 2005-05-09
  • 打赏
  • 举报
回复
up

16,472

社区成员

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

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

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