在Windows 10上使用DWM API的Aero标题标题栏问题

时间:2016-10-13 09:41:08

标签: c++ winapi mfc aero dwm

为了在标题标题栏上绘制图标,我已经审阅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);

}

Windows 7下的结果很好。 enter image description here

但是,我的窗口在Windows 10下显示另一个未知的标题标题栏。 enter image description here

enter image description here

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

最诚挚的问候,

1 个答案:

答案 0 :(得分:2)

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

另见How to glow the minimum. maximum and close button?