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;
::GetWindowRect(hWnd, &stRect);
HDC hDC = ::GetWindowDC(hWnd);
HPEN hPen;
hPen = CreatePen(PS_INSIDEFRAME, 2* GetSystemMetrics(SM_CXBORDER),
refColor);
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));
::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! :-)