16,722
社区成员




namespace 带滚动条的Label控件
{
public class TextBoxLabel : System.Windows.Forms.TextBox
{
[DllImport("user32", EntryPoint = "HideCaret")]
private static extern bool HideCaret(IntPtr hWnd);
[DllImport("user32", EntryPoint = "ShowCaret")]
private static extern bool ShowCaret(IntPtr hWnd);
public TextBoxLabel():base(){
this.TabStop = false;
this.SetStyle(ControlStyles.Selectable, false);
this.Cursor = Cursors.Default;
this.ReadOnly = true;
this.ShortcutsEnabled = false;
this.HideSelection = true;
this.GotFocus += new EventHandler(TextBoxLabel_GotFocus);
this.MouseMove += new MouseEventHandler(TextBoxLabel_MouseMove);
}
private void TextBoxLabel_GotFocus(Object sender, System.EventArgs e){
if (ShowCaret(((TextBox)sender).Handle)){
HideCaret(((TextBox)sender).Handle);
}
}
private void TextBoxLabel_MouseMove(Object sender, MouseEventArgs e){
if (((TextBox)sender).SelectedText.Length > 0){
((TextBox)sender).SelectionLength = 0;
}
}
}
}
[code=vb]
Public Class TextBoxEx
Inherits System.Windows.Forms.TextBox
Declare Function HideCaret Lib "user32" Alias "HideCaret" (ByVal hwnd As Integer) As Integer
Declare Function ShowCaret Lib "user32" Alias "ShowCaret" (ByVal hwnd As Integer) As Integer
Public Sub New()
MyBase.New()
MyBase.TabStop = False
MyBase.SetStyle(ControlStyles.Selectable, False)
MyBase.Cursor = Cursors.Default
MyBase.ReadOnly = True
MyBase.ShortcutsEnabled = False
AddHandler MyBase.GotFocus, AddressOf TextBoxEx_GotFocus '绑定事件处理程序
AddHandler MyBase.MouseMove, AddressOf TextBoxEx_MouseMove '绑定事件处理程序
End Sub
Private Sub TextBoxEx_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
If ShowCaret(CType(sender, TextBox).Handle) Then
HideCaret(CType(sender, TextBox).Handle)
End If
End Sub
Private Sub TextBoxEx_MouseMove(ByVal sender As Object, ByVal e As System.EventArgs)
If CType(sender, TextBox).SelectedText.Length > 0 Then
CType(sender, TextBox).SelectionLength = 0
End If
End Sub
End Class
[/code]