Solved

CStatic background color

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adapt this command to show who installed 29 108
unable to delete all specified values regedit 38 408
NotAlone Challenge 20 83
wordcount challenge 11 121
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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