Solved

CStatic background color

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tab names are off by one 5 54
ASCII Non-Printable characters/codes and their HTML equivalents 6 143
Best tools for Backup and restore of Wordpress site 6 58
Window placement 17 95
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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