Solved

Handling message of controls on dialog boxes

Posted on 2001-09-04
10
225 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 75
Looking for a specific application/software 2 104
MaxSpan challenge 9 80
Change to event 1 100
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now