3

I have a TCP Chat application. When a new message arrives, I want to make the taskbar glow until the user opens the form again (in case it wasn't focus/activated).

An example of what I mean: http://puu.sh/4z01n.png

How can I make it glow like that?

Thanks!

If you still don't understand what I mean.. The image I provided is what appears ON the icon i nthe taskbar when something "glows". Meaning there is a notifcation. That's what I want to acheive.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 5
    Do you mean [this](http://stackoverflow.com/questions/9970207/how-to-let-a-minimized-form-to-notify-user-to-open-it-from-taskbar)? – Nico Schertler Sep 24 '13 at 10:52
  • 1
    Possible duplicate of [Window application flash like orange on taskbar when minimize](https://stackoverflow.com/questions/11309827/window-application-flash-like-orange-on-taskbar-when-minimize) – Jim Fell Jun 06 '19 at 13:42

3 Answers3

6

I hope this will help you

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


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


[Flags]
        public enum FlashMode {
            /// 
            /// 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
        }

        public static bool FlashWindow(IntPtr hWnd, FlashMode fm) {
            FLASHWINFO fInfo = new FLASHWINFO();

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

            return FlashWindowEx(ref fInfo);
        }

Taken from my wiki at Flashing Taskbar

Menelaos Vergis
  • 3,715
  • 5
  • 30
  • 46
2

You need some interoperability to achieve this,first of all add System.Runtime.InteropServices namespace to your class,the in you class define this function at the beginning,

    [DllImport("user32.dll")]
    static extern bool FlashWindow(IntPtr hwnd, bool FlashStatus);

It is an API function and it's description says, The FlashWindow function flashes the specified window once..Then add a Timer to your class(drag-drop it from the toolbox,set its interval to 500 Milliseconds).Then assuming Form1 is the window you want to flash,use the following code to achieve this;

    private void Form1_Activated(object sender, EventArgs e)
    {
        timer1.Stop();//Stop the timer to stop flashing.
    }

    private void Form1_Deactivate(object sender, EventArgs e)
    {
        timer1.Start();//Start timer if window loses focus.
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        FlashWindow(this.Handle, true);//Flash the window untill it gets focused.
    }

Well,call timer1.Start(); when a new message arrives.A sample just in case if you need.

Hope this helps you.

devavx
  • 1,035
  • 9
  • 22
  • For some reason, it's still flashing evne though I am focusing the window. Do I have to set the Focused proeprty or something?.. – user2810715 Sep 24 '13 at 11:31
0

You can use an extension method that will Flash the window until it receives focus, without using a timer.

Just call

    form.FlashNotification();


    public static class ExtensionMethods
        {
            [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

            private const uint FLASHW_ALL = 3;

            private const uint FLASHW_TIMERNOFG = 12;

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

            public static bool FlashNotification(this Form form)
            {
                IntPtr hWnd = form.Handle;
                FLASHWINFO fInfo = new FLASHWINFO();

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

                return FlashWindowEx(ref fInfo);
            }
        }
}