Solved

Handling message of controls on dialog boxes

Posted on 2001-09-04
10
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 92
conditional code and condition difference 9 87
scoreUp challenge 14 74
Sed question 2 139
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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