#region
|
|
using System;
|
using System.Runtime.InteropServices;
|
using System.Text;
|
|
#endregion
|
|
namespace CSFrameworkV5.Common
|
{
|
/// <summary>
|
/// WinAPI函数库
|
/// </summary>
|
public class WinAPI
|
{
|
/// <summary>
|
/// 枚举窗体的回调函数
|
/// </summary>
|
/// <param name="hWnd"></param>
|
/// <param name="lParam"></param>
|
/// <returns></returns>
|
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
|
|
public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
|
|
public const int SW_SHOWNORMAL = 1;
|
public const int SW_SHOWMINIMIZED = 2;
|
public const int SW_SHOWMAXIMIZED = 3;
|
public const int SW_SHOWNOACTIVATE = 4; //用最近的大小和位置显示一个窗口,同时不改变活动窗口
|
public const int SW_SHOW = 5; //用当前的大小和位置显示一个窗口,同时令其进入活动状态
|
public const int SW_MINIMIZE = 6; //最小化窗口,活动状态给令一个窗口
|
public const int SW_SHOWMINNOACTIVE = 7; //最小化一个窗口,同时不改变活动窗口
|
public const int SW_SHOWNA = 8; //用当前的大小和位置显示一个窗口,不改变活动窗口
|
public const int SW_RESTORE = 9; //与 SW_SHOWNORMAL 1 相同
|
public const int SW_SHOWDEFAULT = 10;
|
public const int SW_FORCEMINIMIZE = 11;
|
public const int SW_MAX = 11;
|
public const int WM_SYSCOMMAND = 0x112;
|
public const int SC_MINIMIZE = 0xF020;
|
public const int SC_MAXIMIZE = 0xF030;
|
|
private const int SC_CLOSE = 0xF060;
|
private const int MF_GRAYED = 0x1;
|
|
public static void DisableCloseButton(IntPtr hwnd)
|
{
|
EnableMenuItem(GetSystemMenu(hwnd, false), SC_CLOSE, MF_GRAYED);
|
}
|
|
[DllImport("user32.dll")]
|
private static extern int EnableMenuItem(IntPtr hMenu,
|
int wIDEnableItem, int wEnable);
|
|
[DllImport("user32.dll", EntryPoint = "EnumWindows",
|
SetLastError = true)]
|
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc,
|
uint lParam);
|
|
[DllImport("user32")]
|
public static extern int EnumWindows(EnumWindowsProc hWnd,
|
IntPtr lParam);
|
//使用:PostMessage(Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
|
|
[DllImport("User32.dll ")]
|
public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe,
|
string strclass, string strname);
|
|
[DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
|
public static extern IntPtr GetParent(IntPtr hWnd);
|
|
[DllImport("user32.dll")]
|
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
|
|
[DllImport("user32", SetLastError = true)]
|
public static extern int GetWindowText(
|
IntPtr hWnd, //窗口句柄
|
StringBuilder lpString, //标题
|
int nMaxCount //最大值
|
);
|
|
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
|
public static extern uint GetWindowThreadProcessId(IntPtr hWnd,
|
ref uint lpdwProcessId);
|
|
[DllImport("user32.dll", EntryPoint = "IsWindow")]
|
public static extern bool IsWindow(IntPtr hWnd);
|
|
[DllImport("user32.dll", EntryPoint = "PostMessage")]
|
public static extern int PostMessage(IntPtr hwnd, int wMsg, int wParam,
|
int lParam);
|
|
public static void RestoreWindow(IntPtr hwnd)
|
{
|
ShowWindow(hwnd, SW_RESTORE);
|
}
|
|
[DllImport("user32", EntryPoint = "SetActiveWindow")]
|
public static extern int SetActiveWindow(int hwnd);
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
|
public static extern bool
|
SetForegroundWindow(IntPtr hWnd); //WINAPI 设置当前活动窗体的句柄
|
|
[DllImport("kernel32.dll", EntryPoint = "SetLastError")]
|
public static extern void SetLastError(uint dwErrCode);
|
//使用:IntPtr p=FindWindowEx(System.IntPtr.Zero,System.IntPtr.Zero,null,"窗口标题");
|
|
[DllImport("user32.dll", EntryPoint = "ShowWindow",
|
CharSet = CharSet.Auto)]
|
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
|
|
[DllImport("User32.dll")]
|
public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
|
|
#region 窗体边框阴影效果变量申明
|
|
public const int CS_DropSHADOW = 0x20000;
|
public const int GCL_STYLE = -26;
|
|
//声明Win32 API
|
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
public static extern int SetClassLong(IntPtr hwnd, int nIndex,
|
int dwNewLong);
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
public static extern int GetClassLong(IntPtr hwnd, int nIndex);
|
|
#endregion
|
}
|
}
|