日期:2014-05-18 浏览次数:21039 次
'自定义控件代码 Public Class TestControl Dim MouseDownPt As Point Public Sub New() ' 此调用是 Windows 窗体设计器所必需的。 InitializeComponent() ' 在 InitializeComponent() 调用之后添加任何初始化。 Me.SetStyle(ControlStyles.ContainerControl, True) End Sub Private Sub TestControl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown MouseDownPt = e.Location End Sub Private Sub TestControl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then Dim dx As Integer = e.X - MouseDownPt.X Dim dy As Integer = e.Y - MouseDownPt.Y Me.Location = New Point(Me.Location.X + dx, Me.Location.Y + dy) End If End Sub End Class '测试窗体代码(窗体添加一个TestControl1控件) Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim NewBtn As New Button Dim NewCheckBox As New CheckBox Me.TestControl1.Controls.Add(NewBtn) Me.TestControl1.Controls.Add(NewCheckBox) NewBtn.Text = "Button1" NewBtn.Location = New Point(10, 10) NewCheckBox.Text = "CheckBox1" NewCheckBox.Location = New Point(10, 50) End Sub End Class