view ServerMonitor/Win32Helpers.cs @ 39:7645122aa7a9

Get it working under Mono
author Brad Greco <brad@bgreco.net>
date Tue, 09 Jun 2020 20:59:00 -0400
parents 68d7834dc28e
children
line wrap: on
line source

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ServerMonitorApp
{
    /// <summary>Methods for interacting with the Win32 API.</summary>
    class Win32Helpers
    {
        [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 int HWND_BROADCAST = 0xffff;

        #if __MonoCS__
            public static readonly int WM_SHOWMONITOR = -12345;
        #else
            public static readonly int WM_SHOWMONITOR = RegisterWindowMessage("WM_SHOWMONITOR");
        #endif

        [DllImport("user32")]
        public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
        [DllImport("user32")]
        public static extern int RegisterWindowMessage(string message);

        public enum FlashWindowFlags : uint
        {
            /// <summary>
            /// Stop flashing. The system restores the window to its original state.
            /// </summary>
            FLASHW_STOP = 0,

            /// <summary>
            /// Flash the window caption
            /// </summary>
            FLASHW_CAPTION = 1,

            /// <summary>
            /// Flash the taskbar button.
            /// </summary>
            FLASHW_TRAY = 2,

            /// <summary>
            /// Flash both the window caption and taskbar button.
            /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
            /// </summary>
            FLASHW_ALL = 3,

            /// <summary>
            /// Flash continuously, until the FLASHW_STOP flag is set.
            /// </summary>
            FLASHW_TIMER = 4,

            /// <summary>
            /// Flash continuously until the window comes to the foreground.
            /// </summary>
            FLASHW_TIMERNOFG = 12
        }

        /// <summary>Flashes a window icon in the taskbar.</summary>
        /// <param name="form">The form to flash.</param>
        public static bool FlashWindowEx(Form form)
        {
            if (Helpers.IsMono()) {
                return false;
            }

            IntPtr hWnd = form.Handle;
            FLASHWINFO fInfo = new FLASHWINFO();

            fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
            fInfo.hwnd = hWnd;
            fInfo.dwFlags = (uint)FlashWindowFlags.FLASHW_TRAY;
            fInfo.uCount = 10;
            fInfo.dwTimeout = 0;

            return FlashWindowEx(ref fInfo);
        }

        /// <summary>Stops flashing a window icon in the taskbar.</summary>
        /// <param name="form">The form to stop flashing.</param>
        public static bool StopFlashWindowEx(Form form)
        {
            if (Helpers.IsMono()) {
                return false;
            }

            IntPtr hWnd = form.Handle;
            FLASHWINFO fInfo = new FLASHWINFO();

            fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
            fInfo.hwnd = hWnd;
            fInfo.dwFlags = (uint)FlashWindowFlags.FLASHW_STOP;
            fInfo.dwTimeout = 0;

            return FlashWindowEx(ref fInfo);
        }
    }
}