繪製矩形

繪圖部分一直是我不熟悉的領域,最近想弄一些東西應該會來研究一下,下面這段程式在Form上面按住左鍵後移動滑鼠便會畫出矩形,算是第一步吧 ~

Dim BG As Bitmap
Dim G As Graphics

Dim IsMouseDown As Boolean = False
Dim P_Start As New Point

Private Sub frmEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Application.DoEvents()
BG = New Bitmap(PictureBox1.Width, PictureBox1.Height)
G = Graphics.FromImage(BG)
End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
IsMouseDown = True
P_Start = e.Location
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If IsMouseDown Then
G.Clear(Color.Transparent)
If e.X - P_Start.X > 0 Then
If e.Y - P_Start.Y > 0 Then
G.DrawRectangle(New Pen(Color.Pink, 2), _
New Rectangle( _
P_Start.X, P_Start.Y, e.X - P_Start.X, e.Y - P_Start.Y))
Else
G.DrawRectangle(New Pen(Color.Pink, 2), _
New Rectangle( _
P_Start.X, e.Y, e.X - P_Start.X, P_Start.Y - e.Y))
End If
Else
If P_Start.Y - e.Y > 0 Then
G.DrawRectangle(New Pen(Color.Pink, 2), _
New Rectangle( _
e.X, e.Y, P_Start.X - e.X, P_Start.Y - e.Y))
Else
G.DrawRectangle(New Pen(Color.Pink, 2), _
New Rectangle( _
e.X, P_Start.Y, P_Start.X - e.X, e.Y - P_Start.Y))
End If
End If
PictureBox1.Image = BG
End If
End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
G.Clear(Color.Transparent)
IsMouseDown = False
End Sub

留言

這個網誌中的熱門文章

開啟cshtml檔案時,出現『並未將物件參考設定為物件執行個體』的錯誤訊息

無法設定中斷點 尚未載入符號檔

DataGridView欄位計算總合