Visual Basic 2010在MouseDown事件中检测MouseUp

时间:2015-11-10 23:46:16

标签: vb.net visual-studio-2010 mouseevent

我正在进行的项目涉及在鼠标停止时循环代码并在鼠标再次启动时停止代码。我现在的问题似乎是它自动假设鼠标已关闭,因为它正在运行MouseDown代码,或者至少我将其翻译为。

Private Sub picDisplay_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pictureBox1.MouseDown    
isMouseDown = True
        Me.Text = "Down"
        bg = New Bitmap("base.jpg")
        picDisplay.Image = bg
        moX = MousePosition.X
        moY = MousePosition.Y
        Do
            'insert a ton of code here
            If System.Windows.Input.Mouse.LeftButton.HasFlag(MouseButtonState.Pressed) Then isMouseDown = False
        Loop Until isMouseDown = False
End Sub

我尝试过的事情包括添加MouseUp事件,使用计时器,运行检测器,以及Case MouseButtons Private Sub BackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles moveWindow.DoWork bg = New Bitmap("base.jpg") picDisplay.Image = bg moX = MousePosition.X moY = MousePosition.Y Do mcX = MousePosition.X mcY = MousePosition.Y Dim movementX As Integer = moX - mcX Dim movementY As Integer = moY - mcY displayX = displayX - movementX displayY = displayY - movementY 'If displayX <= 0 And displayY <= 0 Then --- This isn't working right now, but I know why and I'll fix it later picDisplay.CreateGraphics.DrawImage(bg, displayX, displayY, bg.Width, bg.Height) If moveWindow.CancellationPending Then Exit Do Application.DoEvents() Loop Until isMouseDown = False End Sub 来自this的帖子,所有这些都没有结果。

赞赏我的评论!

编辑:

好的,我有这个,但是当前的问题是我已经在使用的对象,我从绘制我的图像作为代码的一部分:

//Array filled with data from external file
$patterns = array('!test!', 'stuff1', 'all!!', '');

//Delete empty values in array
$patterns = array_filter($patterns);

foreach($patterns as &$item){
       $item = preg_quote($item);
}

$pattern = '/(\b|^|- |--|-)(?:'.implode('|', $patterns).')(-|--| -|\b|$)/i';

$clid = "I am the !test! stuff1 all!! string";

echo $clid;
$clid = trim(preg_replace($pattern, ' ', $clid));
echo $clid;

问题似乎是它试图第二次绘制图像,但是在第一次我不确定如何解决这个问题时仍然没有完成绘制它,但我会尝试一些事情我在此期间知道的。

2 个答案:

答案 0 :(得分:0)

这是另一种方法。要测试,请创建一个新的WinForms项目。添加2个标签和一个按钮。然后添加以下代码:

Dim mouseIsDown As Boolean

Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
    Dim counter As Integer
    mouseIsDown = True
    Label1.Text = "Mouse is down"
    Do
        counter += 1
        Label2.Text = CStr(counter)
        Application.DoEvents()
    Loop While mouseIsDown
    Label1.Text = "Mouse is up"
End Sub

Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
    mouseIsDown = False
End Sub

答案 1 :(得分:0)

首先,在Form的设计器视图中,将BackgroundWorker添加到表单中 - 它应该像计时器一样显示在表单下面。单击它以显示visual studio窗口右侧的属性。将 WorkerSupportsCancellation False 更改为 True

用上面的代码替换上面的代码,记住将代码添加到 BackgroundWorker1_DoWork 子中的循环中。

Private Sub picDisplay_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pictureBox1.MouseDown
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        If BackgroundWorker1.IsBusy Then
            If BackgroundWorker1.WorkerSupportsCancellation Then
                BackgroundWorker1.CancelAsync()
            End If
        End If
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Me.Text = "Down"
        bg = New Bitmap("base.jpg")
        picDisplay.Image = bg
        moX = MousePosition.X
        moY = MousePosition.Y
        Do
            'insert a ton of code here
            'Don't delete the line below
            If BackgroundWorker1.CancellationPending Then Exit Do
        Loop
    End Sub

现在发生的情况是,当您单击鼠标按钮时, MouseDown 事件将触发,这将启动包含您的代码的 BackgroundWorker1_DoWork 代码,并检查 BackgroundWorker1.CancellationPending 标志,看它是否为真,然后退出循环。

当您松开鼠标按钮时, MouseUp 代码会运行并检查BackgroundWorker是否实际运行以及是否支持取消。如果它正在运行并支持取消,那么它会运行 CancelAsync 方法,该方法将 CancellatonPending 标志设置为True。与此同时,你的代码正在忙着循环。当循环到达检查 CancellationPending 标志的点时,它退出循环并且后台线程终止。

可能在后台运行代码时遇到问题,具体取决于它与之交互的内容,但是试一试,你可能最终会幸运。如果你确实遇到了问题,那就去解决它们(谷歌是你的朋友)。如果你仍然被困住,请回来看看我们:O)