How to make the taskbar blink my application like Messenger does when a new message arrive?
Asked Answered
F

5

20

Is there an API call in .NET or a native DLL that I can use to create similar behaviour as Windows Live Messenger when a response comes from someone I chat with?

Fuse answered 16/9, 2008 at 14:50 Comment(0)
M
23

FlashWindowEx is the way to go. See here for MSDN documentation

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
    public UInt32 cbSize;
    public IntPtr hwnd;
    public UInt32 dwFlags;
    public UInt32 uCount;
    public UInt32 dwTimeout;
}

public const UInt32 FLASHW_ALL = 3; 

Calling the Function:

FLASHWINFO fInfo = new FLASHWINFO();

fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
fInfo.hwnd = hWnd;
fInfo.dwFlags = FLASHW_ALL;
fInfo.uCount = UInt32.MaxValue;
fInfo.dwTimeout = 0;

FlashWindowEx(ref fInfo);

This was shamelessly plugged from Pinvoke.net

Mendacity answered 16/9, 2008 at 15:10 Comment(5)
For WPF applications, set fInfo.hwnd to new System.Windows.Interop.WindowInteropHelper(this).HandleGangboard
I am using this on a console app. It starts flashing, but how do I get it to stop once the app is clicked from the taskbar (ie, whenever the window has the focus)?Shortly
@Shortly you can set uCount to the # of times you want it to flash, that won't stop it flashing but at least it will stop eventually. you could also look into using FlashWindow which is simpler and just flashes it once: msdn.microsoft.com/en-us/library/windows/desktop/…Olds
@dummy, do you know of a way to do something similar for cmd.exe? I want the cmd.exe taskbar button to flash after a long running cmd script is complete.Pika
@Shortly you might have figured this out already, but I'll add it here anyways if anyone else has the problem. You can add: public const UInt32 FLASHW_TIMERNOFG = 12; fInfo.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG; This will make the window stop flashing when the window is focused.Rational
H
4
HWND hHandle = FindWindow(NULL,"YourApplicationName");
FLASHWINFO pf;
pf.cbSize = sizeof(FLASHWINFO);
pf.hwnd = hHandle;
pf.dwFlags = FLASHW_TIMER|FLASHW_TRAY; // (or FLASHW_ALL to flash and if it is not minimized)
pf.uCount = 8;
pf.dwTimeout = 75;

FlashWindowEx(&pf);

Stolen from experts-exchange member gtokas.

FlashWindowEx.

Hellfire answered 16/9, 2008 at 14:53 Comment(0)
C
3

From a Raymond Chen blog entry:

How do I flash my window caption and taskbar button manually?

How do I flash my window caption and taskbar button manually? Commenter Jonathan Scheepers wonders about those programs that flash their taskbar button indefinitely, overriding the default flash count set by SysteParametersInfo(SPI_SETFOREGROUNDFLASHCOUNT).

The FlashWindowEx function and its simpler precursor FlashWindow let a program flash its window caption and taskbar button manually. The window manager flashes the caption automatically (and Explorer follows the caption by flashing the taskbar button) if a program calls SetForegroundWindow when it doesn't have permission to take foreground, and it is that automatic flashing that the SPI_SETFOREGROUNDFLASHCOUNT setting controls.

For illustration purposes, I'll demonstrate flashing the caption manually. This is generally speaking not recommended, but since you asked, I'll show you how. And then promise you won't do it.

Start with the scratch program and make this simple change:

void
OnSize(HWND hwnd, UINT state, int cx, int cy)
{
  if (state == SIZE_MINIMIZED) {
    FLASHWINFO fwi = { sizeof(fwi), hwnd,
                       FLASHW_TIMERNOFG | FLASHW_ALL };
    FlashWindowEx(&fwi);
  }
}

Compile and run this program, then minimize it. When you do, its taskbar button flashes indefinitely until you click on it. The program responds to being minimzed by calling the FlashWindowEx function asking for everything possible (currently the caption and taskbar button) to be flashed until the window comes to the foreground.

Other members of the FLASHWINFO structure let you customize the flashing behavior further, such as controlling the flash frequency and the number of flashes. and if you really want to take control, you can use FLASHW_ALL and FLASHW_STOP to turn your caption and taskbar button on and off exactly the way you want it. (Who knows, maybe you want to send a message in Morse code.)

Published Monday, May 12, 2008 7:00 AM by oldnewthing Filed under: Code

Callida answered 16/9, 2008 at 14:59 Comment(0)
W
2

The FlashWindowEx Win32 API is the call used to do this. The documentation for it is at: http://msdn.microsoft.com/en-us/library/ms679347(VS.85).aspx

Wheeler answered 16/9, 2008 at 14:53 Comment(1)
Only available on WinXP, Win2KPro and VistaLipkin
L
0

I believe you're looking for SetForegroundWindow.

Lipkin answered 16/9, 2008 at 14:53 Comment(1)
Available on all Windows OSes but behaviour changes over time.Lipkin

© 2022 - 2024 — McMap. All rights reserved.