Solved

CStatic background color

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

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