【发布时间】:2014-09-25 00:05:03
【问题描述】:
我有一个从支票簿派生的控件,我称之为“SettingBooleanButton”,但是当任何窗口或对话框被拖动到控件上时,控件会保留拖动的迹象
下图显示了将应用程序窗口拖动到控件上的效果
这是我用于 OnPaint() 的代码块
Public Class SettingBooleanButton
Inherits CheckBox
Private _settingSection As String
Private _settingName As String
Private _associatedSetting As Setting
Public Event StateChange(ByVal affectedSetting As Setting)
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Appearance = Appearance.Button
FlatStyle = FlatStyle.Flat
TextAlign = ContentAlignment.MiddleCenter
AutoSize = False
End Sub
Public Property SettingSection As String
Get
Return _settingSection
End Get
Set(value As String)
_settingSection = value
End Set
End Property
Public Property SettingName As String
Get
Return _settingName
End Get
Set(value As String)
_settingName = value
End Set
End Property
''' <summary>
''' Sets a boolean value to indicate the initial checked state of the control.
''' </summary>
''' <value>
''' <c>true</c> to set it as [checked state]; otherwise, <c>false</c>.
''' </value>
Public Property CheckedState As Boolean
Get
Return Checked
End Get
Set(value As Boolean)
_associatedSetting = New Setting(_settingSection, _settingName, String.Empty)
RemoveHandler CheckedChanged, AddressOf StateChanged
Checked = value
SetText()
AddHandler CheckedChanged, AddressOf StateChanged
End Set
End Property
Private Sub StateChanged(sender As Object, e As EventArgs)
If IsNothing(_associatedSetting) Then
Return
End If
_associatedSetting.Value = Checked.ToString()
SetText()
RaiseEvent StateChange(_associatedSetting)
End Sub
Public Sub SetText()
If Checked Then
Font = New Font(Font.FontFamily, Font.Size, FontStyle.Bold)
ForeColor = Color.WhiteSmoke
Text = Resource.SettingBooleanButton_TrueState
Else
Font = New Font(Font.FontFamily, Font.Size, FontStyle.Regular)
ForeColor = SystemColors.ControlText
Text = Resource.SettingBooleanButton_FalseState
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
If Checked Then
ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Black, ButtonBorderStyle.Solid)
End If
End Sub
End Class
【问题讨论】:
-
就是这样,画一条线的代码?背景等呢?
-
我无法真正重现它。在控件的构造函数中尝试
SetStyle(ControlStyles.UserPaint, True),AllPaintingInWmPaint、OptimizedDoubleBuffer和ResizeRedraw也是如此。 -
@Plutonix:我添加了 e.Graphics.FillRectangle(Brushes.LimeGreen, e.ClipRectangle),以确保背景被替换/重新绘制,但我仍然得到相同的行为跨度>
-
@Jens 也许如果您将按钮的 FlatStyle 属性设置为 Flat,您可以获得相同的行为
-
不,仍然工作得很好。发布更多代码。定义、构造函数、完整的 OnPaint 事件。也许我们可以弄清楚。在黑暗中猜测无济于事。
标签: vb.net winforms custom-controls onpaint