4442|8

65

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

CE下如何获取其它进程中焦点所处窗口的句柄?在线等 [复制链接]

那位大侠能高诉我CE下 有什么办法能获取另外一个进程中焦点所处窗口的句柄?CE下好象不支持AttachThreadInput函数,还有其它的办法吗?我现在用的是CE6和VS2005

最新回复

How to use GetFocus across process boundaries? By Alexander Shargin, August 20, 2004. Question GetFocus is a very useful API function used to find window which has keyboard focus. However, this function has a limitation - focused window must be created by the calling thread or GetFocus will return NULL. On desktop Windows it was possible to walkaround this problem using AttachThreadInput function. However, on CE-based devices this function is not available. So, how can I use GetFocus to find focused window created by another process? Answer To make GetFocus work, you need to call it from a thread which created focused window. Usually (though not quite always) this is the same thread which created top-level foreground window (obtained with GetForegroundWindow). The trick is to subclass this window and the send it a special message which will be processed by your window proc. Since your window proc will be called by a thread in another process, you can just call GetFocus and return HWND as LRESULT to your own process. Note that on desktop this trick will not work since each process has its own address space and subclassing between process boundaries is not possible. But on CE it does work. Sample code I've written a small function, GetFocusEx, which uses the trick described above. You can use it in your projects the same way you use GetFocus. GetFocusWindowProc is a window proc assigned to foreground window. If it gets special message (registered with RegisterWindowMessage function) it calls GetFocus and returns obtained HWND as LRESULT. Otherwise it just forwards messages to original window proc using CallWindowProc function. static WNDPROC g_pOldWndProc = NULL; static UINT g_uGetFocusMessage = RegisterWindowMessage(_T("SpecialGetFocusMessage")); static LRESULT WINAPI GetFocusWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {         if(uMsg == g_uGetFocusMessage)         {                 return (LRESULT)GetFocus();         }         else         {                 return CallWindowProc(g_pOldWndProc, hWnd, uMsg, wParam, lParam);         } } HWND GetFocusEx() {         HWND hWnd = GetForegroundWindow();         if(!IsWindow(hWnd))                 return NULL;         g_pOldWndProc = (WNDPROC)GetWindowLong(hWnd, GWL_WNDPROC);         SetWindowLong(hWnd, GWL_WNDPROC, (LONG)GetFocusWindowProc);         HWND hResult = (HWND)SendMessage(hWnd, g_uGetFocusMessage, 0, 0);         SetWindowLong(hWnd, GWL_WNDPROC, (LONG)g_pOldWndProc);         g_pOldWndProc = NULL;         return hResult; }   详情 回复 发表于 2009-3-4 18:25
点赞 关注

回复
举报

64

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
FindWindow("窗口的class","窗口的标题"),这样就可以找到窗口的句柄了。其中,窗口的class和窗口的标题不需要两个都有值。
 
 

回复

72

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
to: wyl1220(国际米兰)

   另外一个进程的窗口包括所有子窗口句柄 都可以获取到啊,但是我就是想获取到焦点所处的那个窗口句柄啊。
 
 
 

回复

69

帖子

0

TA的资源

一粒金砂(初级)

4
 
有办法让GetFocus在当前进程中获取到另外一个进程中焦点所处的窗口句柄吗?
 
 
 

回复

73

帖子

0

TA的资源

一粒金砂(初级)

5
 
这个问题我也遇到过.比如说导航软件是第三方提供的,正在运行,如果按退出键(硬键盘),想把它退出,但是不知道怎么得到它的窗口句柄.用FindWindow函数,一般是事先已经知道运用程序窗口的名称,如果名称也不知道怎么办?
 
 
 

回复

87

帖子

0

TA的资源

一粒金砂(初级)

6
 
to YOYO_PAOPAO :

   如果你们的第三方导航软件内部实现了对Esc按键的处理,直接在你的进程中调用keybd_event()就OK了,如果没实现 估计也会比较麻烦啊,呵呵
 
 
 

回复

72

帖子

0

TA的资源

一粒金砂(初级)

7
 
难道无解吗?还是CE 不支持这样的操作啊/???
 
 
 

回复

95

帖子

0

TA的资源

一粒金砂(初级)

8
 
我有看到人家做的导航终端,可以用硬键盘实现应用程序和导航软件的切换.如果仅仅是焦点切换,应该可以实现哦.
static HWND old_hwd;
                                       
                                        HWND current_hwd=::GetForegroundWindow();
                                        if(current_hwd != ((PARENT_WND*)AfxGetApp()->m_pMainWnd)->m_hWnd)
                                                ((PARENT_WND*)AfxGetApp()->m_pMainWnd)->SetForegroundWindow();
                                        else
                                                ::SetForegroundWindow(old_hwd);
                                        old_hwd = current_hwd;
 
 
 

回复

76

帖子

0

TA的资源

一粒金砂(初级)

9
 
How to use GetFocus across process boundaries?
By Alexander Shargin, August 20, 2004.

Question
GetFocus is a very useful API function used to find window which has keyboard focus. However, this function has a limitation - focused window must be created by the calling thread or GetFocus will return NULL. On desktop Windows it was possible to walkaround this problem using AttachThreadInput function. However, on CE-based devices this function is not available. So, how can I use GetFocus to find focused window created by another process?

Answer
To make GetFocus work, you need to call it from a thread which created focused window. Usually (though not quite always) this is the same thread which created top-level foreground window (obtained with GetForegroundWindow). The trick is to subclass this window and the send it a special message which will be processed by your window proc. Since your window proc will be called by a thread in another process, you can just call GetFocus and return HWND as LRESULT to your own process. Note that on desktop this trick will not work since each process has its own address space and subclassing between process boundaries is not possible. But on CE it does work.

Sample code
I've written a small function, GetFocusEx, which uses the trick described above. You can use it in your projects the same way you use GetFocus. GetFocusWindowProc is a window proc assigned to foreground window. If it gets special message (registered with RegisterWindowMessage function) it calls GetFocus and returns obtained HWND as LRESULT. Otherwise it just forwards messages to original window proc using CallWindowProc function.

static WNDPROC g_pOldWndProc = NULL;
static UINT g_uGetFocusMessage = RegisterWindowMessage(_T("SpecialGetFocusMessage"));
static LRESULT WINAPI GetFocusWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
        if(uMsg == g_uGetFocusMessage)
        {
                return (LRESULT)GetFocus();
        }
        else
        {
                return CallWindowProc(g_pOldWndProc, hWnd, uMsg, wParam, lParam);
        }
}

HWND GetFocusEx()
{
        HWND hWnd = GetForegroundWindow();
        if(!IsWindow(hWnd))
                return NULL;

        g_pOldWndProc = (WNDPROC)GetWindowLong(hWnd, GWL_WNDPROC);
        SetWindowLong(hWnd, GWL_WNDPROC, (LONG)GetFocusWindowProc);

        HWND hResult = (HWND)SendMessage(hWnd, g_uGetFocusMessage, 0, 0);

        SetWindowLong(hWnd, GWL_WNDPROC, (LONG)g_pOldWndProc);
        g_pOldWndProc = NULL;

        return hResult;
}

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表