如何在vb.net中闪烁/闪烁任务栏图标?

当应用程序收到通知时,我需要让我的vb.net应用程序能够闪烁/闪烁以吸引用户的注意.

就像这个图像中的DW图标一样:

我已经谷歌搜索了一段时间,并尝试了各种代码示例,都没有成功.

这是我到目前为止所得到的:

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

此代码立即抛出以下错误:

‘Me’ is valid only within an instance method

任何人都可以让我知道我哪里出错或如何实现这一目标?

解决方法

首先,我用来引用当前的类.当该代码在Form类中时,它具有属性Handle.在您的示例中,FlashWindow类没有属性Handle,因此不会编译.

其次,我认为你对API函数的定义有点偏.

将此类添加到项目中:

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

然后可以像这样闪烁窗口 – 传入对主应用程序表单的引用:

Dim res = WindowsApi.FlashWindow(Process.GetCurrentProcess().MainWindowHandle,True,5)

注意:

我编辑了这里找到的代码:http://pinvoke.net/default.aspx/user32.FlashWindowEx

并使用此处定义的定义:Get user attention without stealing focus

这两者都可能对您有用

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...