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.
Like the DW icon does in this image:
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
Can anyone let me know where I'm going wrong or how to achieve this?
Firstly Me
is 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.
Secondly I think that your definition of the API function is a little off.
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
and used the definition defined here: Get user attention without stealing focus
both of which may be useful references to you
If you were building a Windows Forms application, Me.Handle
would be the way to go, but that looks like a console application, in which case try using Process.GetCurrentProcess().MainWindowHandle
instead of Me.Handle. See this question for further details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With