c++ - 如何检测悬停在静态 Win32 控件上?

标签 c++ winapi

我在检测悬停在静态 Win32 控件上时遇到问题。

这不是一个重复的问题,因为它会查找多个静态控件,而不是在编译时只查找单个已知的静态控件句柄。

虽然这可以用另一种语言在几秒钟内完成,但在尝试了几个小时后我变得有点沮丧。希望在这里得到答案。

首先,我创建了一个名为 Label 的类。我在其中创建了一个静态控制窗口。我现在将静态称为标签。

// Create the label's handle.
m_handle = CreateWindowEx(NULL, "static", m_text.c_str(),
    WS_CHILD | SS_LEFT | SS_NOTIFY, m_x, m_y, m_width, m_height,
    m_parentWindow, (HMENU)(UINT_PTR)m_id, m_hInstance, NULL);
if (m_handle == NULL)
    return false;

当鼠标悬停在这个标签上时,应该调用下面的方法:

void Label::invokeOnMouseHover()
{
    if (m_onMouseOver)
        m_onMouseOver();
}

这将调用我的方法:

void lblName_onMouseOver()
{
    MessageBox::show("Hovering!", "My Console",
        MessageBoxButtons::Ok, MessageBoxIcon::Information);
}

这是我从顶层创建它的方式:

Label lblName("This is a label.", 0, 0);
lblName.setVisible(true);
lblName.OnMouseOver(lblName_onMouseOver); 
frm.add(lblName);

承认吧,这个薄层很漂亮。

虽然我的回调对我的 Button 和 Checkbox 控件工作正常,但我注意到静态有点不同。

那么,让我们往下看几个层次:

这是在主窗口的过程中:

case WM_MOUSEMOVE:
{  
    int xPos = LOWORD(lParam);
    int yPos = HIWORD(lParam);

    frm.setMousePos(xPos, yPos);  

    // Get the static's id
    int id = // ?? Which static control id is it out of several?

                // Obtain the control associated with the id.
        X3D::Windows::Control *ctrl = frm.getControls().find(id)->second;
    if (ctrl == NULL)
        return 0;

    // Check if this is a X3D Label control.
    if (typeid(*ctrl) == typeid(X3D::Windows::Label))
    {
        Label *lbl = dynamic_cast<X3D::Windows::Label*>(ctrl);

        int xPos = GET_X_LPARAM(lParam);
        int yPos = GET_Y_LPARAM(lParam);

        if (xPos >= lbl->getX() &&
            yPos >= lbl->getY() &&
            (xPos < (lbl->getX() + lbl->getWidth())) &&
            (yPos < (lbl->getY() + lbl->getHeight())))
        {
            if (lbl != NULL)
                lbl->invokeOnMouseHover();
        }
    }
}
break; 

我在这里要做的是检测检测到的标签 ID,然后调用 Label::invokeOnMouseOver()。

尽管我知道我需要在某些时候使用 TRACKMOUSEEVENT,但它的字段成员“HWND”需要标签的句柄。但我不能轻易说出它是哪个句柄,因为该集合可能包含一个或多个标签。

总的来说,我正在寻找关于如何重组它的建议,或者看看这里是否有一个简单的解决方案。我的猜测是我想多了。

谢谢。

更新:

这是阅读第一个解决方案的第一个答案后的代码更新。虽然这解决了悬停问题,但我在执行时看不到标签的文本。

case WM_MOUSEMOVE:
{  
    int xPos = LOWORD(lParam);
    int yPos = HIWORD(lParam);

    // Get the mouse position
    frm.setMousePos(xPos, yPos);  

    // Check for labels
    X3D::Windows::Control *ctrl = (X3D::Windows::Control*)GetWindowLongPtr(hWnd, GWLP_USERDATA);

    if (ctrl)
    {
        // Check if this is a X3D Label control.
        Label *lbl = dynamic_cast<X3D::Windows::Label*>(ctrl);
        if (lbl)
            lbl->invokeOnMouseHover();

        return CallWindowProc(lbl->getOldProc(), hWnd, msg, wParam, lParam);
    } 
}
break; 

以及控件的创建:

// Create the label's handle.
m_handle = CreateWindowEx(NULL, TEXT("static"), m_text.c_str(),
    WS_CHILD | SS_LEFT | SS_NOTIFY, m_x, m_y, m_width, m_height,
    m_parentWindow, (HMENU)(UINT_PTR)m_id, m_hInstance, NULL);
if (!m_handle)
    return false;

SetWindowLongPtr(m_handle, GWLP_USERDATA, (LONG_PTR)(X3D::Windows::Control*)this);
m_oldProc = (WNDPROC)SetWindowLongPtr(m_handle, GWLP_WNDPROC, (LONG_PTR)&wndProc);

如果是绘画问题,这就是我在 WndProc 中的内容。

    case WM_PAINT:
    {
        hDC = BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);

        return 0;
    }
    break;

更新#2:

替代解决方案解决了该问题。

如果以后有人在使用 SetWindowSubclass() 时遇到未解析的外部符号问题,请记住将以下内容添加到您的项目中或只是#pragma it:

#pragma comment(lib, "comctl32.lib")

最佳答案

WM_MOUSEMOVE 为您提供的唯一标识信息是鼠标移过的HWND(或具有captured the mouseHWND) ).报告的 X/Y 坐标是相对于 HWND 的。这就是您正在寻找的 HWND,因此您无需寻找它,消息会将它提供给您。

如果您需要访问HWND 的控件ID,您可以使用GetDlgCtrlID()。为了那个原因。但请注意,每个 HWND 都有自己的 窗口过程,因此控件 ID 通常仅对通知消息有用,例如 WM_COMMANDWM_NOTIFY ,它们被发送到控件的父窗口(即便如此,这样的通知也带有 child 的 HWND)。

当鼠标移动到特定的 HWND 上时,WM_MOUSEMOVE 被发送到 that HWND 的消息过程仅(或捕获鼠标的 HWND)。听起来您希望它改为发布到控件的 窗口,但事实并非如此。这就是为什么您的 WM_MOUSEMOVE 处理程序没有被调用的原因。您在错误的级别处理消息。您需要准备好在每个控件的基础上处理消息,而不是使用控件自己的消息过程。

通过 SetWindowLongPtr(GWLP_USERDATA) 将 Control 对象的 this 指针存储在其关联的 HWND 本身内会更有效SetWindowSubClass()SetProp(),然后您的消息处理程序可以访问报告消息的 Control* 指针HWND 需要时,例如:

// Create the label's handle.
m_handle = CreateWindowEx(NULL, TEXT("static"), m_text.c_str(),
    WS_CHILD | SS_LEFT | SS_NOTIFY, m_x, m_y, m_width, m_height,
    m_parentWindow, (HMENU)(UINT_PTR)m_id, m_hInstance, NULL);
if (!m_handle)
    return false;
SetWindowLongPtr(m_handle, GWLP_USERDATA, (LONG_PTR)(X3D::Windows::Control*)this);
m_oldproc = (WNDPROC) SetWindowLongPtr(m_handle, GWL_WNDPROC, (LONG_PTR)&MyWndProc);

...

LRESULT CALLBACK MyWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_MOUSEMOVE:
        {
            ...

            X3D::Windows::Control *ctrl = (X3D::Windows::Control*) GetWindowLongPtr(hWnd, GWLP_USERDATA);
            if (ctrl)
            {
                // Check if this is a X3D Label control.
                Label *lbl = dynamic_cast<X3D::Windows::Label*>(ctrl);
                if (lbl)
                    lbl->invokeOnMouseHover();
            }

            break;
        }

        ...
    }

    return CallWindowProc(m_oldproc, hWnd, uMsg, wParam, lParam);
}

或者:

// Create the label's handle.
m_handle = CreateWindowEx(NULL, TEXT("static"), m_text.c_str(),
    WS_CHILD | SS_LEFT | SS_NOTIFY, m_x, m_y, m_width, m_height,
    m_parentWindow, (HMENU)(UINT_PTR)m_id, m_hInstance, NULL);
if (!m_handle)
    return false;
SetWindowSubclass(m_handle, &MySubclassProc, 1, (DWORD_PTR)(X3D::Windows::Control*)this);

...

LRESULT CALLBACK MySubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    switch (uMsg)
    {
        case WM_NCDESTROY:
            RemoveWindowSubclass(hWnd, &MySubclassProc, uIdSubclass);
            break;

        case WM_MOUSEMOVE:
        {
            X3D::Windows::Control *ctrl = (X3D::Windows::Control*) dwRefData;

            // Check if this is a X3D Label control.
            Label *lbl = dynamic_cast<X3D::Windows::Label*>(ctrl);
            if (lbl)
                lbl->invokeOnMouseHover();

            break;
        }

        ...
    }

    return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}

关于c++ - 如何检测悬停在静态 Win32 控件上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51164396/

相关文章:

c++ - 防止编码器错误 - 通过引用返回临时值(悬挂指针)

c++ - msvc 和 gcc 不同的行为

c++ - Youtube 与 WINAPI GetForegroundWindow() 的交互?

c++ - 如何使用 C++ 和 WinAPI 检查 Web 浏览器是否正在运行?

winapi - 错误 RC2104 : undefined keyword or key name: WS_EX_LAYOUTRTL

winapi - 登录用户、LOGON32_LOGON_INTERACTIVE 和 LOGON32_LOGON_NETWORK

C++ 程序以代码 01 退出,未执行任何代码

c++ - 是什么导致代码在 GPU 中运行?

c++ - 由解决方法 : c++ : boost_log : how does compiler choose st or mt 修复

C++ WinAPI 使用 DWM 在自定义窗口框架上显示位图