Solved

CStatic background color

Posted on 1997-07-28
3
665 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

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
string initialization in java 11 110
difference between String.subString() and String.subSequence() 6 174
noX challenge 17 92
Message not shown 5 33
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 …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

895 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

14 Experts available now in Live!

Get 1:1 Help Now