急急急!!! 如何在按上下左右键是BUTTON自动相应

wblovec 2009-06-03 09:51:55
急急急!!! 如何在按上下左右键是BUTTON自动响应
如何能吧方向键和BUTTON控件连在一起 就是按上下左右任意键 能弄相应鼠标单击BUTTON事件 也就是能用方向键 控制图片的上下移动
...全文
160 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzuomin 2009-06-04
  • 打赏
  • 举报
回复
用鼠标和键盘控制图片移动源码


Public Class Form1

Private WithEvents panel As Panel
Private WithEvents pic As PictureBox

Private x, y As Integer


Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Up
panel.AutoScrollPosition = New Point(0 - panel.AutoScrollPosition.X, 1 - panel.AutoScrollPosition.Y)
Case Keys.Down
panel.AutoScrollPosition = New Point(0 - panel.AutoScrollPosition.X, -1 - panel.AutoScrollPosition.Y)
Case Keys.Left
panel.AutoScrollPosition = New Point(1 - panel.AutoScrollPosition.X, 0 - panel.AutoScrollPosition.Y)
Case Keys.Right
panel.AutoScrollPosition = New Point(-1 - panel.AutoScrollPosition.X, 0 - panel.AutoScrollPosition.Y)
Case Else
Return
End Select
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
panel = New Panel With {.BorderStyle = BorderStyle.Fixed3D, .Dock = DockStyle.Fill, .AutoScroll = True}
pic = New PictureBox With {.BorderStyle = BorderStyle.None, .SizeMode = PictureBoxSizeMode.AutoSize}
panel.Controls.Add(pic)
Me.Controls.Add(panel)

Using ofd As New OpenFileDialog With {.Filter = "Image|*.bmp;*.jpg;*.png"}
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
pic.Image = Image.FromFile(ofd.FileName)
End If
End Using
End Sub

Private Sub pic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
pic.Cursor = Cursors.Hand
x = e.X
y = e.Y
End If
End Sub

Private Sub pic_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim dx As Integer = x - e.X
Dim dy As Integer = y - e.Y
panel.AutoScrollPosition = New Drawing.Point((dx - panel.AutoScrollPosition.X), (dy - panel.AutoScrollPosition.Y))
End If
End Sub

Private Sub pic_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic.MouseUp
pic.Cursor = Cursors.Arrow
End Sub
End Class

hxd209 2009-06-04
  • 打赏
  • 举报
回复
这个API是不可少的
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)

Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2 'left mouse button down
Private Const MOUSEEVENTF_LEFTUP As Integer = &H4 ' left mouse button up
Private Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right mouse button down
Private Const MOUSEEVENTF_RIGHTUP = &H10 ' right mouse button up


这几个函数用来做鼠标的按键(x,y鼠标位置):

Public Function getCapslock() As Boolean
'return or set the caps lock toggle
getCapslock = CBool(GetKeyState(System.Windows.Forms.Keys.Capital) And 1)
End Function

Public Function getShift() As Boolean
'check to see if the shift key is pressed
getShift = CBool(GetAsyncKeyState(System.Windows.Forms.Keys.ShiftKey))
End Function

Private Sub RightClick(ByVal x As Integer, ByVal y As Integer)
Dim p As New System.Drawing.Point(x, y)

' Cursor.Position = p 'New System.Drawing.Point(x, y)
mouse_event(MOUSEEVENTF_RIGHTDOWN Or MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
End Sub

Private Sub LeftClick(ByVal x As Integer, ByVal y As Integer)
Dim p As New System.Drawing.Point(x, y)

' Cursor.Position = p 'New System.Drawing.Point(x, y)
mouse_event(MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub

最后判断按了哪个键,然后调用上面上应该的几个函数:
如判断
if 按了上键 then
LeftClick(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y)
end if

上面这句话就是判断如果按了上键就调用单击左键的函数在鼠标为(X,Y)的位置进行程序单击.

wzuomin 2009-06-04
  • 打赏
  • 举报
回复
一会儿我给你写段代码,瞧瞧行不?呵呵
hxd209 2009-06-04
  • 打赏
  • 举报
回复
我会,但是不知道怎么发程序给你.很长,要就加QQ:469840643
joeandlily 2009-06-04
  • 打赏
  • 举报
回复
在keydown或者keypress中处理,
对应的参数e可以判断出当前按下哪个按键
做出相应处理。
Luoning9527 2009-06-03
  • 打赏
  • 举报
回复
1. 添加事件。
2. 监听鼠标和键盘。
3. 响应动作.
a854468521 2009-06-03
  • 打赏
  • 举报
回复
建议使用钩子。

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧