vb.net Cursor.Wait 在 printDialog 之后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14558516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:03:33  来源:igfitidea点击:

Cursor.Wait after printDialog

vb.netwinformscursor

提问by ufo

I'm having a little problem. I set the cursor to wait status. After calling the PrintDialog the cursor returns in default status. I can't set the cursor to wait status again. The code is like this:

我有一个小问题。我将光标设置为等待状态。调用 PrintDialog 后,光标返回默认状态。我无法再次将光标设置为等待状态。代码是这样的:

Cursor.Current = Cursors.WaitCursor
[...]
Dim result As DialogResult = printDialog.ShowDialog()
If result = DialogResult.Cancel Then
    Return
End If
Cursor.Current = Cursors.WaitCursor
[...]

采纳答案by Pilgerstorfer Franz

I just did a small test with your code. When using your code my VS2012 didn't show up Cursor.Currentbut did not throw any exception when using it. So I changed it to

我刚刚用你的代码做了一个小测试。使用您的代码时,我的 VS2012 没有出现,Cursor.Current但在使用时没有抛出任何异常。所以我把它改成

Me.Cursor = Cursors.WaitCursor

Dim result As DialogResult = printDialog.ShowDialog()
If result = DialogResult.Cancel Then
    Return
End If
' not necesary any more
'Cursor.Current = Cursors.WaitCursor

and the WaitCursor stayedafter showing the printDialog.

并且WaitCursor在显示 printDialog 后停留。

EDIT:Found a pretty good explanation on differencebetween Cursor.Current and Cursor!

编辑:找到了一个很好的解释Cursor.Current 和 Cursor之间的区别

EDIT2:I changed my code to make use of HourGlass class from @HansPassant's example stated above. WaitCursor now stays even if you enter a textBox. Anyways - I was still able to get loss of the waitCursor when hovering over the border of eg. a textBox.

EDIT2:我更改了我的代码以使用上述@HansPassant 示例中的 HourGlass 类。即使您输入文本框,WaitCursor 现在也会保留。无论如何 - 当悬停在例如的边界上时,我仍然能够丢失waitCursor。一个文本框。

All in all IMO I think it's not very good to force a waitCursorwhen it is still possible to enter text aso. Perhaps you may consider disabling controlsuntil some kind of actions has finished and afterwards change cursor back.

总而言之,我认为在仍然可以输入文本时强制使用 waitCursor 并不是很好。也许您可以考虑禁用控件,直到某种操作完成,然后再将光标更改回来。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Hourglass.Enabled = True

    Dim result As DialogResult = PrintDialog1.ShowDialog()
    If result = Windows.Forms.DialogResult.Cancel Then
        Return
    End If

    'Cursor.Current = Cursors.WaitCursor
End Sub

Hourglass.vb- I hope I did not make any mistakes when converting it to vb.net

Hourglass.vb- 我希望在将其转换为 vb.net 时没有犯任何错误

Public Class Hourglass
  Implements IDisposable

  Public Shared Property Enabled As Boolean
    Get
        Return Application.UseWaitCursor
    End Get
    Set(ByVal value As Boolean)
        If value = Application.UseWaitCursor Then Return
        Application.UseWaitCursor = value
        Dim f As Form = Form.ActiveForm
        If Not f Is Nothing AndAlso f.Handle <> IntPtr.Zero Then
            SendMessage(f.Handle, 32, f.Handle, 1)
        End If
    End Set
  End Property

  <System.Runtime.InteropServices.DllImport("user32.dll")>
  Private Shared Function SendMessage(hWnd As IntPtr, msg As IntPtr, wp As IntPtr, lp As IntPtr) As IntPtr
  End Function

  Public Sub Dispose() Implements IDisposable.Dispose
    Enabled = False
  End Sub
End Class