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

标签 .net vb.net winapi notifications taskbar

我需要让我的 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



谁能让我知道我哪里出错了或如何实现这一目标?

最佳答案

首先 Me 用于引用当前类。当该代码在 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, True, 5)

笔记:

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

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

两者都可能对您有用

关于.net - 如何在 vb.net 中闪烁/闪烁任务栏图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23565087/

相关文章:

c# - Lambda 变量名称 - 短名称,还是短名称?

.net - 适用于 Windows 7.0 的低功耗蓝牙 API

ASP.NET/VB.NET : Iterate through multiline textbox

c# - 从 PowerShell 调用 WNetAddConnection2

c++ - 如何遍历多语言版本资源?

c# - 从并发字典中获取所有值并在不丢失数据的情况下清除它

sql-server - 使用 SQL Server 2008 自动生成 VB.NET 表单

asp.net - VB.NET 查询字符串

c++ - 低级编程键盘模拟

c# - 在单声道中编译时 C# List 的问题(与作业相关)