我遇到了自己创建的自定义.NET控件的问题。 如果我将控件放在表单上。我可以随意调整大小。 一旦我构建或运行项目,控件默认返回其原始放置的大小!
有什么想法吗?
自定义控制:
Imports System.Drawing.Drawing2D
Public Class KRC4Button
Inherits Button
Private CheckBox As CheckBox
Private FillColorGradiant As Color = Color.FromArgb(151, 151, 151)
Private FillColor As Color = Color.FromArgb(233, 233, 233)
Private PressedFillColorGradiant As Color = Color.FromArgb(108, 200, 255)
Private PressedFillColor As Color = Color.FromArgb(2, 170, 255)
Private DisabledColor As Color = Color.FromArgb(200, 200, 200)
Private BorderColor As Color = Color.FromArgb(100, 100, 100)
Private DisabledBorderColor As Color = Color.FromArgb(170, 170, 170)
Private TextColor As Color = Color.Black
Private DisabledTextColor As Color = Color.FromArgb(136, 136, 136)
Private _CheckBoxVisible As Boolean = False
Private _Checked As Boolean = False
Private BorderPenpenWidth As Single = 1.0F
Private BorderPen As Pen = New Pen(Color.Black, BorderPenpenWidth)
Private KRC4Font As Font = New Font("Tahoma", 13.0F, FontStyle.Regular, GraphicsUnit.Pixel)
Private IsPressed As Boolean = False
Private Radius As Integer = 7
Public Property CheckBoxVisible As Boolean
Get
Return _CheckBoxVisible
End Get
Set(value As Boolean)
_CheckBoxVisible = value
Refresh()
End Set
End Property
Public Property Checked As Boolean
Get
Return _Checked
End Get
Set(value As Boolean)
_Checked = value
Refresh()
End Set
End Property
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
CheckBox = New CheckBox
CheckBox.Text = ""
CheckBox.BackColor = Color.Transparent
Me.Controls.Add(CheckBox)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.ResizeRedraw, True)
SetStyle(ControlStyles.UserPaint, True)
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim BackgroundBrush, TextBrush As Brush
Dim StringSize As New SizeF(e.Graphics.MeasureString(Text, KRC4Font))
If Enabled Then
TextBrush = New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, Me.Height), Me.TextColor, Me.TextColor)
If IsPressed Then
BackgroundBrush = New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, Me.Height), Me.PressedFillColor, Me.PressedFillColorGradiant)
Else
BackgroundBrush = New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, Me.Height), Me.FillColor, Me.FillColorGradiant)
End If
BorderPen.Color = Me.BorderColor
e.Graphics.FillRectangle(BackgroundBrush, ClientRectangle)
Else
TextBrush = New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, Me.Height), Me.DisabledTextColor, Me.DisabledTextColor)
BackgroundBrush = New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, Me.Height), Me.DisabledColor, Me.DisabledColor)
BorderPen.Color = Me.DisabledBorderColor
e.Graphics.FillRectangle(BackgroundBrush, ClientRectangle)
End If
Me.MinimumSize = New Size(66, 40)
Me.MaximumSize = New Size(0, 40)
CheckBox.Visible = CheckBoxVisible
CheckBox.Checked = Checked
CheckBox.Location = New Point(5, Convert.ToInt32(Height / 2) - Convert.ToInt32(CheckBox.Height / 2))
e.Graphics.DrawArc(BorderPen, New Rectangle(0, 1, Radius, Radius), 180, 90)
e.Graphics.DrawArc(BorderPen, New Rectangle(Width - Radius, 0, Radius, Radius), 270, 90)
e.Graphics.DrawArc(BorderPen, New Rectangle(Width - Radius, Height - Radius, Radius, Radius), 0, 90)
e.Graphics.DrawArc(BorderPen, New Rectangle(1, Height - Radius, Radius, Radius), 90, 90)
e.Graphics.DrawRectangle(BorderPen, 0.0F, 0.0F, CType((Width - 1), Single), CType((Height - 1), Single))
' text centred in button
If CheckBoxVisible Then
e.Graphics.DrawString(Text, KRC4Font, TextBrush, 20, Convert.ToInt32(Height / 2) - Convert.ToInt32(StringSize.Height / 2))
Else
e.Graphics.DrawString(Text, KRC4Font, TextBrush, Convert.ToInt32(Width / 2) - Convert.ToInt32(StringSize.Width / 2), Convert.ToInt32(Height / 2) - Convert.ToInt32(StringSize.Height / 2))
End If
End Sub
Private Function GetLeftUpper(ByVal e As Integer) As Rectangle
Return New Rectangle(0, 0, e, e)
End Function
Private Function GetRightUpper(ByVal e As Integer) As Rectangle
Return New Rectangle(Width - e, 0, e - 1, e - 1)
End Function
Private Function GetRightLower(ByVal e As Integer) As Rectangle
Return New Rectangle(Width - e - 1, Height - e, e, e)
End Function
Private Function GetLeftLower(ByVal e As Integer) As Rectangle
Return New Rectangle(0, Height - e, e, e)
End Function
Protected Overrides Sub OnValidated(ByVal e As System.EventArgs)
Me.Refresh()
End Sub
Private Sub KRC4Button_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
IsPressed = True
End Sub
Private Sub KRC4Button_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
IsPressed = False
Refresh()
End Sub
Private Sub KRC4Button_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
IsPressed = False
Refresh()
End Sub
Private Sub KRC4Button_SizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
Dim path As GraphicsPath = New GraphicsPath()
path.StartFigure()
path.AddArc(GetLeftUpper(Radius), 180, 90)
path.AddLine(Radius, 0, Width - Radius, 0)
path.AddArc(GetRightUpper(Radius), 270, 90)
path.AddLine(Width, Radius, Width, Height - Radius)
path.AddArc(GetRightLower(Radius), 0, 90)
path.AddLine(Width - Radius, Height, Radius, Height)
path.AddArc(GetLeftLower(Radius), 90, 90)
path.AddLine(0, Height - Radius, 0, Radius)
path.CloseFigure()
Region = New Region(path)
End Sub
End Class
设计师代码:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class KRC4Button
Inherits Button
'Control overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Control Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer. Do not modify it
' using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.Name = "KRC4Button"
Me.Text = "Text"
Me.MinimumSize = New Size(66, 40)
Me.MaximumSize = New Size(0, 40)
End Sub
End Class