【问题标题】:VB.NET check if form is focusedVB.NET 检查表单是否聚焦
【发布时间】:2012-09-18 14:44:44
【问题描述】:

我似乎在任何地方都找不到这个看似简单的问题的答案 D:

我想检测一个窗体/窗口是否有焦点,以便我只能闪烁窗口如果没有焦点,这里是代码的一部分:

If Me.Focused = False Then ' Doesn't work D: please fix
    Dim flash As New FLASHWINFO
    flash.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(flash) '/// size of structure in bytes
    flash.hwnd = MyBase.Handle '/// Handle to the window to be flashed
    flash.dwFlags = FLASHW_ALL '/// to flash both the caption bar + the tray
    flash.uCount = 5 '/// the number of flashes
    flash.dwTimeout = 1000 '/// speed of flashes in MilliSeconds ( can be left out )
    '/// flash the window you have specified the handle for...
    FlashWindowEx(flash)
End If

有没有简单的方法来完成这项工作?

【问题讨论】:

  • 你读过答案#2吗?即使那个答案不是 VB.NET 而是 C#,也不应该有真正的区别。
  • 当你刚接触 VB 甚至还没有接触过 C# 时就不会
  • 请阅读答案。答案 #1 与语言无关。答案 #2 向您展示了一个现成的 VB.NET 解决方案。那么你的问题出在哪里?

标签: vb.net focus


【解决方案1】:
  Private Sub Form1_LostFocus(sender As Object, e As System.EventArgs) Handles Me.LostFocus
    Dim flash As New FLASHWINFO
    flash.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(flash) '/// size of structure in bytes
    flash.hwnd = MyBase.Handle '/// Handle to the window to be flashed
    flash.dwFlags = FLASHW_ALL '/// to flash both the caption bar + the tray
    flash.uCount = 5 '/// the number of flashes
    flash.dwTimeout = 1000 '/// speed of flashes in MilliSeconds ( can be left out )
    '/// flash the window you have specified the handle for...
    FlashWindowEx(flash)

  End Sub

【讨论】:

  • 完全符合我的要求,但我已将其放入我的代码中;P 使用一个变量,如果调用此函数,则该变量设置为 false,然后在重新建立焦点时设置为 true。 :) 以便异步后台工作人员可以判断是否专注
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多