[英]Draw border on a ListBox control
问题
我试图在子类化的ListBox的表面上绘制边框,我得到了漂亮的结果:
但是,如果我滚动控件,则会发生以下情况:
题
在C#或VB.Net中 ,如何正确地在控件边缘上绘制边框,如图片一所示?
(澄清:不是每个项目都有边框)
码
这是我在做什么:
public class mylistbox : inherits listbox
Public Sub New()
MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
End Sub
Private Sub DrawBorder(ByVal g As Graphics)
ControlPaint.DrawBorder(g, Me.ClientRectangle,
BorderColor, ButtonBorderStyle)
End Sub
Private Sub MyBase_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) _
Handles MyBase.DrawItem
Me.ColorizeItems(e)
End Sub
Private Sub ColorizeItems(ByVal e As DrawItemEventArgs)
' non important code here...
Me.DrawBorder(Graphics.FromHwnd(Me.Handle))
End Sub
end class
在WM_NCPAINT(133)上绘制:
//
<DllImport("User32.dll")>_
Public Shared Function GetWindowDC(ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Dim HDC As IntPtr
If m.Msg = 133 Then
HDC = GetWindowDC(m.HWnd)
If HDC <> IntPtr.Zero Then
MyBase.WndProc(m) //call it to draw what it needs
Using g As Graphics = Graphics.FromHdc(HDC)
g.DrawRectangle(Pens.Blue, 0, 0, Me.Width - 1, Me.Height - 1)
End Using
ReleaseDC(m.HWnd, HDC)
Return
End If
End If
MyBase.WndProc(m)
End Sub
编辑
在WM_NCPAINT中进行更多挖掘,您还可以像这样进行操作:
//
<DllImport("User32.dll")> _
Private Shared Function GetWindowDC(ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<StructLayout(LayoutKind.Sequential)> _
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
<DllImport("gdi32.dll")> _
Private Shared Function CreateRectRgnIndirect(ByRef lpRect As RECT) As IntPtr
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Dim HDC As IntPtr
Dim rgn As IntPtr
Dim rt As RECT
Dim pnt As Point
If m.Msg = 133 Then
HDC = GetWindowDC(m.HWnd)
pnt = Me.PointToScreen(New Point(0, 0))
rt.Left = pnt.X
rt.Top = pnt.Y
rt.Right = rt.Left + Me.Width - 4
rt.Bottom = rt.Top + Me.Height - 4
rgn = CreateRectRgnIndirect(rt)
If HDC <> IntPtr.Zero Then
Using g As Graphics = Graphics.FromHdc(HDC)
g.DrawRectangle(Pens.Red, 0, 0, Me.Width - 1, Me.Height - 1)
End Using
m.WParam = rgn
ReleaseDC(m.HWnd, HDC)
End If
End If
MyBase.WndProc(m)
End Sub
我们正在尝试创建一个不包含边框但包含垂直滚动条的矩形 。 从这个矩形创建一个区域 ,并将其传递给wParam 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.