选择另一个窗口时通知 CDialog-Window

发布于 2024-10-18 00:36:11 字数 484 浏览 3 评论 0原文

我有一个基于对话框的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

客…行舟 2024-10-25 00:36:11

这是我的猜测

替换 HWND hwnd(GetForegroundWindow());与 GetActiveWindow(void) 。

This is my guess

Replace HWND hwnd(GetForegroundWindow()); with GetActiveWindow(void) .

只等公子 2024-10-25 00:36:11

我解决了,谢谢你的努力。

是的,我确实使用 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.

混吃等死 2024-10-25 00:36:11

您显示的代码甚至不应该编译。 MFC 提供的 GetForegroundWindow 函数不返回 HWND,因此您无法使用其返回值来初始化 hwnd 变量。

如果您想获取 HWND,则需要从 Windows API 调用 GetForegroundWindow,方法是使用 :: 转义调用,就像您所做的那样对于GetWindowText。因此,只需按如下方式重写您的代码:

void CMyDlg::OnKillFocus( CWnd* pNewWnd )
{
    CDialog::OnKillFocus(pNewWnd);
    if(m_bSelectorModeActive)
    {
        HWND hwnd(::GetForegroundWindow());
        TCHAR buf[512];
        ::GetWindowText(hwnd, buf, 512);
        MessageBox(buf);
    }
}

除此之外,在查看您的代码时,人们会奇怪您似乎忽略了面向对象的 MFC,而如此谦虚地尝试引入 Windows API。您不需要直接使用窗口句柄。有人可能会说,使用 MFC 最令人信服的理由是它的 CString 类。您没有理由再需要处理 TCHAR 数组了。我可能会这样写:

void CMyDlg::OnKillFocus( CWnd* pNewWnd )
{
    CDialog::OnKillFocus(pNewWnd);
    if(m_bSelectorModeActive)
    {
        CWnd* pForeWnd = GetForegroundWindow();
        CString windowText;
        pForeWnd->GetWindowText(windowText);
        MessageBox(windowText);
    }
}

The code you've shown shouldn't even compile. The GetForegroundWindow function provided by MFC doesn't return an HWND, so you can't initialize the hwnd variable using its return value.

If you want to get an HWND, you need to call GetForegroundWindow from the Windows API by escaping the call with ::, just like you did for GetWindowText. So simply rewrite your code as follows:

void CMyDlg::OnKillFocus( CWnd* pNewWnd )
{
    CDialog::OnKillFocus(pNewWnd);
    if(m_bSelectorModeActive)
    {
        HWND hwnd(::GetForegroundWindow());
        TCHAR buf[512];
        ::GetWindowText(hwnd, buf, 512);
        MessageBox(buf);
    }
}

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 of TCHARs anymore. I might write this instead:

void CMyDlg::OnKillFocus( CWnd* pNewWnd )
{
    CDialog::OnKillFocus(pNewWnd);
    if(m_bSelectorModeActive)
    {
        CWnd* pForeWnd = GetForegroundWindow();
        CString windowText;
        pForeWnd->GetWindowText(windowText);
        MessageBox(windowText);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文