Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
創(chuàng)新互聯是一家從事企業(yè)網站建設、成都網站制作、成都網站建設、行業(yè)門戶網站建設、網頁設計制作的專業(yè)的建站公司,擁有經驗豐富的網站建設工程師和網頁設計人員,具備各種規(guī)模與類型網站建設的實力,在網站建設領域樹立了自己獨特的設計風格。自公司成立以來曾獨立設計制作的站點數千家。
Dim GetColor As Color = CType(PictureBox1.Image, Bitmap).GetPixel(e.X, e.Y)
TextBox3.Text = GetColor.B.ToString
TextBox2.Text = GetColor.G.ToString
TextBox1.Text = GetColor.R.ToString
End Sub
幾乎找不到真正VB.NET的api,都是自己將VB的api改成VB.NET的api,我的理解是聲明中對應輸入是H的地方保留Long,其他的Long改成Integer,既然VB.NET連MSDN都找不到較全的api說明,不保證api的用法在VB.NET其他版本中適用。
給你個好玩的例子,包括鼠標的隱藏(Ctrl_↓)、顯示(Ctrl_↑),移動、點擊
在Form1上放上2個TextBox,其中TextBox1.Multiline = True,并且面積大些,2個Button,運行后在TextBox1中點擊一下會記錄位置,移開鼠標,點擊移動或點擊Button,可模擬在TextBox1中的點擊
Public Class Form1
Public My_MousePosition As Point
'模擬鼠標移動、點擊
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
'定義光標的位置
Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Integer, ByVal y As Integer) As Integer
'鼠標的隱藏
Declare Function ShowCursor Lib "user32" Alias "ShowCursor" (ByVal bShow As Integer) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Multiline = True
Button1.Text = "點擊"
Button2.Text = "移動"
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Control And e.KeyCode = Keys.Down Then ShowCursor(0)
If e.Control And e.KeyCode = Keys.Up Then ShowCursor(1)
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
TextBox2.Text = MousePosition.X " " MousePosition.Y
End Sub
Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
My_MousePosition = MousePosition
TextBox1.Text = My_MousePosition.X " " My_MousePosition.Y vbCrLf
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SetCursorPos(My_MousePosition.X, My_MousePosition.Y)
mouse_event(H2, 0, 0, 0, 0)
mouse_event(H4, 0, 0, 0, 0)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SetCursorPos(My_MousePosition.X, My_MousePosition.Y)
End Sub
End Class
其中
mouse_event Lib "user32" (ByVal dwFlags As Long
的dwFlags的代碼是
Const MOUSEEVENTF_LEFTDOWN = H2
Const MOUSEEVENTF_LEFTUP = H4
Const MOUSEEVENTF_MIDDLEDOWN = H20
Const MOUSEEVENTF_MIDDLEUP = H40
Const MOUSEEVENTF_MOVE = H1
Const MOUSEEVENTF_ABSOLUTE = H8000
Const MOUSEEVENTF_RIGHTDOWN = H8
Const MOUSEEVENTF_RIGHTUP = H10
Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Long '全屏坐標聲明
Private Declare Function ScreenToClient Lib "user32.dll" (ByVal hwnd As Int32, ByRef lpPoint As POINTAPI) As Int32 '窗口坐標聲明
Private Structure POINTAPI '聲明坐標變量
Public x As Int32 '聲明坐標變量為32位
Public y As Int32 '聲明坐標變量為32位
End Structure
'以上是聲明部分
'以下是窗口部分
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick '用時鐘控件來取坐標,窗口上放個Timer組件,Enabled為允許,周期為10到100毫秒均可
Dim P As POINTAPI '聲明坐標變量
Dim xx, yy '聲明轉換要用到的變量
GetCursorPos(P) '獲取鼠標在屏幕中的位置
ScreenToClient(Me.Handle.ToInt32, P) '轉換為本窗體的坐標
xx = P.x.ToString '把X轉換成能顯示到文本框的字符串格式
yy = P.y.ToString '把Y轉換成能顯示到文本框的字符串格式
TextBox1.Text = xx + "和" + yy '文本框的內容為X坐標和Y坐標
End Sub
通過窗體的MouseDown事件的e.X和e.Y屬性獲取。通過e.Location屬性獲取Point類型的坐標。
下面的代碼示例使用 Location 屬性跟蹤鼠標左鍵單擊,并繪制一系列直線段作為對用戶輸入的響應。如果隱藏窗體然后重新顯示它,此示例不保留已繪制的線段,該代碼為簡單起見而被省略了。
Dim FirstPoint As Point
Dim HaveFirstPoint As Boolean = False
Sub Form1_MouseDownDrawing(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If HaveFirstPoint Then
Dim g As Graphics = Me.CreateGraphics()
g.DrawLine(Pens.Black, FirstPoint, e.Location)
HaveFirstPoint = False
Else
FirstPoint = e.Location
HaveFirstPoint = True
End If
End Sub
調用api
在Visual Baisc.net中的聲明:
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = H2 '模擬鼠標左鍵按下
Public Const MOUSEEVENTF_LEFTUP = H4 ’模擬鼠標左鍵釋放
Public Const MOUSEEVENTF_MIDDLEDOWN = H20 '模擬鼠標中間鍵按下
Public Const MOUSEEVENTF_MIDDLEUP = H40 '模擬鼠標中間鍵釋放
Public Const MOUSEEVENTF_RIGHTDOWN = H8 '模擬鼠標右鍵按下
Public Const MOUSEEVENTF_RIGHTUP = H10 '模擬鼠標右鍵釋放
Public Const MOUSEEVENTF_MOVE = H1 '模擬鼠標指針移動
例:
mouse_event MOUSEEVENTF_LEFTDOWN,10,10,0,0
'在(10,10)模擬鼠標左鍵按下