123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- using System;
- using System.Runtime.InteropServices;
- using System.Security;
- using System.Windows.Forms;
- namespace SmartCoalApplication.SystemLayer
- {
- [SuppressUnmanagedCodeSecurity]
- public static class SafeNativeMethods
- {
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool DrawMenuBar(IntPtr hWnd);
- [DllImport("user32.dll", SetLastError = false)]
- internal static extern IntPtr GetSystemMenu(
- IntPtr hWnd,
- [MarshalAs(UnmanagedType.Bool)] bool bRevert);
- [DllImport("user32.dll", SetLastError = false)]
- internal static extern int EnableMenuItem(
- IntPtr hMenu,
- uint uIDEnableItem,
- uint uEnable);
- [DllImport("user32.dll", SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool FlashWindow(
- IntPtr hWnd,
- [MarshalAs(UnmanagedType.Bool)] bool bInvert);
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool ShowScrollBar(
- IntPtr hWnd,
- int wBar,
- [MarshalAs(UnmanagedType.Bool)] bool bShow);
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool GetLayeredWindowAttributes(
- IntPtr hwnd,
- out uint pcrKey,
- out byte pbAlpha,
- out uint pdwFlags);
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", MessageId = "2")]
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool SetLayeredWindowAttributes(
- IntPtr hwnd,
- uint crKey,
- byte bAlpha,
- uint dwFlags);
- [DllImport("gdi32.dll", SetLastError = true)]
- internal static extern IntPtr CreateDIBSection(
- IntPtr hdc,
- ref NativeStructs.BITMAPINFO pbmi,
- uint iUsage,
- out IntPtr ppvBits,
- IntPtr hSection,
- uint dwOffset);
- [DllImport("user32.dll", SetLastError = false)]
- internal static extern int GetUpdateRgn(
- IntPtr hWnd,
- IntPtr hRgn,
- [MarshalAs(UnmanagedType.Bool)] bool bErase);
- [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- internal static extern IntPtr FindWindowExW(
- IntPtr hwndParent,
- IntPtr hwndChildAfter,
- [MarshalAs(UnmanagedType.LPWStr)] string lpszClass,
- [MarshalAs(UnmanagedType.LPWStr)] string lpszWindow);
- [DllImport("user32.dll", SetLastError = false)]
- internal static extern IntPtr SendMessageW(
- IntPtr hWnd,
- uint msg,
- IntPtr wParam,
- IntPtr lParam);
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool PostMessageW(
- IntPtr handle,
- uint msg,
- IntPtr wParam,
- IntPtr lParam);
- [DllImport("user32.dll", SetLastError = true)]
- internal static extern uint GetWindowLongW(
- IntPtr hWnd,
- int nIndex);
- [DllImport("user32.dll", SetLastError = true)]
- internal static extern uint SetWindowLongW(
- IntPtr hWnd,
- int nIndex,
- uint dwNewLong);
- [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- internal static extern unsafe void memcpy(void* dst, void* src, UIntPtr length);
- [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- internal static extern unsafe void memset(void* dst, int c, UIntPtr length);
- [DllImport("User32.dll", SetLastError = false)]
- internal static extern int GetSystemMetrics(int nIndex);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool QueryPerformanceCounter(out ulong lpPerformanceCount);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool QueryPerformanceFrequency(out ulong lpFrequency);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool CloseHandle(IntPtr hObject);
- [DllImport("kernel32.dll", SetLastError = true)]
- internal static extern uint WaitForSingleObject(IntPtr hHandle,uint dwMilliseconds);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool SetHandleInformation(
- IntPtr hObject,
- uint dwMask,
- uint dwFlags);
- [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- internal static extern IntPtr FindWindowW(
- [MarshalAs(UnmanagedType.LPWStr)] string lpClassName,
- [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName);
- [DllImport("user32.dll", SetLastError = false)]
- internal static extern uint GetWindowThreadProcessId(
- IntPtr hWnd,
- out uint lpdwProcessId);
- [DllImport("kernel32.dll", SetLastError = true)]
- internal static extern uint WaitForMultipleObjects(
- uint nCount,
- IntPtr[] lpHandles,
- [MarshalAs(UnmanagedType.Bool)] bool bWaitAll,
- uint dwMilliseconds);
- [DllImport("Gdi32.Dll", SetLastError = true)]
- public extern static IntPtr CreateCompatibleDC(IntPtr hdc);
- [DllImport("Gdi32.dll", SetLastError = true)]
- public extern static uint DeleteDC(IntPtr hdc);
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool GetVersionEx(ref NativeStructs.OSVERSIONINFOEX lpVersionInfo);
- internal static uint WaitForMultipleObjects(IntPtr[] lpHandles, bool bWaitAll, uint dwMilliseconds)
- {
- return WaitForMultipleObjects((uint)lpHandles.Length, lpHandles, bWaitAll, dwMilliseconds);
- }
- [DllImport("wtsapi32.dll", SetLastError = true)]
- internal static extern uint WTSRegisterSessionNotification(IntPtr hWnd, uint dwFlags);
- [DllImport("wtsapi32.dll", SetLastError = true)]
- internal static extern uint WTSUnRegisterSessionNotification(IntPtr hWnd);
- [DllImport("Gdi32.dll", SetLastError = true)]
- internal unsafe static extern uint GetRegionData(
- IntPtr hRgn,
- uint dwCount,
- NativeStructs.RGNDATA* lpRgnData);
- [DllImport("Gdi32.dll", SetLastError = true)]
- internal unsafe static extern IntPtr CreateRectRgn(
- int nLeftRect,
- int nTopRect,
- int nRightRect,
- int nBottomRect);
- [DllImport("Gdi32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool DeleteObject(IntPtr hObject);
- [DllImport("kernel32.dll", SetLastError = true)]
- internal static extern IntPtr VirtualAlloc(
- IntPtr lpAddress,
- UIntPtr dwSize,
- uint flAllocationType,
- uint flProtect);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool VirtualFree(
- IntPtr lpAddress,
- UIntPtr dwSize,
- uint dwFreeType);
- [DllImport("Kernel32.dll", SetLastError = false)]
- internal static extern IntPtr HeapAlloc(IntPtr hHeap, uint dwFlags, UIntPtr dwBytes);
- [DllImport("Kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool HeapFree(IntPtr hHeap, uint dwFlags, IntPtr lpMem);
- [DllImport("Kernel32.dll", SetLastError = false)]
- internal static extern UIntPtr HeapSize(IntPtr hHeap, uint dwFlags, IntPtr lpMem);
- [DllImport("Kernel32.dll", SetLastError = true)]
- internal static extern IntPtr HeapCreate(
- uint flOptions,
- [MarshalAs(UnmanagedType.SysUInt)] IntPtr dwInitialSize,
- [MarshalAs(UnmanagedType.SysUInt)] IntPtr dwMaximumSize
- );
- [DllImport("Kernel32.dll", SetLastError = true)]
- internal static extern uint HeapDestroy(IntPtr hHeap);
- [DllImport("Kernel32.Dll", SetLastError = true)]
- internal unsafe static extern uint HeapSetInformation(
- IntPtr HeapHandle,
- int HeapInformationClass,
- void* HeapInformation,
- uint HeapInformationLength
- );
- [DllImport("user32.dll", SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- //
- // 以下是参考https://www.fangsi.net/30.html,为了实现hint
- //
- /// <summary>
- ///
- /// </summary>
- /// <param name="hWnd"></param>
- /// <param name="msg"></param>
- /// <param name="wParam"></param>
- /// <param name="lParam"></param>
- /// <returns></returns>
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
- [DllImport("user32.dll")]
- private static extern bool GetComboBoxInfo(IntPtr hwnd, ref COMBOBOXINFO pcbi);
- [StructLayout(LayoutKind.Sequential)]
- private struct COMBOBOXINFO
- {
- public int cbSize;
- public RECT rcItem;
- public RECT rcButton;
- public IntPtr stateButton;
- public IntPtr hwndCombo;
- public IntPtr hwndItem;
- public IntPtr hwndList;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct RECT
- {
- public int left;
- public int top;
- public int right;
- public int bottom;
- }
- private const int EM_SETCUEBANNER = 0x1501;
- public static void SetCueText(Control control, string text)
- {
- if (control is ComboBox)
- {
- COMBOBOXINFO info = GetComboBoxInfo(control);
- SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, text);
- }
- else
- {
- SendMessage(control.Handle, EM_SETCUEBANNER, 0, text);
- }
- }
- private static COMBOBOXINFO GetComboBoxInfo(Control control)
- {
- COMBOBOXINFO info = new COMBOBOXINFO();
- info.cbSize = Marshal.SizeOf(info);
- GetComboBoxInfo(control.Handle, ref info);
- return info;
- }
- }
- }
|