123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- using Microsoft.Win32.SafeHandles;
- using System;
- using System.Runtime.InteropServices;
- using System.Security;
- using System.Text;
- using System.Windows.Forms;
- namespace PaintDotNet.SystemLayer
- {
- [SuppressUnmanagedCodeSecurity]
- public static class SafeNativeMethods
- {
- [DllImport("user32.dll")]
- public static extern IntPtr GetDesktopWindow();
- [DllImport("user32.dll")]
- public static extern IntPtr GetWindowDC(IntPtr hWnd);
- [DllImport("user32.dll")]
- public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
- [DllImport("user32.dll")]
- public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
- public const int SRCCOPY = 0x00CC0020;
- [DllImport("gdi32.dll")]
- public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth,
- int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop);
- [DllImport("gdi32.dll")]
- public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
- [DllImport("kernel32.dll", SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool IsProcessorFeaturePresent(uint ProcessorFeature);
- [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("dwmapi.dll")]
- internal unsafe static extern int DwmGetWindowAttribute(
- IntPtr hwnd,
- uint dwAttribute,
- void* pvAttribute,
- uint cbAttribute);
- [DllImport("kernel32.dll", SetLastError = false)]
- internal static extern IntPtr GetCurrentThread();
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool SetThreadPriority(
- IntPtr hThread,
- int nPriority);
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- internal static extern IntPtr CreateFileMappingW(
- IntPtr hFile,
- IntPtr lpFileMappingAttributes,
- uint flProtect,
- uint dwMaximumSizeHigh,
- uint dwMaximumSizeLow,
- [MarshalAs(UnmanagedType.LPTStr)] string lpName);
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- internal static extern IntPtr MapViewOfFile(
- IntPtr hFileMappingObject,
- uint dwDesiredAccess,
- uint dwFileOffsetHigh,
- uint dwFileOffsetLow,
- UIntPtr dwNumberOfBytesToMap);
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool ShowScrollBar(
- IntPtr hWnd,
- int wBar,
- [MarshalAs(UnmanagedType.Bool)] bool bShow);
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool GetVersionEx(ref NativeStructs.OSVERSIONINFOEX lpVersionInfo);
- [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", CharSet = CharSet.Unicode, SetLastError = true)]
- internal static extern IntPtr CreateFontW(
- int nHeight,
- int nWidth,
- int nEscapement,
- int nOrientation,
- int fnWeight,
- uint fdwItalic,
- uint fdwUnderline,
- uint fdwStrikeOut,
- uint fdwCharSet,
- uint fdwOutputPrecision,
- uint fdwClipPrecision,
- uint fdwQuality,
- uint fdwPitchAndFamily,
- string lpszFace);
- [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- internal static extern int DrawTextW(
- IntPtr hdc,
- string lpString,
- int nCount,
- ref NativeStructs.RECT lpRect,
- uint uFormat);
- [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("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- internal static extern IntPtr CreateFileW(
- string lpFileName,
- uint dwDesiredAccess,
- uint dwShareMode,
- IntPtr lpSecurityAttributes,
- uint dwCreationDisposition,
- uint dwFlagsAndAttributes,
- IntPtr hTemplateFile);
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal unsafe static extern bool WriteFile(
- IntPtr hFile,
- void* lpBuffer,
- uint nNumberOfBytesToWrite,
- out uint lpNumberOfBytesWritten,
- IntPtr lpOverlapped);
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal unsafe static extern bool ReadFile(
- SafeFileHandle sfhFile,
- void* lpBuffer,
- uint nNumberOfBytesToRead,
- out uint lpNumberOfBytesRead,
- IntPtr lpOverlapped);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool CloseHandle(IntPtr hObject);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool SetHandleInformation(
- IntPtr hObject,
- uint dwMask,
- uint dwFlags);
- [DllImport("user32.dll", SetLastError = false)]
- internal static extern int GetUpdateRgn(
- IntPtr hWnd,
- IntPtr hRgn,
- [MarshalAs(UnmanagedType.Bool)] bool bErase);
- [DllImport("user32.dll", SetLastError = false)]
- internal static extern uint GetWindowThreadProcessId(
- IntPtr hWnd,
- out uint lpdwProcessId);
- [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", 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("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("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)]
- internal static extern uint WaitForSingleObject(
- IntPtr hHandle,
- uint dwMilliseconds);
- [DllImport("kernel32.dll", SetLastError = true)]
- internal static extern uint WaitForMultipleObjects(
- uint nCount,
- IntPtr[] lpHandles,
- [MarshalAs(UnmanagedType.Bool)] bool bWaitAll,
- uint dwMilliseconds);
- 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)]
- internal extern static bool MoveToEx(
- IntPtr hdc,
- int X,
- int Y,
- out NativeStructs.POINT lpPoint);
- [DllImport("Gdi32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool LineTo(
- IntPtr hdc,
- int nXEnd,
- int nYEnd);
- [DllImport("User32.dll", SetLastError = true)]
- internal extern static int FillRect(
- IntPtr hDC,
- ref NativeStructs.RECT lprc,
- IntPtr hbr);
- [DllImport("Gdi32.dll", SetLastError = true)]
- internal extern static IntPtr CreatePen(
- int fnPenStyle,
- int nWidth,
- uint crColor);
- [DllImport("Gdi32.dll", SetLastError = true)]
- internal extern static IntPtr CreateSolidBrush(uint crColor);
- [DllImport("Gdi32.dll", SetLastError = false)]
- public extern static IntPtr SelectObject(
- IntPtr hdc,
- IntPtr hgdiobj);
- [DllImport("Gdi32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool DeleteObject(IntPtr hObject);
- [DllImport("Gdi32.dll", SetLastError = true)]
- public extern static uint DeleteDC(IntPtr hdc);
- [DllImport("Gdi32.Dll", SetLastError = true)]
- public extern static IntPtr CreateCompatibleDC(IntPtr hdc);
- [DllImport("Gdi32.Dll", SetLastError = true)]
- internal extern static uint BitBlt(
- IntPtr hdcDest,
- int nXDest,
- int nYDest,
- int nWidth,
- int nHeight,
- IntPtr hdcSrc,
- int nXSrc,
- int nYSrc,
- uint dwRop);
- [DllImport("Gdi32.Dll", SetLastError = true)]
- internal extern static uint StretchBlt(
- IntPtr hdcDest,
- int nXOriginDest,
- int nYOriginDest,
- int nWidthDest,
- int nHeightDest,
- IntPtr hdcSrc,
- int nXOriginSrc,
- int nYOriginSrc,
- int nWidthSrc,
- int nHeightSrc,
- uint dwRop);
- [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 = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool VirtualProtect(
- IntPtr lpAddress,
- UIntPtr dwSize,
- uint flNewProtect,
- out uint lpflOldProtect);
- [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("winhttp.dll", CharSet = CharSet.Unicode)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool WinHttpGetIEProxyConfigForCurrentUser(ref NativeStructs.WINHTTP_CURRENT_USER_IE_PROXY_CONFIG pProxyConfig);
- [DllImport("kernel32.dll", SetLastError = true)]
- internal static extern IntPtr GlobalFree(IntPtr hMem);
- [DllImport("user32.dll", SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool SetForegroundWindow(IntPtr hWnd);
- [DllImport("user32.dll", SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool IsIconic(IntPtr hWnd);
- [DllImport("user32.dll", SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- //
- // ÒÔÏÂÊDzο¼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 SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder 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;
- private const int EM_GETCUEBANNER = 0x1502;
- 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();
- //a combobox is made up of three controls, a button, a list and textbox;
- //we want the textbox
- info.cbSize = Marshal.SizeOf(info);
- GetComboBoxInfo(control.Handle, ref info);
- return info;
- }
- public static string GetCueText(Control control)
- {
- StringBuilder builder = new StringBuilder();
- if (control is ComboBox)
- {
- COMBOBOXINFO info = new COMBOBOXINFO();
- //a combobox is made up of two controls, a list and textbox;
- //we want the textbox
- info.cbSize = Marshal.SizeOf(info);
- GetComboBoxInfo(control.Handle, ref info);
- SendMessage(info.hwndItem, EM_GETCUEBANNER, 0, builder);
- }
- else
- {
- SendMessage(control.Handle, EM_GETCUEBANNER, 0, builder);
- }
- return builder.ToString();
- }
- }
- }
|