如何同时使用鼠标事件? [英] How Do I Use Mouse Events At The Same Time?

查看:95
本文介绍了如何同时使用鼠标事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我以编程方式添加图像来模拟桌面快捷方式....我的想法是我可以拖动图像并点击它们来开始一个过程。

该应用程序通过addHandler创建鼠标事件以便拖动图像...

它使用1)mousedown:它将布尔值设置为true以指示拖动操作已经开始2)mousemove:calculate移动过程中的位置并更新图像位置3)mouseup:将布尔值设置为false



问题是,如果我添加Mouseclick事件,它总是很快被触发因为事件发生了,因为点击只是一个mousedown + mouseup。

是否有解决方法来避免这种情况?

提前谢谢

Alex

解决方案

最好的办法是添加另一个bool:ignoreClick。

如果在MouseUp处理程序中将其设置为true它正在拖动并在点击处理程序中检查它。

Res它在两个地方:在Click处理程序的末尾,在MouseDown事件中只是为了安全起见。




我得到了执行错误的顺序:当您释放鼠标时,您将按此顺序获得事件:

单击
MouseClick
Up

所以它更容易:你所要做的就是检查你是否正在拖动并忽略事件 - 如果你是 - Up事件处理程序将清除拖动标志,以便随后的那些工作。


我发现Button对象对于这个特定问题是不可行的,绝不存在在编程上定义mousedown和mouseup事件的同一对象中实现MouseClick或MouseDoubleClick的方法。

我必须将新按钮类型更改为Picturebox,现在一切正常......除了图片框没有文本属性,所以我不得不使用工具提示来显示图标名称。



感谢大家的帮助。

Alex



我在这里报告完整的代码部分,以防有人发现它有用...基本的想法是模拟windows的桌面快捷方式系统,即桌面上被拖动的图标,可以启动一个过程。





'开始:保存2个变量和文件中的图标名称和关联进程

Private Sub createiconbutton_Click(sender As System.Object,e As System.EventArgs)处理createiconbutton.Click

我的。 Settings.butttext = iconnametext.Text

My.Settings.buttprocess = iconprocesstext.Text

My.Settings.Save()

My.Settings .Reload()

Dim path As String = My.Application.Info.DirectoryPath& \custom\custombuttons.txt

Dim sw As StreamWriter

如果File.Exists(path)= False那么

sw = File.CreateText(path)

sw.WriteLine(iconnametext.Text&^& iconprocesstext.Text)

sw.Flush()

sw.Close()

否则

sw = File.AppendText(路径)

sw.WriteLine(iconnametext.Text& ^& iconprocesstext.Text)

sw.Flush()

sw.Close()

结束如果

Dim NewButton作为新PictureBox

Dim myToolTip As New ToolTip()

'设置新图标属性

使用NewButton

。宽度= 65

。高度= 65

.BackColor = Color.Transparent

.BackgroundImage = My.Resources。 App_icon'在此指定whateve你想要的图像

.BackgroundImageLayout = ImageLayout.Zoom

.Text = My.Settings.butttext

.Font = New Font(Kokila ,14,FontStyle.Bold,GraphicsUnit.Pixel)

。顶部= 100

.Left = 100

myToolTip.SetToolTip(NewButton, My.Settings.butttext)

结束

Me.Controls.Add(NewButton)

AddHandler NewButton.DoubleClick,AddressOf newbutton_DoubleClick

AddHandler NewButton.MouseUp,AddressOf newbutton_MouseUp

AddHandler NewButton.MouseDown,AddressOf newbutton_MouseDown

AddHandler NewButton.MouseMove,AddressOf newbutton_MouseMove

结束子

朋友子newbutton_DoubleClick(发件人作为对象,e作为MouseEventArgs)

尝试

'leggidizionario是读取文件和拆分的函数将字符串转换为键a nd值

LeggiDizionario(发件人,My.Application.Info.DirectoryPath& \ custom\custombuttons.txt)

Process.Start(My.Settings.buttprocess)

Catch ex As Exception

MsgBox (无法启动流程 - 检查定义:& My.Settings.buttprocess& - & My.Settings.butttext)

结束尝试

dict .Clear()

End Sub

Private Sub newbutton_MouseDown(发送者为对象,e为MouseEventArgs)

drag = True

mousex = Windows.Forms.Cursor.Position.X - sender.Left

mousey = Windows.Forms.Cursor.Position.Y - sender.Top

End Sub $ / $
Private Sub newbutton_MouseUp(sender As Object,e As MouseEventArgs)

drag = False

End Sub

Private Sub newbutton_MouseMove(sender As Object,e As MouseEventArgs)

如果drag = True那么

sender.Top = Windows.Forms.Cursor.Position.Y - mousey

sender.Left = Windows.Forms.Cursor.Position.X - mousex

结束如果

结束子

私人Sub LeggiDizionario(sender As Object,FullPathFileName As String)

Dim lines As String()= IO.File.ReadAllLines(FullPathFileName)

For Each line As String In lines

Dim kv As KeyValuePair(Of String,String)= ToKeyValuePair(line)

dict.Add(kv.Key,kv.Value)

如果kv.Key = sender.text那么

My.Settings.buttprocess = kv.Value

结束如果

下一页

End Sub

'此函数拆分定义的2部分

公共函数ToKeyValuePair(对作为字符串)作为KeyValuePair(Of String,String)

Dim two As String()= pair.Split(^)

返回New KeyValuePair(Of String,String)(二(0),二(1))

结束函数

I have an app where i add images programmatically to simulate the desktop shortcuts.... the idea is that i can drag the images around and click on them to start a process.
The app creates the mouse events via addHandler in order to drag the image around...
it uses 1)mousedown: it sets a boolean to true in order to indicate the drag operatio has started 2)mousemove: calculates the position during movement and updates the image position 3)mouseup: sets the boolean to false

the problem is that if i add a Mouseclick event it is always triggered as soon as the event museup occurs, because a click is just a mousedown+mouseup.
Is there a workaround to avoid this?
Thanks in advance
Alex

解决方案

Your best bet is to add another bool: ignoreClick.
Set it to true in the MouseUp handler if it was doing a drag and check it in the Click handler.
Reset it in two places: at the end of the Click handler, and in the MouseDown event just to be on the safe side.


I got the order of execution wrong: when you release the mouse you get events in this order:

Click
MouseClick
Up

So it's even easier: all you have to do is check if you are dragging and ignore the event if you are - the Up event handler will clear the drag flag so subsequent ones will work.


I have found out that the Button object is not viable for this particular issue, in no way there is a method to implement the MouseClick or the MouseDoubleClick in the same object defining the mousedown and mouseup event programmatically.
I had to change the newbutton type to Picturebox and now everything works... except that picturebox does not have a 'Text' property so i had to use a Tooltip to show the icon name.

Thanks to everyone for your aid.
Alex

I report here the full code section in case anyone finds it useful... the basic idea is to simulate windows' desktop shortcut system, that is, icons on the desktop that an be dragged and can start a process.


'start: saves icon name and associated process in 2 variables and in a file
Private Sub createiconbutton_Click(sender As System.Object, e As System.EventArgs) Handles createiconbutton.Click
My.Settings.butttext = iconnametext.Text
My.Settings.buttprocess = iconprocesstext.Text
My.Settings.Save()
My.Settings.Reload()
Dim path As String = My.Application.Info.DirectoryPath & "\custom\custombuttons.txt"
Dim sw As StreamWriter
If File.Exists(path) = False Then
sw = File.CreateText(path)
sw.WriteLine(iconnametext.Text & "^" & iconprocesstext.Text)
sw.Flush()
sw.Close()
Else
sw = File.AppendText(path)
sw.WriteLine(iconnametext.Text & "^" & iconprocesstext.Text)
sw.Flush()
sw.Close()
End If
Dim NewButton As New PictureBox
Dim myToolTip As New ToolTip()
'sets the new icon properties
With NewButton
.Width = 65
.Height = 65
.BackColor = Color.Transparent
.BackgroundImage = My.Resources.App_icon 'assign here whatever image you want
.BackgroundImageLayout = ImageLayout.Zoom
.Text = My.Settings.butttext
.Font = New Font("Kokila", 14, FontStyle.Bold, GraphicsUnit.Pixel)
.Top = 100
.Left = 100
myToolTip.SetToolTip(NewButton, My.Settings.butttext)
End With
Me.Controls.Add(NewButton)
AddHandler NewButton.DoubleClick, AddressOf newbutton_DoubleClick
AddHandler NewButton.MouseUp, AddressOf newbutton_MouseUp
AddHandler NewButton.MouseDown, AddressOf newbutton_MouseDown
AddHandler NewButton.MouseMove, AddressOf newbutton_MouseMove
End Sub
Friend Sub newbutton_DoubleClick(sender As Object, e As MouseEventArgs)
Try
'leggidizionario is the function that reads the file and splits the string into key and value
LeggiDizionario(sender, My.Application.Info.DirectoryPath & "\custom\custombuttons.txt")
Process.Start(My.Settings.buttprocess)
Catch ex As Exception
MsgBox("Cannot start process - check definition: " & My.Settings.buttprocess & " -- " & My.Settings.butttext)
End Try
dict.Clear()
End Sub
Private Sub newbutton_MouseDown(sender As Object, e As MouseEventArgs)
drag = True
mousex = Windows.Forms.Cursor.Position.X - sender.Left
mousey = Windows.Forms.Cursor.Position.Y - sender.Top
End Sub
Private Sub newbutton_MouseUp(sender As Object, e As MouseEventArgs)
drag = False
End Sub
Private Sub newbutton_MouseMove(sender As Object, e As MouseEventArgs)
If drag = True Then
sender.Top = Windows.Forms.Cursor.Position.Y - mousey
sender.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub LeggiDizionario(sender As Object, FullPathFileName As String)
Dim lines As String() = IO.File.ReadAllLines(FullPathFileName)
For Each line As String In lines
Dim kv As KeyValuePair(Of String, String) = ToKeyValuePair(line)
dict.Add(kv.Key, kv.Value)
If kv.Key = sender.text Then
My.Settings.buttprocess = kv.Value
End If
Next
End Sub
'this function splits the 2 parts of the definition
Public Function ToKeyValuePair(pair As String) As KeyValuePair(Of String, String)
Dim two As String() = pair.Split("^")
Return New KeyValuePair(Of String, String)(two(0), two(1))
End Function


这篇关于如何同时使用鼠标事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆