如何在 vb.net 中闪烁/闪烁任务栏图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23565087/
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
How to flash/blink taskbar icon in vb.net?
提问by John
I need to make my vb.net application be able to flash/blink to attract a user's attention when a notification is received within the application.
我需要让我的 vb.net 应用程序能够在应用程序中收到通知时闪烁/闪烁以吸引用户的注意力。
Like the DW icon does in this image:
就像这张图片中的 DW 图标一样:


I've been Googling this for a while and have tried various code examples, all with no success.
我已经在谷歌上搜索了一段时间并尝试了各种代码示例,但都没有成功。
Here's what I've got so far:
这是我到目前为止所得到的:
Public Class FlashWindow
Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
Shared Sub main()
FlashWindow(Me.Handle, 1)
End Sub
End Class
This code throws the following error straight away:
此代码立即引发以下错误:
'Me' is valid only within an instance method
'Me' 仅在实例方法中有效
Can anyone let me know where I'm going wrong or how to achieve this?
任何人都可以让我知道我哪里出错了或如何实现这一目标?
回答by Matt Wilko
Firstly Meis used to reference the current class. When that code is in a Form class it has a property Handle. In your example the FlashWindow class does not have a property Handle so this will not compile.
首先Me用于引用当前类。当该代码在 Form 类中时,它具有一个属性Handle。在您的示例中,FlashWindow 类没有属性 Handle,因此无法编译。
Secondly I think that your definition of the API function is a little off.
其次,我认为您对 API 函数的定义有点偏离。
Add this class to your project:
将此类添加到您的项目中:
Public Class WindowsApi
Private Declare Function FlashWindowEx Lib "User32" (ByRef fwInfo As FLASHWINFO) As Boolean
' As defined by: http://msdn.microsoft.com/en-us/library/ms679347(v=vs.85).aspx
Public Enum FlashWindowFlags As UInt32
' Stop flashing. The system restores the window to its original state.
FLASHW_STOP = 0
' Flash the window caption.
FLASHW_CAPTION = 1
' Flash the taskbar button.
FLASHW_TRAY = 2
' Flash both the window caption and taskbar button.
' This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
FLASHW_ALL = 3
' Flash continuously, until the FLASHW_STOP flag is set.
FLASHW_TIMER = 4
' Flash continuously until the window comes to the foreground.
FLASHW_TIMERNOFG = 12
End Enum
Public Structure FLASHWINFO
Public cbSize As UInt32
Public hwnd As IntPtr
Public dwFlags As FlashWindowFlags
Public uCount As UInt32
Public dwTimeout As UInt32
End Structure
Public Shared Function FlashWindow(ByRef handle As IntPtr, ByVal FlashTitleBar As Boolean, ByVal FlashTray As Boolean, ByVal FlashCount As Integer) As Boolean
If handle = Nothing Then Return False
Try
Dim fwi As New FLASHWINFO
With fwi
.hwnd = handle
If FlashTitleBar Then .dwFlags = .dwFlags Or FlashWindowFlags.FLASHW_CAPTION
If FlashTray Then .dwFlags = .dwFlags Or FlashWindowFlags.FLASHW_TRAY
.uCount = CUInt(FlashCount)
If FlashCount = 0 Then .dwFlags = .dwFlags Or FlashWindowFlags.FLASHW_TIMERNOFG
.dwTimeout = 0 ' Use the default cursor blink rate.
.cbSize = CUInt(System.Runtime.InteropServices.Marshal.SizeOf(fwi))
End With
Return FlashWindowEx(fwi)
Catch
Return False
End Try
End Function
End Class
Then can flash the window like this - passing in the reference to the main application form:
然后可以像这样闪烁窗口 - 传递对主应用程序表单的引用:
Dim res = WindowsApi.FlashWindow(Process.GetCurrentProcess().MainWindowHandle, True, True, 5)
Note:
笔记:
I edited the code found here: http://pinvoke.net/default.aspx/user32.FlashWindowEx
我编辑了在这里找到的代码:http: //pinvoke.net/default.aspx/user32.FlashWindowEx
and used the definition defined here: Get user attention without stealing focus
并使用此处定义的定义:在不窃取焦点的情况下获得用户注意力
both of which may be useful references to you
两者都可能对您有用
回答by Mark
If you were building a Windows Forms application, Me.Handlewould be the way to go, but that looks like a console application, in which case try using Process.GetCurrentProcess().MainWindowHandleinstead of Me.Handle. See this questionfor further details.
如果您正在构建 Windows 窗体应用程序,Me.Handle这将是Process.GetCurrentProcess().MainWindowHandle可行的方法,但它看起来像一个控制台应用程序,在这种情况下,请尝试使用而不是 Me.Handle。有关更多详细信息,请参阅此问题。

