using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Windows; using bts; namespace mbts.ui.native { public static class NativeMethods { [DllImport("user32.dll")] public static extern bool PostThreadMessage(int threadid, uint msg, IntPtr wparam, IntPtr lparam); [DllImport("user32.dll")] public static extern bool GetMessage(ref MSG msg, IntPtr hwnd, uint filter_mind, uint filter_max); [DllImport("user32.dll")] public static extern bool DispatchMessage(ref MSG msg); [DllImport("user32.dll")] public static extern bool TranslateMessage(ref MSG msg); #region Windows Message Loop Stuff [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32")] public static extern IntPtr SendNotifyMessage(IntPtr hwnd, WM msg, Int64 wparam, Int64 lparam); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr PostMessage(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll")] public static extern bool AllocConsole(); public delegate IntPtr WndProcDelegate(IntPtr hwnd, uint msg, IntPtr wparam, IntPtr lparam); [DllImport("user32.dll")] public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, GWL nIndex, WndProcDelegate dwNewLong); [DllImport("user32.dll")] public static extern IntPtr SetWindowLongPtr(IntPtr hwnd, GWL index, WS_EX style); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex); [DllImport("user32.dll")] public static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); public delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(HookType hookType, IntPtr lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", SetLastError = true)] public static extern bool UnhookWindowsHookEx(IntPtr hhook); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr CallNextHookEx(IntPtr hhk, int code, IntPtr wparam, IntPtr lparam); [StructLayout(LayoutKind.Sequential)] public struct WINDOWPOS { public IntPtr hwnd; public IntPtr hwndInsertAfter; public int x; public int y; public int cx; public int cy; public int flags; public override string ToString() { return "{0},{1} {2},{3}".fmt(x, y, cx, cy); } } [DllImport("kernel32.dll")] public static extern uint GetCurrentThreadId(); #endregion #region Window Flashing API Stuff private const UInt32 FLASHW_STOP = 0; //Stop flashing. The system restores the window to its original state. private const UInt32 FLASHW_CAPTION = 1; //Flash the window caption. private const UInt32 FLASHW_TRAY = 2; //Flash the taskbar button. private const UInt32 FLASHW_ALL = 3; //Flash both the window caption and taskbar button. private const UInt32 FLASHW_TIMER = 4; //Flash continuously, until the FLASHW_STOP flag is set. private const UInt32 FLASHW_TIMERNOFG = 12; //Flash continuously until the window comes to the foreground. [StructLayout(LayoutKind.Sequential)] private struct FLASHWINFO { public UInt32 cbSize; //The size of the structure in bytes. public IntPtr hwnd; //A Handle to the Window to be Flashed. The window can be either opened or minimized. public UInt32 dwFlags; //The Flash Status. public UInt32 uCount; // number of times to flash the window public UInt32 dwTimeout; //The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate. } [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool FlashWindowEx(ref FLASHWINFO pwfi); public static void FlashWindow(IntPtr hwnd, uint count = UInt32.MaxValue) { FLASHWINFO fwi = new FLASHWINFO() { hwnd = hwnd, dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG, uCount = count, dwTimeout = 0, }; fwi.cbSize = (uint)Marshal.SizeOf(fwi); bool ret = FlashWindowEx(ref fwi); Console.WriteLine(ret); } #endregion public static System.Windows.Point GetCursorPos() { Win32Point pt; GetCursorPos(out pt); return new System.Windows.Point(pt.x, pt.y); } [DllImport("user32.dll")] public static extern bool GetCursorPos(out Win32Point pt); public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); public static readonly IntPtr HWND_TOP = new IntPtr(0); public static readonly IntPtr HWND_BOTTOM = new IntPtr(1); [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwnd_insert_after, int x, int y, int cx, int cy, SWP flags); [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hwnd, out Win32Rect lpRect); [DllImport("user32")] public static extern bool GetWindowPlacement(IntPtr hwnd, ref WINDOWPLACEMENT lpwndpl); public static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff); public static readonly WM WM_SHOWME = RegisterWindowMessage("SHOW_ME"); public static readonly WM WM_SHELL_TERMINATE = RegisterWindowMessage("SHELL_TERMINATE"); public static readonly WM WM_URL = RegisterWindowMessage("BTS_URL"); [DllImport("user32.dll")] public static extern WM RegisterWindowMessage(string name); [DllImport("user32")] public static extern IntPtr DefWindowProc(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam); [DllImport("dwmapi", PreserveSig = true)] public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref Win32Rect margins); #region Bitmap / Device context related [DllImport("user32")] public static extern IntPtr GetDC(IntPtr hwnd); [DllImport("user32")] public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc); [DllImport("gdi32")] public static extern IntPtr CreateBitmap(int width, int height, uint planes, uint bits_per_pixel, IntPtr bits); [DllImport("gdi32")] public static extern bool DeleteObject(IntPtr hObject); [DllImport("gdi32")] public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj); [DllImport("gdi32")] public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); [DllImport("gdi32")] public static extern IntPtr CreateCompatibleDC(IntPtr hdc); [DllImport("gdi32")] public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop = 0x00CC0020/*SRCCOPY*/); [DllImport("gdi32")] public static extern int SetStretchBltMode(IntPtr hdc, int mode); [DllImport("gdi32")] public static extern bool StretchBlt(IntPtr hdc_dest, int x_dest, int y_dest, int width_dest, int height_dest, IntPtr hdc_src, int x_src, int y_src, int width_src, int height_src, int dwRop = 0x00CC0020/*SRCCOPY*/); #endregion #region Monitor/Workarea related public const int MONITOR_DEFAULTONPRIMARY = 1; public const int MONITOR_DEFAULTONNEAREST = 2; [DllImport("user32")] public static extern IntPtr MonitorFromWindow(IntPtr hwnd, int flags); [DllImport("user32")] public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi); [DllImport("user32", EntryPoint = "SystemParametersInfo", SetLastError=true)] private static extern bool SetWorkArea_SystemParametersInfo(SPI action, uint zero, Win32Rect param, uint winini); public static void SetWorkArea(Rect workarea) { var dpi_ratio = config.Configuration.get().dpi_ratio; workarea = new Rect(workarea.Left * dpi_ratio, workarea.Top * dpi_ratio, (workarea.Right - workarea.Left) * dpi_ratio, (workarea.Bottom - workarea.Top) * dpi_ratio); //const uint SPIF_SENDWININICHANGE = 2; const uint SPIF_UPDATEINIFILE = 1; Win32Rect rect = new Win32Rect(workarea); // Sending SPIF_SENDWININICHANGE causes this to block until all top level windows have acknowledged the settings change // Since we want to do that during shutdown this could cause a long delay. bool result = SetWorkArea_SystemParametersInfo(SPI.SETWORKAREA, 1, rect, SPIF_UPDATEINIFILE); if (!result) { Log.Ui.error("Failed to set work area. {0} - {1}".fmt(Marshal.GetLastWin32Error(), new System.ComponentModel.Win32Exception().Message)); } SendNotifyMessage(HWND_BROADCAST, WM.SETTINGCHANGE, (Int64)SPI.SETWORKAREA, 0); } // Get the work area for monitor where the specified window is currently. // The work area is the area of the window not covered by the task bar or a docked shell public static Rect GetWorkArea(IntPtr window) { var dpi_ratio = config.Configuration.get().dpi_ratio; IntPtr monitor = MonitorFromWindow(window, MONITOR_DEFAULTONNEAREST); MONITORINFO info = new MONITORINFO(); info.cbSize = Marshal.SizeOf(info); GetMonitorInfo(monitor, ref info); return new Rect(info.rcWork.Left/dpi_ratio, info.rcWork.Top/dpi_ratio, (info.rcWork.Right - info.rcWork.Left)/dpi_ratio, (info.rcWork.Bottom - info.rcWork.Top)/dpi_ratio); } #endregion [DllImport("user32")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32")] public static extern bool IsWindow(IntPtr handle); } public enum GWL { WNDPROC = -4, EXSTYLE = -20, } public enum WS_EX { TRANSPARENT = 0x20 } [Flags] public enum SWP : uint { SWP_NOSIZE = 1, SWP_NOMOVE = 2, SWP_NOZORDER = 0x0004, SWP_NOREDRAW = 0x0008, SWP_NOACTIVATE = 0x0010, // The frame changed: send WM_NCCALCSIZE SWP_FRAMECHANGED = 0x0020, SWP_SHOWWINDOW = 0x0040, SWP_HIDEWINDOW = 0x0080, SWP_NOCOPYBITS = 0x0100, // Don’t do owner Z ordering SWP_NOOWNERZORDER = 0x0200, // Don’t send WM_WINDOWPOSCHANGING SWP_NOSENDCHANGING = 0x0400, TOPMOST_FLAGS = SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSENDCHANGING } public enum SPI : uint { GETBEEP = 0x0001, SETBEEP = 0x0002, GETMOUSE = 0x0003, SETMOUSE = 0x0004, GETBORDER = 0x0005, SETBORDER = 0x0006, GETKEYBOARDSPEED = 0x000A, SETKEYBOARDSPEED = 0x000B, LANGDRIVER = 0x000C, ICONHORIZONTALSPACING = 0x000D, GETSCREENSAVETIMEOUT = 0x000E, SETSCREENSAVETIMEOUT = 0x000F, GETSCREENSAVEACTIVE = 0x0010, SETSCREENSAVEACTIVE = 0x0011, GETGRIDGRANULARITY = 0x0012, SETGRIDGRANULARITY = 0x0013, SETDESKWALLPAPER = 0x0014, SETDESKPATTERN = 0x0015, GETKEYBOARDDELAY = 0x0016, SETKEYBOARDDELAY = 0x0017, ICONVERTICALSPACING = 0x0018, GETICONTITLEWRAP = 0x0019, SETICONTITLEWRAP = 0x001A, GETMENUDROPALIGNMENT = 0x001B, SETMENUDROPALIGNMENT = 0x001C, SETDOUBLECLKWIDTH = 0x001D, SETDOUBLECLKHEIGHT = 0x001E, GETICONTITLELOGFONT = 0x001F, SETDOUBLECLICKTIME = 0x0020, SETMOUSEBUTTONSWAP = 0x0021, SETICONTITLELOGFONT = 0x0022, GETFASTTASKSWITCH = 0x0023, SETFASTTASKSWITCH = 0x0024, SETDRAGFULLWINDOWS = 0x0025, GETDRAGFULLWINDOWS = 0x0026, GETNONCLIENTMETRICS = 0x0029, SETNONCLIENTMETRICS = 0x002A, GETMINIMIZEDMETRICS = 0x002B, SETMINIMIZEDMETRICS = 0x002C, GETICONMETRICS = 0x002D, SETICONMETRICS = 0x002E, SETWORKAREA = 0x002F, GETWORKAREA = 0x0030, SETPENWINDOWS = 0x0031, GETHIGHCONTRAST = 0x0042, SETHIGHCONTRAST = 0x0043, GETKEYBOARDPREF = 0x0044, SETKEYBOARDPREF = 0x0045, GETSCREENREADER = 0x0046, SETSCREENREADER = 0x0047, GETANIMATION = 0x0048, SETANIMATION = 0x0049, GETFONTSMOOTHING = 0x004A, SETFONTSMOOTHING = 0x004B, SETDRAGWIDTH = 0x004C, SETDRAGHEIGHT = 0x004D, SETHANDHELD = 0x004E, GETLOWPOWERTIMEOUT = 0x004F, GETPOWEROFFTIMEOUT = 0x0050, SETLOWPOWERTIMEOUT = 0x0051, SETPOWEROFFTIMEOUT = 0x0052, GETLOWPOWERACTIVE = 0x0053, GETPOWEROFFACTIVE = 0x0054, SETLOWPOWERACTIVE = 0x0055, SETPOWEROFFACTIVE = 0x0056, SETCURSORS = 0x0057, SETICONS = 0x0058, GETDEFAULTINPUTLANG = 0x0059, SETDEFAULTINPUTLANG = 0x005A, SETLANGTOGGLE = 0x005B, GETWINDOWSEXTENSION = 0x005C, SETMOUSETRAILS = 0x005D, GETMOUSETRAILS = 0x005E, SETSCREENSAVERRUNNING = 0x0061, SCREENSAVERRUNNING = SETSCREENSAVERRUNNING, GETFILTERKEYS = 0x0032, SETFILTERKEYS = 0x0033, GETTOGGLEKEYS = 0x0034, SETTOGGLEKEYS = 0x0035, GETMOUSEKEYS = 0x0036, SETMOUSEKEYS = 0x0037, GETSHOWSOUNDS = 0x0038, SETSHOWSOUNDS = 0x0039, GETSTICKYKEYS = 0x003A, SETSTICKYKEYS = 0x003B, GETACCESSTIMEOUT = 0x003C, SETACCESSTIMEOUT = 0x003D, GETSERIALKEYS = 0x003E, SETSERIALKEYS = 0x003F, GETSOUNDSENTRY = 0x0040, SETSOUNDSENTRY = 0x0041, GETSNAPTODEFBUTTON = 0x005F, SETSNAPTODEFBUTTON = 0x0060, GETMOUSEHOVERWIDTH = 0x0062, SETMOUSEHOVERWIDTH = 0x0063, GETMOUSEHOVERHEIGHT = 0x0064, SETMOUSEHOVERHEIGHT = 0x0065, GETMOUSEHOVERTIME = 0x0066, SETMOUSEHOVERTIME = 0x0067, GETWHEELSCROLLLINES = 0x0068, SETWHEELSCROLLLINES = 0x0069, GETMENUSHOWDELAY = 0x006A, SETMENUSHOWDELAY = 0x006B, GETWHEELSCROLLCHARS = 0x006C, SETWHEELSCROLLCHARS = 0x006D, GETSHOWIMEUI = 0x006E, SETSHOWIMEUI = 0x006F, GETMOUSESPEED = 0x0070, SETMOUSESPEED = 0x0071, GETSCREENSAVERRUNNING = 0x0072, GETDESKWALLPAPER = 0x0073, GETAUDIODESCRIPTION = 0x0074, SETAUDIODESCRIPTION = 0x0075, GETSCREENSAVESECURE = 0x0076, SETSCREENSAVESECURE = 0x0077, GETHUNGAPPTIMEOUT = 0x0078, SETHUNGAPPTIMEOUT = 0x0079, GETWAITTOKILLTIMEOUT = 0x007A, SETWAITTOKILLTIMEOUT = 0x007B, GETWAITTOKILLSERVICETIMEOUT = 0x007C, SETWAITTOKILLSERVICETIMEOUT = 0x007D, GETMOUSEDOCKTHRESHOLD = 0x007E, SETMOUSEDOCKTHRESHOLD = 0x007F, GETPENDOCKTHRESHOLD = 0x0080, SETPENDOCKTHRESHOLD = 0x0081, GETWINARRANGING = 0x0082, SETWINARRANGING = 0x0083, GETMOUSEDRAGOUTTHRESHOLD = 0x0084, SETMOUSEDRAGOUTTHRESHOLD = 0x0085, GETPENDRAGOUTTHRESHOLD = 0x0086, SETPENDRAGOUTTHRESHOLD = 0x0087, GETMOUSESIDEMOVETHRESHOLD = 0x0088, SETMOUSESIDEMOVETHRESHOLD = 0x0089, GETPENSIDEMOVETHRESHOLD = 0x008A, SETPENSIDEMOVETHRESHOLD = 0x008B, GETDRAGFROMMAXIMIZE = 0x008C, SETDRAGFROMMAXIMIZE = 0x008D, GETSNAPSIZING = 0x008E, SETSNAPSIZING = 0x008F, GETDOCKMOVING = 0x0090, SETDOCKMOVING = 0x0091, GETACTIVEWINDOWTRACKING = 0x1000, SETACTIVEWINDOWTRACKING = 0x1001, GETMENUANIMATION = 0x1002, SETMENUANIMATION = 0x1003, GETCOMBOBOXANIMATION = 0x1004, SETCOMBOBOXANIMATION = 0x1005, GETLISTBOXSMOOTHSCROLLING = 0x1006, SETLISTBOXSMOOTHSCROLLING = 0x1007, GETGRADIENTCAPTIONS = 0x1008, SETGRADIENTCAPTIONS = 0x1009, GETKEYBOARDCUES = 0x100A, SETKEYBOARDCUES = 0x100B, GETMENUUNDERLINES = GETKEYBOARDCUES, SETMENUUNDERLINES = SETKEYBOARDCUES, GETACTIVEWNDTRKZORDER = 0x100C, SETACTIVEWNDTRKZORDER = 0x100D, GETHOTTRACKING = 0x100E, SETHOTTRACKING = 0x100F, GETMENUFADE = 0x1012, SETMENUFADE = 0x1013, GETSELECTIONFADE = 0x1014, SETSELECTIONFADE = 0x1015, GETTOOLTIPANIMATION = 0x1016, SETTOOLTIPANIMATION = 0x1017, GETTOOLTIPFADE = 0x1018, SETTOOLTIPFADE = 0x1019, GETCURSORSHADOW = 0x101A, SETCURSORSHADOW = 0x101B, GETMOUSESONAR = 0x101C, SETMOUSESONAR = 0x101D, GETMOUSECLICKLOCK = 0x101E, SETMOUSECLICKLOCK = 0x101F, GETMOUSEVANISH = 0x1020, SETMOUSEVANISH = 0x1021, GETFLATMENU = 0x1022, SETFLATMENU = 0x1023, GETDROPSHADOW = 0x1024, SETDROPSHADOW = 0x1025, GETBLOCKSENDINPUTRESETS = 0x1026, SETBLOCKSENDINPUTRESETS = 0x1027, GETUIEFFECTS = 0x103E, SETUIEFFECTS = 0x103F, GETDISABLEOVERLAPPEDCONTENT = 0x1040, SETDISABLEOVERLAPPEDCONTENT = 0x1041, GETCLIENTAREAANIMATION = 0x1042, SETCLIENTAREAANIMATION = 0x1043, GETCLEARTYPE = 0x1048, SETCLEARTYPE = 0x1049, GETSPEECHRECOGNITION = 0x104A, SETSPEECHRECOGNITION = 0x104B, GETFOREGROUNDLOCKTIMEOUT = 0x2000, SETFOREGROUNDLOCKTIMEOUT = 0x2001, GETACTIVEWNDTRKTIMEOUT = 0x2002, SETACTIVEWNDTRKTIMEOUT = 0x2003, GETFOREGROUNDFLASHCOUNT = 0x2004, SETFOREGROUNDFLASHCOUNT = 0x2005, GETCARETWIDTH = 0x2006, SETCARETWIDTH = 0x2007, GETMOUSECLICKLOCKTIME = 0x2008, SETMOUSECLICKLOCKTIME = 0x2009, GETFONTSMOOTHINGTYPE = 0x200A, SETFONTSMOOTHINGTYPE = 0x200B, GETFONTSMOOTHINGCONTRAST = 0x200C, SETFONTSMOOTHINGCONTRAST = 0x200D, GETFOCUSBORDERWIDTH = 0x200E, SETFOCUSBORDERWIDTH = 0x200F, GETFOCUSBORDERHEIGHT = 0x2010, SETFOCUSBORDERHEIGHT = 0x2011, GETFONTSMOOTHINGORIENTATION = 0x2012, SETFONTSMOOTHINGORIENTATION = 0x2013, GETMINIMUMHITRADIUS = 0x2014, SETMINIMUMHITRADIUS = 0x2015, GETMESSAGEDURATION = 0x2016, SETMESSAGEDURATION = 0x2017, } #region supporting structures [StructLayout(LayoutKind.Sequential)] public struct CWPSTRUCT { public IntPtr lparam; public IntPtr wparam; public WM message; public IntPtr hwnd; } [StructLayout(LayoutKind.Sequential)] public struct DEBUGHOOKINFO { public uint idThread; public uint idThreadInstaller; public IntPtr lParam; public IntPtr wParam; public int code; }; [StructLayout(LayoutKind.Sequential)] public struct MSG { public IntPtr hwnd; public WM msg; public IntPtr wparam; public IntPtr lparam; public uint time; } public enum HookType { WH_GETMESSAGE = 3, WH_CALLWNDPROC = 4, WH_CALLWNDPROCRET = 12, WH_DEBUG = 9, WH_MOUSE = 7, WH_SHELL = 10, } public enum SysCommand { SC_CLOSE = 0xf060, SC_MINIMIZE = 0XF020, SC_MAXIMIZE = 0xF030, SC_RESTORE = 0xF120, } [StructLayout(LayoutKind.Sequential)] public struct Win32Point { public override string ToString() { return x + "," + y; } public Int32 x; public Int32 y; } [StructLayout(LayoutKind.Sequential)] public struct Win32Rect { public int Left; public int Top; public int Right; public int Bottom; public Win32Rect(Rect r) { Left = (int)r.Left; Top = (int)r.Top; Right = ((int)(r.Left + r.Width)); Bottom = ((int)(r.Top + r.Height)); } public int Width { get { return Right - Left; } set { Right = Left + value; } } public int Height { get { return Bottom - Top; } set { Bottom = Top = value; } } } [StructLayout(LayoutKind.Sequential)] public struct WINDOWPLACEMENT { public int Length; public int Flags; public int showCmd; public Win32Point MinPosition; public Win32Point MaxPosition; public Win32Rect NormalPosition; public static WINDOWPLACEMENT Default { get { var result = new WINDOWPLACEMENT(); result.Length = Marshal.SizeOf(result); return result; } } } [StructLayout(LayoutKind.Sequential)] public struct MINMAXINFO { public Win32Point ptReserved; public Win32Point ptMaxSize; public Win32Point ptMaxPosition; public Win32Point ptMinTrackSize; public Win32Point ptMaxTrackSize; } [StructLayout(LayoutKind.Sequential)] public struct MONITORINFO { public int cbSize; public Win32Rect rcMonitor; public Win32Rect rcWork; public uint dwFlags; } #endregion }