VB.Net Tutorial/Event/Mouse Event

Материал из VB Эксперт
Перейти к: навигация, поиск

Double and single click

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class PictureBoxSingleDoubleClicked
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private oldcolor As Color
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image = System.Drawing.Image.FromFile("yourfile.jpg")
        Label1.Text = "Loaded"
    End Sub
    Private Sub Button1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        If (e.Button <> MouseButtons.Left) Then
            Label1.Text = "Left button"
        End If
    End Sub
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Label1.Text = "single cliked"
    End Sub
    Private Sub PictureBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
        Label1.Text = "double clicked"
    End Sub
    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        PictureBox1.BackColor = System.Drawing.Color.Gray
        PictureBox1.Image = System.Drawing.Image.FromFile("yourfile.jpg")
        Label1.Text = "(" + Str(e.X) + "," + Str(e.Y) + ")"
    End Sub
    Private Sub PictureBox1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
        PictureBox1.BackColor = oldcolor
        PictureBox1.Image = System.Drawing.Image.FromFile("yourfile.jpg")
        Label1.Text = "Exit"
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oldcolor = PictureBox1.BackColor
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        CType(Me.PictureBox1, System.ruponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        "
        "Button1
        "
        Me.Button1.Location = New System.Drawing.Point(181, 231)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "load pic"
        Me.Button1.UseVisualStyleBackColor = True
        "
        "Label1
        "
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(28, 9)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(41, 12)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Label1"
        "
        "PictureBox1
        "
        Me.PictureBox1.Location = New System.Drawing.Point(20, 30)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(391, 189)
        Me.PictureBox1.TabIndex = 2
        Me.PictureBox1.TabStop = False
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(431, 266)
        Me.Controls.Add(Me.PictureBox1)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.PictureBox1, System.ruponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
End Class

Form mouse move event

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class FormRefresh
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private MouseX As Integer
    Private MouseY As Integer
    Dim rectangle1 As Rectangle = New Rectangle(15, 35, 80, 80)
    Private Sub Form1_MouseMove(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.MouseEventArgs) _
          Handles Me.MouseMove
        MouseX = e.X
        MouseY = e.Y
        Me.Invalidate()
    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.PaintEventArgs) _
          Handles Me.Paint
          
          rectangle1.X = MouseX
          rectangle1.Y = MouseY
        Dim canvas As Graphics = e.Graphics  
        canvas.DrawRectangle(Pens.DarkRed, rectangle1)
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(469, 387)
        Me.Name = "Form1"
        Me.Text = "Redrawing by Invalidating"
        Me.ResumeLayout(False)
    End Sub
End Class

Mouse Enter and Leave

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class MouseEnterLeave
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private ButtonBackColor As Color = Color.LightGreen
    Private Sub Button2_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.MouseEnter
        ButtonBackColor = Color.Yellow
    End Sub
    Private Sub Button2_MouseLeave(ByVal sender As Object,ByVal e As System.EventArgs) Handles Button2.MouseLeave
        ButtonBackColor = Color.Red
    End Sub
    Private Sub Button2_MouseDown(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseDown
        ButtonBackColor = Color.Green
    End Sub
    Private Sub Button2_MouseUp(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseUp
        ButtonBackColor = Color.LightGreen
        Button2.Refresh()
    End Sub
    Private Sub Button2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button2.Paint
        Dim canvas As Graphics = e.Graphics
        canvas.Clear(ButtonBackColor)
        Dim atomPen As Pen = New Pen(Color.Blue, 2)
        Dim largerFont As Font = New Font(Me.Font.Name, 10)
        e.Graphics.DrawString("This is a form", largerFont, Brushes.Black,0, 20)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox("Button2 clicked!", MsgBoxStyle.Exclamation,"Painting on Controls")
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        "
        "Panel1
        "
        Me.Panel1.Location = New System.Drawing.Point(56, 32)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(368, 40)
        Me.Panel1.TabIndex = 0
        "
        "Button2
        "
        Me.Button2.Location = New System.Drawing.Point(272, 104)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(152, 152)
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Button2"
        Me.Button2.UseVisualStyleBackColor = True
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(479, 284)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Panel1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Text = "Special Effects"
        Me.ResumeLayout(False)
    End Sub
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Button2 As System.Windows.Forms.Button
End Class

Mouse Events Illustation

Option Strict On
imports System
imports System.Drawing
imports System.Windows.Forms
public class MouseEvents : inherits Form
  private lbl as Label
  private WithEvents btnReset as Button
  public sub New()
    Size = new Size(400,600)
    btnReset = new Button()
    btnReset.Parent = me
    btnReset.Location = new Point(250,50)
    btnReset.Text = "Reset"
    lbl = new Label()
    lbl.Parent = me
    lbl.Location = new Point(50,50)
    lbl.Size = new Size(250,250)
    lbl.BorderStyle = BorderStyle.Fixed3D
    
    AddHandler lbl.MouseEnter, AddressOf lbl_MouseEnter
    AddHandler lbl.MouseHover, AddressOf lbl_MouseHover
    AddHandler lbl.MouseLeave, AddressOf lbl_MouseLeave
    AddHandler lbl.MouseDown, AddressOf lbl_MouseDown
      AddHandler lbl.MouseMove, AddressOf lbl_MouseMove
    AddHandler lbl.MouseUp, AddressOf lbl_MouseUp
    AddHandler lbl.MouseWheel, AddressOf lbl_MouseWheel
    AddHandler lbl.Click, AddressOf lbl_Click
    AddHandler lbl.DoubleClick, AddressOf lbl_DoubleClick
  end sub
  public shared sub Main() 
    Application.Run(new MouseEvents())
  end sub
  private sub btnReset_Click(ByVal sender as object, _
                 ByVal e as EventArgs) _
                   Handles btnReset.Click
    lbl.Text = ""
  end sub
  private sub lbl_MouseEnter(ByVal sender as object, _
                 ByVal e as EventArgs)
    lbl.Text = "MouseEnter"
    Console.WriteLine("Label MouseEnter")
  end sub
  private sub lbl_MouseHover(ByVal sender as object, _
                 ByVal e as EventArgs)
    lbl.Text = "MouseHover"
    Console.WriteLine("Label MouseHover")
  end sub
  private sub lbl_MouseLeave(ByVal sender as object, _
                 ByVal e as EventArgs)
    lbl.Text = "MouseLeave"
    Console.WriteLine("Label MouseLeave")
  end sub
  private sub lbl_MouseDown(ByVal sender as object, _
                ByVal e as MouseEventArgs)
    lbl.Text = "MouseDown"
    Console.WriteLine("Label MouseDown")
    Console.WriteLine("Button:  " + e.Button.ToString())
    Console.WriteLine("Clicks:  " + e.Clicks.ToString())
    Console.WriteLine("Delta:  " + e.Delta.ToString())
    Console.WriteLine("X:  " + e.X.ToString())
    Console.WriteLine("Y:  " + e.Y.ToString())
  end sub
  private sub lbl_MouseMove(ByVal sender as object,ByVal e as MouseEventArgs)
    lbl.Text = "MouseMove"
    Console.WriteLine("Label MouseMove")
    Console.WriteLine("Button:  " + e.Button.ToString())
    Console.WriteLine("Clicks:  " + e.Clicks.ToString())
    Console.WriteLine("Delta:  " + e.Delta.ToString())
    Console.WriteLine("X:  " + e.X.ToString())
    Console.WriteLine("Y:  " + e.Y.ToString())
  end sub
  private sub lbl_MouseUp(ByVal sender as object, _
                ByVal e as MouseEventArgs)
    lbl.Text = "MouseUp"
    Console.WriteLine("Label MouseUp")
    Console.WriteLine("Button:  " + e.Button.ToString())
    Console.WriteLine("Clicks:  " + e.Clicks.ToString())
    Console.WriteLine("Delta:  " + e.Delta.ToString())
    Console.WriteLine("X:  " + e.X.ToString())
    Console.WriteLine("Y:  " + e.Y.ToString())
  end sub
  private sub lbl_MouseWheel(ByVal sender as object,ByVal e as MouseEventArgs)
    lbl.Text = "MouseWheel"
    Console.WriteLine("Label MouseWheel")
    Console.WriteLine("Button:  " + e.Button.ToString())
    Console.WriteLine("Clicks:  " + e.Clicks.ToString())
    Console.WriteLine("Delta:  " + e.Delta.ToString())
    Console.WriteLine("X:  " + e.X.ToString())
    Console.WriteLine("Y:  " + e.Y.ToString())
  end sub
  private sub lbl_Click(ByVal sender as object,ByVal e as EventArgs)
    lbl.Text = "Click"
    Console.WriteLine("Label Click")
  end sub
  private sub lbl_DoubleClick(ByVal sender as object,ByVal e as EventArgs)
    lbl.Text = "DoubleClick"
    Console.WriteLine("Label DoubleClick")
  end sub
  protected overrides sub OnMouseEnter(ByVal e as EventArgs)
    myBase.OnMouseEnter(e)
    Console.WriteLine("Form MouseEnter")
  end sub
  protected overrides sub OnMouseHover(ByVal e as EventArgs)
    myBase.OnMouseHover(e)
    Console.WriteLine("Form MouseHover")
  end sub
  protected overrides sub OnMouseLeave(ByVal e as EventArgs)
    myBase.OnMouseLeave(e)
    Console.WriteLine("Form MouseLeave")
  end sub
end class

Mouse move event

Imports System.Windows.Forms
public class MouseMoveEvent
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
#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
   
    Private WithEvents Label1 As System.Windows.Forms.Label
    Private WithEvents lblX As System.Windows.Forms.Label
    Private WithEvents Label3 As System.Windows.Forms.Label
    Private WithEvents lblY As System.Windows.Forms.Label
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.Container
    "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.
    <System.Diagnostics.DebuggerStepThroughAttribute()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.lblX = New System.Windows.Forms.Label()
        Me.lblY = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        "
        "Label1
        "
        Me.Label1.Location = New System.Drawing.Point(8, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(80, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "X coordinate"
        "
        "Label3
        "
        Me.Label3.Location = New System.Drawing.Point(8, 64)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(80, 16)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "Y coordinate"
        "
        "lblX
        "
        Me.lblX.Location = New System.Drawing.Point(184, 16)
        Me.lblX.Name = "lblX"
        Me.lblX.Size = New System.Drawing.Size(80, 16)
        Me.lblX.TabIndex = 1
        "
        "lblY
        "
        Me.lblY.Location = New System.Drawing.Point(184, 64)
        Me.lblY.Name = "lblY"
        Me.lblY.Size = New System.Drawing.Size(80, 16)
        Me.lblY.TabIndex = 3
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(312, 101)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblY, Me.Label3, Me.lblX, Me.Label1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        lblX.Text = e.X
        lblY.Text = e.Y
    End Sub
End Class