Interactive Drag and Drop with Mouse and Keyboard Events in VB.NET
This tutorial demonstrates how to implement interactive drag and drop functionality using mouse events (MouseDown, MouseMove, MouseUp) and keyboard events (KeyDown, KeyUp) in a VB.NET application. Users will learn how to handle mouse actions to move pictures and display dynamic labels based on keyboard inputs. With practical examples like dragging an image and changing UI elements upon key presses, this guide serves as a hands-on approach to understanding event-driven programming in Windows Forms.
Interactive Drag and Drop with Mouse and Keyboard Events in VB.NET
E N D
Presentation Transcript
滑鼠事件 (MouseDown,MouseMove,MouseUp) Public Class Form1 Dim drag As Boolean = False '宣告drag 變數,用來記錄是否可拖曳 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'Me.Text = "拖曳範例" ' Me.BackgroundImage = Image.FromFile("c:/vb2008/ch10/image/BackImage.jpg") ' picBlack.Image = New Bitmap("c:/vb2008/ch10/image/black.gif") 'picBlack.SizeMode = PictureBoxSizeMode.AutoSize 'picBlack.Cursor = System.Windows.Forms.Cursors.Hand End Sub '按下滑鼠 Private Sub picBlack_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBlack.MouseDown If e.Button = Windows.Forms.MouseButtons.Left Then '按下左鍵 drag = True End If End Sub '移動滑鼠 Private Sub picBlack_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBlack.MouseMove If drag Then picBlack.Left += e.X picBlack.Top += e.Y 'picBlack.Left += e.X - picBlack.Width / 2 'picBlack.Top += e.Y - picBlack.Height / 2 End If End Sub '放開滑鼠 Private Sub picBlack_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBlack.MouseUp drag = False End Sub End Class
滑鼠事件 (MouseEnter,MouseHover,MouseLeave) Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load picCard.Image = Image.FromFile("card.jpg") lblKing.Text = "" End Sub '滑鼠進入時 Private Sub picCard_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles picCard.MouseEnter picCard.Image = Image.FromFile("king.jpg") End Sub '滑鼠進入時且暫停時 Private Sub picCard_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles picCard.MouseHover lblKing.Text = "大家好!" + vbCrLf lblKing.Text += "我是老K。K就是king,就是國王。" + vbCrLf lblKing.Text += "很高興認識大家。" End Sub '滑鼠離開時 Private Sub picCard_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles picCard.MouseLeave picCard.Image = Image.FromFile("card.jpg") lblKing.Text = "" End Sub End Class
鍵盤事件(KeyDown, KeyUp) Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load picRider.Image = Image.FromFile("bike_go.jpg") End Sub Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown lblKey.Text = "按下" & e.KeyCode.ToString() & "鍵, 鍵值為" & e.KeyValue 'KeyValue:可以取得鍵盤值(ASCII code) 'KeyCode:取得鍵盤碼 (eg: keys.Left, keys.F1, keys.a, key.B, ......) If e.KeyCode = Keys.A Then picRider.Image = Image.FromFile("bike_go.jpg") If picRider.Left >= Me.Width Then picRider.Left = 0 - picRider.Width Else picRider.Left += 20 End If lblState.Text = "前進!!" ElseIf e.KeyCode = Keys.Left Then picRider.Image = Image.FromFile("bike_go.jpg") If picRider.Width + picRider.Left <= 0 Then picRider.Left = Me.Width Else picRider.Left -= 20 End If lblState.Text = "後退!!" ElseIf e.KeyCode = Keys.Up Then picRider.Image = Image.FromFile("bike_up.jpg") lblState.Text = "舉孤輪!!" ElseIf e.KeyCode = Keys.Down Then picRider.Image = Image.FromFile("bike_stop.jpg") lblState.Text = "煞車!!" End If End Sub End Class