Solved

Handling message of controls on dialog boxes

Posted on 2001-09-04
10
241 Views
Last Modified: 2013-11-20
Hi
I have a dialog box on which there is a button control. I wish to handle the mouse move message on the button.
That is when the user moves the mouse over the button on the dialog box, I wish to change the background & foreground color of the button. How do I achieve this?
The environment is VC++ 6.0, MFC and the application is a dialog based application.

Thanks
Rashmin
0
Comment
Question by:rsanwatsarkar
  • 6
  • 4
10 Comments
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6453309
Derive a class from CButton,let's say CBtn. Go to class wizard and override WM_MOUSEMOVE event for CBtn class and do your drawing there. Map a control of CBtn type over your button.
0
 
LVL 3

Author Comment

by:rsanwatsarkar
ID: 6453334
Hi
Thanks for the quick answer. How do I map the control of CBtn over my button. Can u please elaborate it a bit

Thanks
Rashmin
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6453411
Go to class wizzard, Member Variables tab.
At class name select the name o the dialog class that contains the button.
At controls id's select the id of your button.
Press Add variable button on the right.
At category select control.
At type select CBtn.
Enter a name.
Press Ok button.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:rsanwatsarkar
ID: 6453576
Hi
I tried adding the member variable, specifying the type as CBtn. In the CBtn class, I have overridden the OnCtlColor function and changed the background color using the
     pDC->SetBkColor( RGB(255,0,0) );
function
But the background of the button is still noy changing. Do I need to do anyting specific in my dialog class which is hosting the button?
Thanks
Rashmin
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6453610
No, override the on paint to paint the button.
0
 
LVL 3

Author Comment

by:rsanwatsarkar
ID: 6453642
Thanks a lot for the quick reply.

I tried the following code in the OnPaint of CBtn

     CPaintDC dc(this); // device context for painting

     if(m_bMouseOver )
     {
          dc.SetBkColor( RGB(255,0,0) );
          m_bMouseOver = FALSE;
     }
     else
          dc.SetBkColor( RGB(0,255,0) );


The m_bMouseOver is set to true in the OnMouseMove implementaion of CBtn

But its still not working.

Can u please tell what is the problem?
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6453777
void CBtn::OnMouseMove(UINT nFlags, CPoint point)
{
     m_bMouseOver = TRUE;
     Invalidate();
     TRACE("mouse over \n");
}

void CBtn::OnPaint()
{
     CPaintDC dc(this); // device context for painting
     CBrush br;
     TRACE("painting \n");
     RECT rc;
     GetClientRect( &rc );
     if(m_bMouseOver )
     {
          TRACE("mouse true \n");
          br.CreateSolidBrush( RGB ( 255,0,0 ) );
          m_bMouseOver = FALSE;
     }
     else
     {
          TRACE("mouse false \n");
          br.CreateSolidBrush( RGB ( 0,255,0 ) );
     }
          dc.FillRect( &rc, &br );
     
}

I recomand you to override the OnMouseMove on your Dialog class and redraw the button.

If u want more, set the button style BS_OWNERDRAW and override the CButton::DrawItem() function.
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6453791
Don't forget m_bMouseOver = FALSE on constructor.
0
 
LVL 3

Author Comment

by:rsanwatsarkar
ID: 6454195
Hey thanks a lot for the help. I tried that and the color changes to red when the mouse moves over the button but it stays red when the mouse moves away from the button. It does not turn back green again. Why is it so?
Why do I need to overridethe OnMouseMove of dialog  to redraw the button?
Rashmin
0
 
LVL 2

Accepted Solution

by:
BogdyPtr earned 50 total points
ID: 6454209
So the color change back to green. The button dosen't know when the mouse is leaving. U can use a timer to periodicaly check if the mouse is on the button if u want.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven archtype selection in eclipse 1 57
MaxSpan challenge 9 106
scoreUp challenge 14 64
moto 4g phone enable finger print 4 28
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question