Click here to Skip to main content
15,879,348 members
Articles / Desktop Programming / MFC
Article

A border for the Frame window

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
17 Jul 20021 min read 71.8K   916   23   5
How to draw/redraw a rectangle along the window rect, when the window is activated or deactivated.

Image 1

Introduction

In one of my projects, I needed to switch between the many frame-windows that I had in an SDI application. During this operation, I wanted to give a bounding rectangle to the frame that gained focus, so that I wont have much difficulty in understanding which among the many frame windows gained focus. Also, the frame that lost the focus should be repainted to look normal.

Steps to follow

  1. Create a private member variable bool m_bActivate in the view

  2. Create the following private member function in the view as follows:

    void CSDIBorderView::DrawFrameBorder(HWND hWnd,COLORREF refColor)
    {
        RECT stRect;
    
        // Get the coordinates of the window on the screen
        ::GetWindowRect(hWnd, &stRect);
    
        // Get a handle to the window's device context
        HDC hDC = ::GetWindowDC(hWnd);
    
        HPEN hPen;
        hPen = CreatePen(PS_INSIDEFRAME, 2* GetSystemMetrics(SM_CXBORDER), 
            refColor);
    
        // Draw the rectangle around the window
        HPEN   hOldPen   = (HPEN)SelectObject(hDC, hPen);
        HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, 
            GetStockObject(NULL_BRUSH));
    
        Rectangle(hDC, 0, 0, (stRect.right - stRect.left), 
            (stRect.bottom - stRect.top));
    
        //Give the window its device context back, and destroy our pen
        ::ReleaseDC(hWnd, hDC);
    
        SelectObject(hDC, hOldPen);
        SelectObject(hDC, hOldBrush);
    
        DeleteObject(hPen);
        DeleteObject(hDC);
    }
  3. Override the view's OnActivateView and add the following code:

    CWnd* pWnd = GetParent();
    if(!bActivate)
    {
        m_bActivate = FALSE;
        DrawFrameBorder(pWnd->m_hWnd,RGB(255,255,255));
    }
    else
    {
        m_bActivate = TRUE;
        DrawFrameBorder(pWnd->m_hWnd,RGB(255, 0, 0));
    }
  4. Override the view's OnDraw and add the following code:

    CWnd* pWnd = GetParent();
    if(pWnd)
    {
        if(m_bActivate)
            DrawFrameBorder(pWnd->m_hWnd,RGB(255, 0, 0));
        else
            DrawFrameBorder(pWnd->m_hWnd,RGB(255,255,255));
    }

That's all. Compile and generate the executable. Spawn multiple exes of the same application (as in the figure). Try switching between these or other applications which are running. As soon as one of our window gains focus, it will be having a red bounding rectangle, and the one which had focus, will be redrawn in order to loose its rectangle. There is a little trick though. To redraw the rectangle, I draw a rectangle with a white brush on top of the red rectangle. There could be some way to invert the color. Any suggestions are welcome. Thanks! :-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generala border width Pin
yesong18-Jul-02 15:06
yesong18-Jul-02 15:06 
GeneralRe: a border width Pin
Dave Matrix18-Jul-02 19:35
Dave Matrix18-Jul-02 19:35 
GeneralRe: a border width Pin
yesong18-Jul-02 21:07
yesong18-Jul-02 21:07 
GeneralRe: a border width Pin
Dave Matrix18-Jul-02 21:14
Dave Matrix18-Jul-02 21:14 
GeneralRe: a border width Pin
Tim Ranker19-Jul-02 3:08
Tim Ranker19-Jul-02 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.