view ServerMonitor/Win32Helpers.cs @ 26:b5502ce8cb1f

Fix servers getting disabled at program launch if no SSH key is set, even if they have no SSH checks.
author Brad Greco <brad@bgreco.net>
date Sun, 02 Jun 2019 17:45:37 -0400
parents 68d7834dc28e
children 7645122aa7a9
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;
        public static readonly int WM_SHOWMONITOR = RegisterWindowMessage("WM_SHOWMONITOR");
        [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)
        {
            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)
        {
            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);
        }
    }
}