-
MouseOver
Let me just say this first. I am not very good with C++.
Ok, here's what I want to do. I want a label on my form (I am using MFC) to change when the mouse moves over each button on my form. Kinda like a tooltip, but in a static place. I know how to change text in a label, but I don't know how to put code in a mouseover or mousemove event. Can anyone tell me how to do this?
Thanks for any help. :)
-
Subclass the buttons and catch the WM_MOUSEMOVE message. Subclassing in MFC is quite confusing, you might want to use the traditional way.
If you don't understand what I said you need to learn more about the basics of windows programming.
-
I understand what you said, I just don't know how to do it. You have to subclass for something as simple as a mouse over? I think I'll just stick to VB and say f**k you to the people that don't have the VB runtimes. hehe.
-
Look, the button area belongs to the button, not to the parent window. Therefore only the button can catch the mousemove messages.
The reason why VB can have a mouseover event might be either that it subclasses the button itself or (more likely) that even simple buttons in VB are evil slow ActiveX controls that notify the parent of such useless things.
It's really not hard to subclass something.
Here's untested code to subclass a button in MFC. Just put it somewhere into your class that represents the parent of the button
Code:
SubclassWindow(ctlButton.GetSafeHwnd());
But if you already use CButton to create the button there is a better (more MFC-style) way: derive a class.
Code:
class CMyButt : public CButton
{
DECLARE_DYNAMIC(CMyButt);
DECLARE_MESSAGE_MAP();
public:
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
};
IMPLEMENT_DYNAMIC(CMyButt, CButton);
BEGIN_MESSAGE_MAP(CMyButt, CButton)
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
// code for OnMouseMove
Then in your container do
CMyButt m_ctlButt;
instead of
CButton m_ctlButt;
and you're all settled.
In case it's a dialog you can't do that of course. In this case you use the DDX mechanism:
Code:
class CMyDialog : public CDialog {
// bla bla bla, lot's of wizard code
CMyButt m_ctlButt;
};
// Add this line to the DoDataExchange function:
DDX_Control(pDX, IDC_OFYOURBUTT, m_ctlButt);
It's damn easy once you know how.
-
Why not just use a CToolTipCtrl?
Code:
// Add a member variable for the tooltip control.
class ParentWnd
{
...
CToolTipCtrl tooltip;
}
// Do this in OnCreate().
tooltip.Create(pParentWnd, IDC_SOMEID, TTS_NOPREFIX);
// Do this in OnInitDialog() or something close.
tooltip.AddTool(GetDlgItem(IDC_A_BUTTON), TEXT("It's a button."));
tooltip.AddTool(GetDlgItem(IDC_A_BUTTON2), TEXT("It's a button2."));
// Do this in a OnMouseMove() for the parent not each individual button.
LPMSG pMsg = ::GetCurrentMessage();
tooltip.RelayEvent(pMsg);