首页 > 解决方案 > 更改 ComboBox 中嵌入的编辑控件的背景色

问题描述

我正在尝试创建一个带有圆角的自定义组合框,我仍然可以通过用户输入来编辑文本。到目前为止,我拥有的解决方案允许我拥有一个像样的 ComboBox,但仅限于 DropDownList 样式,我想加倍努力并尝试绘制当组合框设置为 DropDown 时出现的嵌入式 EditControl。到目前为止我的解决方案是

Public Sub New()

        ' Esta llamada es exigida por el diseñador.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        Me.SetStyle(ControlStyles.UserPaint _
                    Or ControlStyles.AllPaintingInWmPaint _
                    Or ControlStyles.Opaque _
                    Or ControlStyles.ResizeRedraw, True)
        Me.m_ControlBuffer = New Bitmap(Me.Width, Me.Height)
        Me.m_ControlGraphics = Graphics.FromImage(m_ControlBuffer)
        Me.m_BackBuffer = New Bitmap(Me.Width, Me.Height)
        Me.m_BackGraphics = Graphics.FromImage(m_BackBuffer)

        Dim backcolor As Color = If(Enabled, m_UITheme.TextBoxColor, Color.White)
        BackgroundBrush = New SolidBrush(backcolor)
        Me.BackColor = backcolor
    End Sub

和 Paint 方法是

Public Sub CustomPaint(screenGraphics As Graphics)
        ' If there is body to be drawn.
        If Me.Width > 0 AndAlso Me.Height > 0 Then
            ' Clear the background image graphics 
            If Me.m_backImage Is Nothing Then
                '   Cached Background Image
                Me.m_backImage = New Bitmap(Me.Width, Me.Height)
                Dim backGraphics As Graphics = Graphics.FromImage(Me.m_backImage)
                backGraphics.Clear(Color.Transparent)
                Me.PaintTransparentBackground(backGraphics, Me.ClientRectangle)
            End If

            m_BackGraphics.Clear(Color.Transparent)
            m_BackGraphics.DrawImageUnscaled(Me.m_backImage, 0, 0)

            m_ControlGraphics.Clear(Color.Transparent)
            m_ControlGraphics.SmoothingMode = SmoothingMode.HighQuality

            ' Begin drawing
            Dim path As GraphicsPath = GetBoxBorder()
            m_ControlGraphics.FillPath(BackgroundBrush, path)

            PaintArrowAndLine(m_ControlGraphics)

            ' Draw text
            DrawText(m_ControlGraphics)

            m_ControlGraphics.Flush()

            m_BackGraphics.DrawImage(m_ControlBuffer,
                                         New Rectangle(0, 0,
                                                       m_ControlBuffer.Width,
                                                       m_ControlBuffer.Height),
                                         0, 0,
                                         m_ControlBuffer.Width,
                                         m_ControlBuffer.Height,
                                         GraphicsUnit.Pixel)
            m_BackGraphics.Flush()

            '   Now paint this to the screen
            screenGraphics.DrawImageUnscaled(m_BackBuffer, 0, 0)
        End If
    End Sub

问题是对于 DropDown 样式,白色的 EditControl 出现在我的绘图顶部。在其他论坛上搜索时,我收到了获得该控件句柄的想法,也许我可以更改颜色。然后我找到了WM_CTLCOLOREDIT,我认为它可以帮助我完成我必须做的事情。它说,

如果应用程序处理此消息,它必须返回画笔句柄。系统使用画笔绘制编辑控件的背景。

所以我试图在我的 ComboBox 的 WndProc 上捕获该消息并将消息的结果(据我了解是 WM_CTLCOLOREDIT 的返回值)更改为指向我正在创建的画笔的指针。

<DllImport("gdi32.dll")>
Public Shared Function CreateSolidBrush(ByVal color As Integer) As IntPtr
End Function
Protected Overrides Sub WndProc(ByRef m As Message)
    If (m.Msg = &H133) Then
        Dim brush As IntPtr = CreateSolidBrush(RGB(255, 0, 0))
        m.Result = brush
    End If
    MyBase.WndProc(m)

End Sub

我不确定如何使用 WndProc,上面的代码仍然不起作用。任何帮助将不胜感激。

编辑: 更改 WndProc 后,

Private hBrush As IntPtr = CreateSolidBrush(RGB(255, 0, 0))
    Protected Overrides Sub WndProc(ByRef m As Message)

        If (m.Msg = &H133) Then
            m.Result = hBrush
        Else
            MyBase.WndProc(m)
        End If

    End Sub

它确实绘制了红色矩形,除非我将焦点放在控件上。见下图, 没有焦点 有重点

我感觉如此遥远,同时又如此接近解决方案。

标签: windowsvb.netwinformscomboboxgdi

解决方案


推荐阅读