1

我正在构建 MFC 应用程序,其中有 CDialog 和从 CStatic 派生的子控件。

我想接收 CStatic 控制的鼠标事件,所以我将"Notify"它设置为 true。现在我可以直接通过消息映射接收消息事件MyStatic


class CMyStatic : public CStatic
{
afx_msg void OnLButtonDown(UINT nFlags, CPoint point); // Gets invoked
DECLARE_MESSAGE_MAP()
}

问题是从现在开始,当鼠标悬停在MyStatic孩子上时,父 CDialog 不会接收鼠标事件。我可以手动发送它们,MyStatic但有什么方法可以让它们自动通过?并且仍然能够在MyStatic使用消息映射时接收它们?

4

2 回答 2

5

No, you're misunderstanding. SS_NOTIFY does not cause your CStatic to receive messages, it causes the CStatic to relay messages back to the parent. So "Now I am able to receive message events through message map directly in MyStatic" is based on a false premise.

This is an important point to make because a control can only receive messages for itself, not for its children, unless those children 'play along' by relaying messages or by doing other tricks (global hooking, ...) ; but those cases are the exception, the 'normal' way is that each window receives messages for itself.

So that answers your question: no, there is no way to let both a parent (your CDialog) and a child (your CStatic) receive mouse events without doing so 'manually' or getting involved in tedious message routing hacks. So in your case, what you need to do is GetParent()->SendMessage(WM_LBUTTONDOWN, ...) etc, manually recreating the WPARAM and LPARAM values; or calling OnLButtonDown directly on the CDialog: GetParent()->OnLButtonDown(...). You'll have to think about what mouse coordinates you pass back to CDialog though, you may need to translate them to the CDialog's client coordinates.

于 2010-01-18T12:37:34.703 回答
1

如果您调用基本 OnLButtonDown 处理程序,则应将消息发送到父级。

CMyStatic::OnLButtonDown(UINT nFlags, CPoint point)
{
    CStatic::OnLButtonDown(nFlags, point);

    // custom handling
}
于 2010-01-19T18:06:50.143 回答