8,834
社区成员
发帖
与我相关
我的任务
分享
Public Class PicControl
Inherits PictureBox
Dim oldX, oldY As Integer
Dim txt As String = "pic"
Dim f As New StringFormat
Public Sub New()
InitializeComponent()
f.LineAlignment = StringAlignment.Center
f.Alignment = StringAlignment.Center
Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
End Sub
Public Sub SetID(ByVal index As Integer)
txt = "pic" + index.ToString
End Sub
Private Sub Pic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
oldX = e.X
oldY = e.Y
Else
Dim cd As New OpenFileDialog
cd.Filter = "*.jpg|*.jpg"
If cd.ShowDialog = DialogResult.OK Then
Me.BackgroundImage = Drawing.Image.FromFile(cd.FileName)
End If
End If
End Sub
Private Sub Pic_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 - oldX
Dim dy As Integer = e.Y - oldY
Me.Location = New Point(Me.Location.X + dx, Me.Location.Y + dy)
End If
End Sub
Private Sub Pic_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawString(txt, Font, Brushes.Red, Me.ClientRectangle, f)
End Sub
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 20
Dim ctrl As New PicControl
ctrl.SetID(i)
Me.Controls.Add(ctrl)
Next
End Sub
End Class