【发布时间】:2010-12-15 21:54:05
【问题描述】:
使用下面的代码:
Private Sub ShowDropDown()
Using f As New DropDownForm
f.Visible = True
Do While f.Visible
Application.DoEvents()
// Call to not take up 100% resources
Loop
End Using
End Sub
如果 ShowDropDown 方法不是由单击按钮调用的,则在 DropDownForm 中的第一次鼠标单击将被忽略。 因此,如果它是在单击 PictureBox 或单击窗体后调用的,则会被忽略。
我可以通过执行以下操作来解决问题:
Private Sub ShowDropDown()
Using f As New DropDownForm
f.Visible = True
Dim capture As IntPtr = GetCapture()
If (capture <> IntPtr.Zero) Then
SendMessage(New HandleRef(Nothing, capture), &H1F, IntPtr.Zero, IntPtr.Zero)
ReleaseCapture()
End If
Do While f.Visible
Application.DoEvents()
Loop
End Using
End Sub
这是一个猜测,在查看反射器中的 Form.ShowDialog 方法之后。
我的问题是,我可以进行托管调用来获得相同的结果吗?按钮点击有什么其他点击没有的功能?
ETA:如果我使用键打开表单,则不会出现此问题。
【问题讨论】:
-
仅供参考 - 您可以使用 Thread.Sleep(0) 作为 Application.DoEvents() 的替代方法来停止 100% CPU... Application.DoEvents() 可能会给您带来您没有的副作用想要。
-
我不知道哪个更好,但我使用 Application.DoEvents(),然后是 MsgWaitForMultipleObjectsEx(0, IntPtr.Zero, 250, &HFF, 4)。这就是 PropertyGrid 处理它的方式。
标签: .net vb.net winforms forms