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
//
///
///
///
///
///
///
///
///
[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;
}
}
}