我有一段类似的代码:
Public Class frmControlPad
Inherits System.Windows.Forms.Form
Private bMoved As Boolean
Private bAdded As Boolean
Private bSelected As Boolean
Dim i As Integer
Dim dblBeginX, dblBeginY As Double
Dim strControls As String = ""
Dim WithEvents btn1 As Button
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnNone As System.Windows.Forms.Button
Friend WithEvents btn As System.Windows.Forms.Button
Friend WithEvents pnl As System.Windows.Forms.Panel
Friend WithEvents pnlMain As System.Windows.Forms.Panel
Friend WithEvents pic As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnNone = New System.Windows.Forms.Button
Me.btn = New System.Windows.Forms.Button
Me.pnl = New System.Windows.Forms.Panel
Me.pnlMain = New System.Windows.Forms.Panel
Me.pic = New System.Windows.Forms.PictureBox
Me.pnl.SuspendLayout()
Me.pnlMain.SuspendLayout()
Me.SuspendLayout()
'
'btnNone
'
Me.btnNone.BackColor = System.Drawing.Color.LightGray
Me.btnNone.Cursor = System.Windows.Forms.Cursors.Hand
Me.btnNone.Enabled = False
Me.btnNone.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnNone.Location = New System.Drawing.Point(8, 16)
Me.btnNone.Name = "btnNone"
Me.btnNone.Size = New System.Drawing.Size(64, 24)
Me.btnNone.TabIndex = 0
Me.btnNone.Text = "none"
'
'btn
'
Me.btn.BackColor = System.Drawing.Color.LightGray
Me.btn.Cursor = System.Windows.Forms.Cursors.Hand
Me.btn.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.btn.Location = New System.Drawing.Point(8, 48)
Me.btn.Name = "btn"
Me.btn.Size = New System.Drawing.Size(64, 24)
Me.btn.TabIndex = 1
Me.btn.Text = "按钮"
'
'pnl
'
Me.pnl.BackColor = System.Drawing.Color.LightGray
Me.pnl.Controls.Add(Me.btnNone)
Me.pnl.Controls.Add(Me.btn)
Me.pnl.Location = New System.Drawing.Point(0, 0)
Me.pnl.Name = "pnl"
Me.pnl.Size = New System.Drawing.Size(80, 440)
Me.pnl.TabIndex = 0
'
'pnlMain
'
Me.pnlMain.Controls.Add(Me.pic)
Me.pnlMain.Location = New System.Drawing.Point(88, 0)
Me.pnlMain.Name = "pnlMain"
Me.pnlMain.Size = New System.Drawing.Size(576, 440)
Me.pnlMain.TabIndex = 1
'
'pic
'
Me.pic.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.pic.Location = New System.Drawing.Point(24, 24)
Me.pic.Name = "pic"
Me.pic.Size = New System.Drawing.Size(64, 32)
Me.pic.TabIndex = 4
Me.pic.TabStop = False
Me.pic.Visible = False
'
'frmControlPad
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.Color.Gainsboro
Me.ClientSize = New System.Drawing.Size(664, 442)
Me.Controls.Add(Me.pnlMain)
Me.Controls.Add(Me.pnl)
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.Name = "frmControlPad"
Me.pnl.ResumeLayout(False)
Me.pnlMain.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
bSelected = True
btnNone.Focus()
iniStatus()
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bSelected = False
btn.SuspendLayout()
End Sub
Private Sub btnNone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNone.Click
bSelected = False
pnl.Focus()
iniStatus()
End Sub
Private Sub iniStatus()
If bSelected Then
btnNone.FlatStyle = FlatStyle.Standard
btnNone.BackColor = Color.LightGray
btnNone.Enabled = True
btn.FlatStyle = FlatStyle.Flat
btn.Enabled = False
btn.BackColor = Color.WhiteSmoke
Else
btnNone.FlatStyle = FlatStyle.Flat
btnNone.Enabled = False
btnNone.BackColor = Color.WhiteSmoke
btn.FlatStyle = FlatStyle.Standard
btn.Enabled = True
btn.BackColor = Color.LightGray
End If
End Sub
Private Sub iniPicture(ByVal pic As PictureBox, ByVal x1 As Double, ByVal y1 As Double, ByVal x2 As Double, ByVal y2 As Double)
If bAdded Then
If x1 - x2 > 0 Then
pic.Left = x2
Else
pic.Left = x1
End If
If y1 - y2 > 0 Then
pic.Top = y2
Else
pic.Top = y1
End If
pic.Width = System.Math.Abs(x1 - x2)
pic.Height = System.Math.Abs(y1 - y2)
pic.Visible = True
pic.BringToFront()
Else
pic.Visible = False
End If
End Sub
Private Sub pnlMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlMain.MouseDown
If bSelected Then
bAdded = True
dblBeginX = e.X
dblBeginY = e.Y
Else
bAdded = False
End If
End Sub
Private Sub pnlMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlMain.MouseMove
iniPicture(pic, dblBeginX, dblBeginY, e.X, e.Y)
End Sub
Private Sub pnlMain_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlMain.MouseUp
If bAdded Then
btn1 = New Button
pnlMain.Controls.Add(btn1)
With btn1
.Name = "button" & i
.Text = .Name
If e.X > dblBeginX Then
.Left = dblBeginX
Else
.Left = e.X
End If
If e.Y > dblBeginY Then
.Top = dblBeginY
Else
.Top = e.Y
End If
.Width = System.Math.Abs(dblBeginX - e.X)
.Height = System.Math.Abs(dblBeginY - e.Y)
.BringToFront()
End With
i += 1
bSelected = False
iniStatus()
End If