Solved

Handling message of controls on dialog boxes

Posted on 2001-09-04
10
214 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No, override the on paint to paint the button.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Author Comment

by:rsanwatsarkar
Comment Utility
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
Comment Utility
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
Comment Utility
Don't forget m_bMouseOver = FALSE on constructor.
0
 
LVL 3

Author Comment

by:rsanwatsarkar
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 166
ODBC Connection Logging, ADO.NET 6 49
has22 challenge 11 71
sum28 challenge 31 95
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

12 Experts available now in Live!

Get 1:1 Help Now