Solved

CStatic background color

Posted on 1997-07-28
3
660 Views
Last Modified: 2013-11-20
I have a CStatic derived member in my Dialog. I want to change the background color when my mouse enters the client area of the CStatic and should be restored to the original color when the mouse leaves the static member.
0
Comment
Question by:Ram072297
3 Comments
 
LVL 8

Expert Comment

by:gelbert
ID: 1302874
Use function OnMouseMove() to find out when mouse is over your static control then change background color by setting new brush in OnCtlColor ( handler of WM_CTLCOLOR ). Do the same way when mouse leave control then reset brush in OnCtlColor to default
0
 

Author Comment

by:Ram072297
ID: 1302875
The OnCtlColor handler is called at the time of the creation of the dialog so the color of the static is getting changed earlier and also the color of all the static members is getting changed. I want the color change for only the static which is under the mouse.
0
 
LVL 3

Accepted Solution

by:
Melange earned 50 total points
ID: 1302876
Change the ID of your static control to something other than IDSTATIC such as IDC_MY_STATIC.
Handling OnCtlColor is correct. This gets called whenever painting is done in your dialog.
It should look something like this:
   Note:  bHasMouse is a bool variable in your dialog class.

CMyDlg::OnCtlColor( ... )
{
    switch(nCtlColor)
    {
        case CTLCOLOR_STATIC:
            if( bHasMouse  &&  pWnd->GetSafeHwnd() == GetDlgItem(IDC_MY_STATIC) )
                return( hHasMouseBrush );
            break;
    }
    return( CDialog::OnCtlColor(pDC, pWnd, nCtlColor) );
}

I'm not sure but you may need to subclass the static control in order to receive mouse messages for that control. Try putting in a handler for OnMouseMove in your dialog. If it never gets hit properly then you will need to subclass it.

Without subclassing:

CMyDlg::OnMouseMove( UINT flags, CPoint pt )
{
    CDialog::OnMouseMove(flags, pt);

    CRect    StaticCtrlRect;
    CWnd    *pStatic;

    pStatic = GetDlgItem(IDC_MY_STATIC);
    pStatic->GetWindowRect(StaticCtrlRect);
    ScreenToClient(StaticCtrlRect);
    if( StaticCtrlRect.PtInRect(pt) )
    {
        if( ! bHasMouse )
        {
            bHasMouse = true;
            pStatic->Invalidate();
        }
    }
    else
    {
        if( bHasMouse )
        {
            bHasMouse = false;
            pStatic->Invalidate();
        }
    }
}


Procedure for subclassing static control:

Add a new class derived from CStatic (such as CMyStatic).
Add a member variable for the static control (using the "control" type not a CString).
In the dialog class header file replace the "CStatic" for the control with "CMyStatic".
Using classwizard add an OnMouseMove handler for the new CMyStatic class.

0

Featured Post

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.

Join & Write a Comment

Suggested Solutions

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

13 Experts available now in Live!

Get 1:1 Help Now