Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A border for the Frame window

0.00/5 (No votes)
17 Jul 2002 1  
How to draw/redraw a rectangle along the window rect, when the window is activated or deactivated.

Sample Image - ScreenShot.jpg

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