在 windows 10 上使用 DWM API 的 Aero 字幕标题栏问题

An Aero caption title bar issue using DWM API on the windows 10

为了在标题栏上绘制图标,我参考了 this MSDN article 并使用 DWM API 通过调用 DwmExtendFrameIntoClientArea 创建了我的自定义客户区。

我的代码:

CMainFrame::CMainFrame()
{
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR           gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    BOOL fDwmEnabled = FALSE;
    if (SUCCEEDED(DwmIsCompositionEnabled(&fDwmEnabled)))
        TRACE0("DWM is enabled\n");

    TCHAR szLogoPath[MAX_PATH];
    GetModuleFileName ( GetModuleHandle(NULL), szLogoPath, _countof(szLogoPath)     );
    PathRemoveFileSpec ( szLogoPath );
    PathAppend ( szLogoPath, _T("lena.bmp") );
    m_pLogoImage = m_pLogoImage->FromFile ( CT2CW(szLogoPath) );
    if(NULL == m_pLogoImage)
        TRACE0("load image fail\n");
}

void CMainFrame::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp)
{
    int xFrame = 2; 
    int yFrame = 2; 
    int nTHight = 30; 
    NCCALCSIZE_PARAMS * p; 
    RECT * rc; 
    RECT aRect;
    RECT bRect;
    RECT acRect;
    p = (NCCALCSIZE_PARAMS *)lpncsp; 
    
    CopyRect(&bRect,&p->rgrc[1]); 
    CopyRect(&aRect,&p->rgrc[0]);

    acRect.left = aRect.left + xFrame;
    acRect.top = aRect.top - nTHight;
    acRect.right = aRect.right - xFrame;
    acRect.bottom = aRect.bottom - yFrame;
    CopyRect(&p->rgrc[0],&acRect);
    CopyRect(&p->rgrc[1],&aRect);
    CopyRect(&p->rgrc[2],&bRect);
    CFrameWnd::OnNcCalcSize(TRUE, lpncsp);
}

 LRESULT CMainFrame::OnNcHitTest(CPoint p)
 {
    BOOL dwm_enabled = FALSE;
    if (SUCCEEDED(DwmIsCompositionEnabled(&dwm_enabled)))
    {
        LRESULT result = 0;
        if (!DwmDefWindowProc(m_hWnd, WM_NCHITTEST, 0, MAKELPARAM(p.x, p.y), &result))
            result = HitTestNCA(m_hWnd, p);

        if (result == HTNOWHERE && GetForegroundWindow() != this)
        {
            return HTCAPTION;
        }

        return result;
    }

    return CWnd::OnNcHitTest(p);
 }

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    if(cs.hMenu!=NULL)
    {
    ::DestroyMenu(cs.hMenu);    
        cs.hMenu = NULL ;    
    }
    if( !CFrameWnd::PreCreateWindow(cs) )
        return FALSE;
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs
    cs.style = WS_CAPTION | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_OVERLAPPED| WS_SYSMENU | WS_THICKFRAME;
    cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
    cs.lpszClass = AfxRegisterWndClass(0);

    return TRUE;
}

void CMainFrame::OnActivate(UINT nState,CWnd* pWndOther,BOOL bMinimized )
{
    CFrameWnd::OnActivate(nState,pWndOther,bMinimized);
    BOOL fDwmEnabled = FALSE;
    if (SUCCEEDED(DwmIsCompositionEnabled(&fDwmEnabled)))
    {
        if(nState == WA_ACTIVE )
        {
            MARGINS margins = {-1};
            /*margins.cyTopHeight = 30;
            margins.cxLeftWidth = 0;
            margins.cxRightWidth = 0;
            margins.cyBottomHeight = 0;*/
            HRESULT hr = DwmExtendFrameIntoClientArea(m_hWnd, &margins);
            if (!SUCCEEDED(hr))
               TRACE0("Failed in DwmExtendFrameIntoClientArea\n");
        }
    }
}

void CMainFrame::OnNcPaint()
{
    CFrameWnd::OnPaint();
    CDC* dc = GetWindowDC();
    RECT rcClient;
    GetWindowRect(&rcClient);
    dc->FillSolidRect(0,0,RECTWIDTH(rcClient),RECTHEIGHT(rcClient),RGB(255,0,0));

    CPaintDC gdc(this); // device context for painting
    Graphics gr(gdc.m_hDC);
    gr.DrawImage ( m_pLogoImage, 0, 0 );
    ReleaseDC(dc);

}

Windows7下的结果就可以了。

然而,我的window在Windows10下出现了另一个未知的字幕标题栏。

我发现未知标题是由 cs.style 中的 WS_THICKFRAME 引起的。 如果我删除 WS_THICKFRAME,未知的阳离子栏将消失,但我无法调整 window 的边框大小。此外,我的程序无法再捕获自定义标题栏上的最小值、最大值和关闭按钮消息。 我想删除未知的标题栏而没有任何副作用。 谁能给我一个好的解决方案或建议?

此致,

当使用DwmExtendFrameIntoClientArea时,表示框架扩展到客户区。它不再在非客户区。所以不需要重写OnNcPaint,你可以在OnPaint

中完成所有的绘制
void CMainFrame::OnPaint()
{
    CPaintDC dc(this);

    //paint titlebar area (this used to be the non-client area)
    CRect rc;
    GetClientRect(&rc);
    rc.bottom = titlebar_height;

    CDC memdc;
    memdc.CreateCompatibleDC(&dc);
    BITMAPINFOHEADER bmpInfoHeader = { 
        sizeof(BITMAPINFOHEADER), rc.Width(), -rc.Height(), 1, 32 };
    HBITMAP hbitmap = CreateDIBSection(
        dc, (BITMAPINFO*)(&bmpInfoHeader), DIB_RGB_COLORS, NULL, NULL, 0);
    auto oldbitmap = memdc.SelectObject(hbitmap);

    dc.BitBlt(0, 0, rc.Width(), rc.Height(), &memdc, 0, 0, SRCCOPY);
    memdc.SelectObject(oldbitmap);
    DeleteObject(hbitmap);

    //begin normal paint
    //The new client area begins below titlebar_height which we define earlier
    GetClientRect(&rc);
    rc.top = titlebar_height;
    dc.FillSolidRect(&rc, RGB(0, 0, 255));

    Gdiplus::Image *image = Gdiplus::Image::FromFile(L"file.jpg");
    Gdiplus::Graphics gr(dc);
    gr.DrawImage(image, 0, 0);
    delete image;
}

使用成员变量CRect m_border 来跟踪边框的粗细。您可以使用 AdjustWindowRectEx 来计算边框的粗细。

void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
    CFrameWnd::OnActivate(nState, pWndOther, bMinimized);

    titlebar_height = 100;
    //find border thickness
    if (GetWindowLongPtr(m_hWnd, GWL_STYLE) & WS_THICKFRAME)
    {
        m_border = { 0,0,0,0 };
        AdjustWindowRectEx(&m_border, GetWindowLongPtr(m_hWnd,
                GWL_STYLE) & ~WS_CAPTION, FALSE, NULL);
        m_border.left = abs(m_border.left);
        m_border.top = abs(m_border.top);
    }
    else if (GetWindowLongPtr(m_hWnd, GWL_STYLE) & WS_BORDER)
    {
        m_border = { 1,1,1,1 };
    }
    else
    {
        m_border = { 0,0,0,0 };
    }

    //Extend frame in to client area
    MARGINS margins = { 0 };
    margins.cyTopHeight = titlebar_height; //<<=== *** edited
    DwmExtendFrameIntoClientArea(m_hWnd, &margins);
    SetWindowPos(NULL, 0, 0, 0, 0, 
            SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
}

m_border 例如 {7,7,7,7};

允许Windows绘制左、右、下边框。上边框是唯一更改的边框

void CMainFrame::OnNcCalcSize(BOOL validate, NCCALCSIZE_PARAMS FAR* lpncsp)
{
    if (validate)
    {
        lpncsp->rgrc[0].left += m_border.left;
        lpncsp->rgrc[0].right -= m_border.right;
        lpncsp->rgrc[0].bottom -= m_border.bottom;
    }
    else
    {
        CFrameWnd::OnNcCalcSize(validate, lpncsp);
    }
}

另见