根据这篇文章,我试图模仿
Enabled = False
没有实际禁用控件。(在我的情况下是多行文本框)
接下来我要完成的是通过禁用控件的鼠标来模仿焦点行为。如果我单击禁用的控件,它将不会获得焦点,并且以前具有焦点的控件不会失去焦点。
到目前为止我想出了什么:我可以在 WndProc 中拦截 WM_SETFOCUS 消息,这样我的控件就不会收到焦点。
Private Const WM_SETFOCUS = &H7
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If Me.ReadOnly AndAlso (m.Msg = WM_SETFOCUS) Then Exit Sub
MyBase.WndProc(m)
End Sub
这样做的问题是,以前的控制失去了焦点,这不是故意的。如何防止即使单击鼠标也会在焦点行为中做任何事情?有没有办法做到这一点?
更新:06.08.12
正如贾斯汀所建议的那样,我通过将其更改为自动滚动面板中的标签来解决问题。一个最小的代码示例如下:
Imports System.Windows.Forms
Public Class ScrollableDisabledTextBox
Inherits TextBox
Private xLabel As Label
Private xPanel As Panel
Public Sub New()
InizializeComponent()
End Sub
Private Sub InizializeComponent()
xPanel = New Panel
xPanel.AutoScroll = True
xPanel.BorderStyle = BorderStyle.FixedSingle
xLabel = New Label
xLabel.Enabled = False
xLabel.AutoSize = True
xPanel.Controls.Add(xLabel)
Me.Me_SizeChanged()
End Sub
Private Sub Me_EnabledChanged() Handles Me.EnabledChanged
If Me.Enabled Then
Me.Show()
xPanel.Hide()
Else
xPanel.Show()
Me.SendToBack()
Me.Hide()
End If
End Sub
Private Sub Me_TextChanged() Handles Me.TextChanged
xLabel.Text = Me.Text
End Sub
Private Sub Me_SizeChanged() Handles Me.SizeChanged
xPanel.Size = Me.Size
xLabel.MaximumSize = New System.Drawing.Size(xPanel.Size.Width, 0)
End Sub
Private Sub Me_ParentChanged() Handles Me.ParentChanged
xPanel.Location = Me.Location
'If parent changed multiple times, remember to remove panel from old parent!
If Not Me.Parent.Controls.Contains(xPanel) Then
Me.Parent.Controls.Add(xPanel)
End If
End Sub
结束类