A border for the Frame window





5.00/5 (1 vote)
Jul 18, 2002
1 min read

72240

917
How to draw/redraw a rectangle along the window rect, when the window is activated or deactivated.
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
Create a private member variable
bool m_bActivate
in the view-
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); }
-
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)); }
-
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! :-)