如何在DeskBand任务栏工具栏上强制在C#中使用GDI +渲染ComboBox? (透明度问题)

时间:2009-05-15 19:16:25

标签: toolbar taskbar deskband

我使用Codeproject中的BandObjectLib(Extending Explorer with Band Objects using .NET and Windows Forms)实现了DeskBand任务栏工具栏,修改后支持IDeskBand2接口,允许Windows Vista中的“开始”菜单在启用DeskBand任务栏工具栏时保持透明度。但是,组合框或文本框中显示的文本会显示与文本原始颜色混合的基础桌面背景的颜色。

标签没有此问题,因为它通常使用GDI(+)绘制,忽略渲染文本上的DWMComposition(不是标签的背景)。

我认为问题是因为DWM在Vista上的某些文本元素的工作方式将在以下页面中解释:

Using Vista Controls on Aero Glass
Windows Vista Aero Pt. 1 - Adding Glass to a Windows Forms Application
Adding or Retrofitting Aero Glass into Legacy Windows Applications

我只在DeskBand工具栏上使用组合框,所以我只需要知道如何强制组合框不能使用DWM显示,即使在系统上启用了DWM并且通过IDeskBand2的实现在DeskBand上启用了接口

更新:我进一步研究了它,Adding or Retrofitting Aero Glass into Legacy Windows Applications处的C ++代码似乎最有可能让这个工作变得有效,因此组合框显示文本不透明。如果有人可以查看与组合框相关的代码,并帮助我让它适用于C#组合框,那么这将是我的一个月!我已经开始获得奖励,希望得到答案。

以下是上述项目中的EditProc.cpp类,它可以快速预览我正在寻找的解决方案。要全面了解,您需要查看整个项目:

/*
*
* $RCSfile: aeroedit.cpp,v $
* $Source: /cvs/common/aeroedit.cpp,v $
* $Author: cvs $
* $Revision: 1.12 $
* $Date: 2007/05/20 10:38:25 $
* $State: Exp $
* Copyright (c) Stefan Kuhr
*/

#include <windows.h>
#include <tchar.h>
#include "safassrt.h"
#include "aaeroint.h"
#include "aerosubc.h"
#include "aeroglss.h"
#include <windowsx.h>
#include <gdiplus.h>
using namespace Gdiplus;

static void UpdateIfSelChanged(HWND hWnd, PAERO_SUBCLASS_WND_DATA pWndData)
{
    DWORD dwFirst, dwLast;
    SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwFirst, (LPARAM)&dwLast);
    if(dwFirst!=pWndData->m_dwSelFirst || dwLast!=pWndData->m_dwSelLast)
    {
        pWndData->m_dwSelFirst = dwFirst;
        pWndData->m_dwSelLast = dwLast;
        VERIFY(InvalidateRect(hWnd, NULL, TRUE));
        VERIFY(UpdateWindow(hWnd));
    }
}


static LRESULT CALLBACK EditProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    PAERO_SUBCLASS_WND_DATA pWndData = (PAERO_SUBCLASS_WND_DATA)GetProp(hWnd,         WINDOW_DATA_STRING);
    ASSERT(pWndData);
    ASSERT(pWndData->m_pDwmApiImpl);
    WNDPROC pOldProc = pWndData->m_oldWndProc;
    ASSERT(pOldProc);
    PAERO_SUBCLASS_WND_DATA pWndDataParent =     (PAERO_SUBCLASS_WND_DATA)GetProp(GetParent(hWnd), WINDOW_DATA_STRING);

    /// 
    /// if aero glass is turned off and if we are not in destruction code, 
    /// just call the original wnd proc we had prior to subclassing:
    /// 
    if(WM_DESTROY!=uMsg && WM_NCDESTROY!=uMsg && WM_DWMCOMPOSITIONCHANGED!=uMsg &&     pWndDataParent && !pWndData->m_pDwmApiImpl->IsDwmCompositionEnabled())
        return CallWindowProc(pOldProc, hWnd, uMsg, wParam, lParam);



    if(pWndData->m_uiRedrawMsg==uMsg && pWndData->m_dwFlags & WD_IN_PAINT_CONTROL)
    {
        HDC hdc = GetDC(hWnd);
        hdc = GetDC(hWnd);
        if(hdc)
        {
            RECT rcClient;
            GetClientRect(hWnd, &rcClient);

            BP_PAINTPARAMS params = { sizeof(BP_PAINTPARAMS) };
            params.dwFlags        = 0L;//BPPF_ERASE;
            HDC hdcPaint = NULL;
            HPAINTBUFFER hBufferedPaint = pWndData->m_pUxTheme->BeginBufferedPaint(hdc,     &rcClient, BPBF_TOPDOWNDIB, &params,     &hdcPaint);
            if (hdcPaint)
            {
                LONG_PTR dwStyle = GetWindowLongPtr(hWnd, GWL_STYLE);
                DWORD_PTR dwSyscolorIdx = (dwStyle&WS_DISABLED ||         dwStyle&ES_READONLY)?COLOR_3DFACE:COLOR_WINDOW;
                VERIFY(FillRect(hdcPaint, &rcClient, (HBRUSH)(dwSyscolorIdx+1)));

                SendMessage(hWnd, WM_PRINTCLIENT, (WPARAM) hdcPaint,     PRF_CLIENT|PRF_CHECKVISIBLE);

                /// Make every pixel opaque
                    VERIFY(S_OK==pWndData->m_pUxTheme->BufferedPaintMakeOpaque_(hBufferedPaint, &rcClient));
                VERIFY(S_OK==pWndData->m_pUxTheme->EndBufferedPaint(hBufferedPaint, TRUE));    
        }

        VERIFY(1==ReleaseDC(hWnd, hdc));
        pWndData->m_dwFlags &= ~WD_IN_PAINT_CONTROL;
    }

    return 1;
}

switch(uMsg)
{
    case WM_KEYDOWN:
    {    
        LONG_PTR dwStyle = GetWindowLongPtr(hWnd, GWL_STYLE);
        if(dwStyle&WS_VSCROLL || dwStyle&ES_MULTILINE)
        {
            if(!(pWndData->m_dwFlags&WD_CARET_HIDDEN))
            {
                HideCaret(hWnd);
                pWndData->m_dwFlags|=WD_CARET_HIDDEN;
            }
        }
    }
        break;
    case WM_KEYUP:
    case WM_LBUTTONDOWN:
    case WM_LBUTTONUP:
    case WM_MOUSELEAVE:
    {
        LONG_PTR dwStyle = GetWindowLongPtr(hWnd, GWL_STYLE);
        if(dwStyle&WS_VSCROLL || dwStyle&ES_MULTILINE)
        {
            if(pWndData->m_dwFlags&WD_CARET_HIDDEN)
            {
                ShowCaret(hWnd);
                pWndData->m_dwFlags&=~WD_CARET_HIDDEN;
            }

            UpdateIfSelChanged(hWnd, pWndData);
        }
    }
        break;
    case WM_NCPAINT:
        {
            LRESULT lRes = 0;
            lRes = CallWindowProc(pOldProc, hWnd, uMsg, wParam, lParam);
            DrawEditBorder(hWnd, pWndData);
            return lRes;
        }
    case WM_NCDESTROY:
    case WM_DESTROY:
        VERIFY(UnsubclassControl(hWnd, EditProc, pWndData));
        break;
}

return CallWindowProc(pOldProc, hWnd, uMsg, wParam, lParam);
}

BOOL AeroSubClassEdit(HWND hwnd)
{
    return AeroSubClassControl(hwnd, EditProc, WD_IN_PAINT_CONTROL);
}

谢谢,

John Rennemeyer
MuvEnum, LLC

1 个答案:

答案 0 :(得分:2)

这是一个彻头彻尾的痛苦,我不知道为什么微软没有让WinForms控件与DWMManager一起工作。 TextBox很容易做到,你可以重新绘制它的位图。 DropDown比较棘手,因为它内部有一个原生控件“编辑”框。因为它不是.Net控件(编辑部分),所以你不能使用.Net类轻松地重绘它(我还在试图弄清楚如何做到这一点)。

然而,我已经想出了如何让DropDown在玻璃上渲染,但前提是文本框部分被禁用(通过更改样式来完成)。不完全理想。我在VB.Net,我还在努力。然而,我受到了这个项目的启发,这个人已经在C#中取消了它,可能对你有很大的帮助:

http://dwmwinform.codeplex.com/

作为旁注,WPF支持Aero Glass效果及其所有控件。它的功能更强大,但使用起来也更耗时(IMO ......而且,如果你复制WinForms应用程序,WPF对你没有任何好处)。我只是喜欢WinForms,因为我编写业务应用程序并且不关心或没有时间编写动画(WPF很酷,不要我错,我更喜欢WinForms)。

这是我继承ComboBox(在Vb.Net中)的开始。这个网站没有正确发布整个类,所以我只包括该类的内部:

    ''' <summary>
''' Enum of Windows messages that will trigger the redraw of the control
''' </summary>
''' <remarks></remarks>
Public Enum WindowsMessage
    WM_CHAR = &H100
    WM_KEYDOWN = &H102
    WM_MOUSEMOVE = &H200
    WM_PAINT = 15
    WM_PRINT = &H314
End Enum

''' <summary>
''' Constructor
''' </summary>
''' <remarks></remarks>
Sub New()
    Me.DropDownStyle = ComboBoxStyle.DropDownList
End Sub

''' <summary>
''' Processing of incoming messages.  We're going to get a bitmap of the control and then
''' redraw it onto the form when a few specified windows messages come through.
''' </summary>
''' <param name="m"></param>    
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    MyBase.WndProc(m)

    Select Case m.Msg
        Case WindowsMessage.WM_PAINT, WindowsMessage.WM_CHAR, WindowsMessage.WM_KEYDOWN, _
             WindowsMessage.WM_MOUSEMOVE, WindowsMessage.WM_PRINT
            RedrawControlAsBitmap(Me.Handle)
    End Select

End Sub

''' <summary>
''' Redraws a given control as a bitmap ontop of itself.
''' </summary>
''' <param name="hwnd"></param>
''' <remarks></remarks>
Public Sub RedrawControlAsBitmap(ByVal hwnd As IntPtr)

    Dim c As Control = Control.FromHandle(hwnd)

    If c IsNot Nothing Then
        Using bm As New Bitmap(c.Width, c.Height)
            c.DrawToBitmap(bm, c.ClientRectangle)

            Using g As Graphics = c.CreateGraphics
                g.DrawImage(bm, New Point(0, 0))
            End Using

        End Using
    End If

    c = Nothing

End Sub