Search code examples
c#winformsshowdialog

the showdialog refocus effect custom call


Is there a way to let the form blink, like the ShowDialog when it loses focus?

When you call ShowDialog on a windows form and you try another action the form blinks for a few seconds and then focuses.

Is there a way to call that blinking action in a custom way?


Solution

  • Try this.

        [DllImport("user32.dll")]
        private static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi);
    
        [StructLayout(LayoutKind.Sequential)]
        private struct FLASHWINFO {
            public UInt32 cbSize;
            public IntPtr hwnd;
            public FLASHW dwFlags;
            public UInt32 uCount;
            public Int32 dwTimeout;
        }
    
        [Flags]
        private enum FLASHW: int {
            // stop flashing
            FLASHW_STOP = 0x00,
            // flash the window title
            FLASHW_CAPTION = 0x01,
            // flash the taskbar button
            FLASHW_TRAY = 0x02,
            // flash the window title and the taskbar button
            FLASHW_ALL = 0x03,
            // flash continuously
            FLASHW_TIMER = 0x04,
            // flash until the window comes to the foreground
            FLASHW_TIMERNOFG = 0x0c,
        }
    
        public static void FlashWindow(IWin32Window form, int count) {
            FLASHWINFO pwfi = new FLASHWINFO();
            pwfi.cbSize = (uint)Marshal.SizeOf(pwfi);
            pwfi.dwFlags = FLASHW.FLASHW_ALL;
            pwfi.dwTimeout = 0;
            pwfi.hwnd = form.Handle;
            pwfi.uCount = (uint)count;
            FlashWindowEx(ref pwfi);
        }
    

    Extend it for your needs.