65.9K
CodeProject 正在变化。 阅读更多。
Home

框架窗口的边框

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2002年7月18日

1分钟阅读

viewsIcon

72871

downloadIcon

917

如何在窗口激活或停用时绘制/重绘沿着窗口矩形的矩形。

Sample Image - ScreenShot.jpg

引言

在一个项目中,我需要在一个 SDI 应用程序中切换多个框架窗口。在此操作期间,我想为获得焦点的框架提供一个边界矩形,这样我就不会很难理解哪个框架窗口获得了焦点。 此外,失去焦点的框架应该被重新绘制以看起来正常。

步骤如下

  1. 在视图中创建一个私有成员变量 bool m_bActivate

  2. 在视图中创建以下私有成员函数

    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. 重写视图的 OnActivateView 并添加以下代码

    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. 重写视图的 OnDraw 并添加以下代码

    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));
    }

就这样了。编译并生成可执行文件。 启动同一应用程序的多个 exe 文件(如图所示)。 尝试在这些或其他正在运行的应用程序之间切换。 只要我们的一个窗口获得焦点,它就会有一个红色的边界矩形,而之前有焦点的窗口将被重新绘制以失去其矩形。 还有一个小技巧。 为了重绘矩形,我用白色画笔在红色矩形上绘制一个矩形。 也许有办法反转颜色。 欢迎提出任何建议。 谢谢!:-)

© . All rights reserved.