选择另一个窗口时通知 CDialog-Window
我有一个基于对话框的 MFC 工具,当我单击它时,它应该在消息框中显示另一个应用程序窗口的标题。 我的问题是 WM_KILLFOCUS 在这里不起作用。也许我做错了。 我执行以下操作:
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
...
...
void CMyDlg::OnKillFocus( CWnd* pNewWnd )
{
CDialog::OnKillFocus(pNewWnd);
if(m_bSelectorModeActive)
{
HWND hwnd(GetForegroundWindow());
TCHAR buf[512];
::GetWindowText(hwnd, buf, 512);
MessageBox(buf);
}
}
知道出了什么问题吗?
I got a dialog-based MFC-Tool that is supposed to show the title of a window of another application in a messagebox when i click on it.
My Problem is that the WM_KILLFOCUS doesn't work here. Maybe I'm doing it wrong.
I do the following:
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
...
...
void CMyDlg::OnKillFocus( CWnd* pNewWnd )
{
CDialog::OnKillFocus(pNewWnd);
if(m_bSelectorModeActive)
{
HWND hwnd(GetForegroundWindow());
TCHAR buf[512];
::GetWindowText(hwnd, buf, 512);
MessageBox(buf);
}
}
Any idea what's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我的猜测
替换 HWND hwnd(GetForegroundWindow());与 GetActiveWindow(void) 。
This is my guess
Replace HWND hwnd(GetForegroundWindow()); with GetActiveWindow(void) .
我解决了,谢谢你的努力。
是的,我确实使用 CStrings,这只是我所做的更复杂的事情的一个小例子。我的问题不是函数本身,而是事件 WM_KILLFOCUS 似乎不起作用。
也许我在这里说得不够清楚,抱歉。
WM_ACTIVATE 满足我的需要。当焦点被设置和/或丢失时,它会通知我的对话框。
I solved it, thanks for your efforts.
Yes, i do use CStrings, this was just a little example of a bit more complex thing i do. My problem was not the function itself but the event WM_KILLFOCUS that didn't seem to work.
Maybe I was not clear enough here, sorry.
WM_ACTIVATE does what i need. It notifies my dialog when the focus is set and/or lost.
您显示的代码甚至不应该编译。 MFC 提供的
GetForegroundWindow
函数不返回HWND
,因此您无法使用其返回值来初始化hwnd
变量。如果您想获取
HWND
,则需要从 Windows API 调用GetForegroundWindow
,方法是使用::
转义调用,就像您所做的那样对于GetWindowText
。因此,只需按如下方式重写您的代码:除此之外,在查看您的代码时,人们会奇怪您似乎忽略了面向对象的 MFC,而如此谦虚地尝试引入 Windows API。您不需要直接使用窗口句柄。有人可能会说,使用 MFC 最令人信服的理由是它的 CString 类。您没有理由再需要处理
TCHAR
数组了。我可能会这样写:The code you've shown shouldn't even compile. The
GetForegroundWindow
function provided by MFC doesn't return anHWND
, so you can't initialize thehwnd
variable using its return value.If you want to get an
HWND
, you need to callGetForegroundWindow
from the Windows API by escaping the call with::
, just like you did forGetWindowText
. So simply rewrite your code as follows:Beyond that, in looking at your code, one wonders that you seem to be ignoring the object-orientation MFC so humbly attempts to bring to the Windows API. You shouldn't need to work directly with window handles. And one could argue that the most compelling reason to use MFC is its
CString
class. There's no reason you should have to deal with an array ofTCHAR
s anymore. I might write this instead: