简体   繁体   English

我菜单的双重缓冲

[英]Double buffering for my Menu

i have implemented double buffering in my application, using the code below. 我已经使用以下代码在我的应用程序中实现了双缓冲。 After wards, i found that my child windows (ie, buttoms), was not animating like normal, so i added WS_EX_COMPOSITED to my parent window, and the child windows are now animating correctly. 在病房之后,我发现我的子窗口(即buttoms)的动画设置不正常,因此我在父窗口中添加了WS_EX_COMPOSITED动画,并且子窗口现在可以正确设置动画了。 However, after adding WS_EX_COMPOSITED, my Menu that is created from my resource, is black, and not displaying properly. 但是,添加WS_EX_COMPOSITED之后,从我的资源创建的菜单是黑色的,并且无法正确显示。

So, how to add double buffering to my top Menu? 那么,如何在我的顶部菜单中添加双缓冲?

MainWinHwnd = CreateWindowEx(WS_EX_COMPOSITED, MainWinClassName, MainWinTitle,
WS_VISIBLE|WS_BORDER|WS_SYSMENU|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_SIZEBOX|WS_CLIPCHILDREN,
        NULL, NULL, 609, 440, NULL, NULL, hInstance, NULL);
    if (!MainWinHwnd) return FALSE;

case WM_PAINT:
    {
        RECT WinRt;
        RECT TxtRt;
        HFONT TxtFont = NULL;
        SIZE TxtSize;

        HDC WinDC = GetDC(hWnd);
        GetClientRect(hWnd, &WinRt);
        HDC WinBuffer = CreateCompatibleDC(WinDC);
        HBITMAP BitBuffer = CreateCompatibleBitmap(WinDC, WinRt.right, WinRt.bottom);
        SelectObject(WinBuffer, BitBuffer);

        FillRect(WinBuffer, &WinRt, (HBRUSH) (COLOR_BTNFACE + 1));

        SetBkColor(WinBuffer, GetSysColor((COLOR_BTNFACE)));

        DrawThemeBackground(ButtonThemeData, WinBuffer, BP_GROUPBOX, GBS_NORMAL, &GroupOptionBox, NULL);
        DrawThemeBackground(DragDropThemeData, WinBuffer, RP_GRIPPER, 0, &DragDropItem, NULL);

        for (DWORD I = 0; I < sizeof(MainWinLabels) / sizeof(CustomLabel); I++)
        {
            if (MainWinLabels[I].Font == NULL)
            {
                TxtFont = CreateFontA(MainWinLabels[I].cFontHeight, NULL, NULL, NULL, FW_DONTCARE,
                    MainWinLabels[I].cFontItalic, MainWinLabels[I].cFontUnderline,
                    MainWinLabels[I].cFontStrikeOut, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
                    MainWinLabels[I].cFontQuality, DEFAULT_PITCH, MainWinLabels[I].cFontFaceName);
                if (TxtFont == NULL) continue;
                SelectObject(WinBuffer, TxtFont);
            }
            else SelectObject(WinBuffer, *MainWinLabels[I].Font);

            SetTextColor(WinBuffer, MainWinLabels[I].Color);

            TxtRt.left = MainWinLabels[I].X;
            TxtRt.top = MainWinLabels[I].Y;

            DrawTextA(WinBuffer, MainWinLabels[I].Text, strlen(MainWinLabels[I].Text), &TxtRt,
                DT_LEFT|DT_SINGLELINE|DT_NOCLIP);

            GetTextExtentPoint32A(WinBuffer, MainWinLabels[I].Text, strlen(MainWinLabels[I].Text), &TxtSize);
            MainWinLabels[I].Width = TxtSize.cx;
            MainWinLabels[I].Height = TxtSize.cy;

            if (TxtFont != NULL) DeleteObject(TxtFont);
        }

        BitBlt(WinDC, 0, 0, WinRt.right, WinRt.bottom, WinBuffer, 0, 0, SRCCOPY);

        ReleaseDC(hWnd, WinDC);
        DeleteObject(BitBuffer);
        DeleteDC(WinBuffer);

        break;
    }

The solution: 解决方案:

  1. Do not use WS_EX_COMPOSITED. 不要使用WS_EX_COMPOSITED。
  2. Instead of using GetDC(hWnd) use BeginPaint(hwnd, &ps) and EndPaint(hwnd, &ps) 代替使用GetDC(hWnd),使用BeginPaint(hwnd,&ps)和EndPaint(hwnd,&ps)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM