9个常用的WindowsAPI
1.API根据窗体句柄修改位置和大小
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")] public static extern bool MoveWindow(int hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); [DllImport("user32.dll", EntryPoint = "FindWindow")] public static extern int FindWindow( string lpClassName, string lpWindowName ); MoveWindow(FindWindow(null, "VLC media player"), groupBox2.Location.X, groupBox2.Location.Y, groupBox2.Width, groupBox2.Height, true);
2.API根据窗体句柄修改指定窗体的title(标题)
[DllImport("user32.dll", EntryPoint = "FindWindowA", CharSet = CharSet.Ansi)] public static extern int FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Ansi)] public static extern int SetWindowText(int hwnd, string lpString); static void Main(string[] args) { int lHwnd = FindWindow(null, "无标题 - 记事本"); while (lHwnd != 0) { SetWindowText(lHwnd, "有标题"); lHwnd = FindWindow(null, "无标题 - 记事本"); } }
3.C#使用API禁用/删除程序窗体的关闭菜单和按钮
[DllImport("user32.dll")] static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert); [DllImport("user32.dll")] static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags); [DllImport("user32.dll")] static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable); const uint SC_MOVE = 0xF010; //移动 const uint SC_CLOSE = 0xF060;//关闭 const uint MF_BYCOMMAND = 0x00; //按命令方式 const uint MF_GRAYED = 0x01; //灰掉 const uint MF_DISABLED = 0x02; //不可用 private void Form1_Load(object sender, EventArgs e) { IntPtr hMenu = GetSystemMenu(this.Handle, false); //获取程序窗体的句柄 if (hMenu != IntPtr.Zero) { DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND); //删除移动菜单,禁用移动功能 EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED | MF_DISABLED); //禁用关闭功能 } } RemoveMenu(GetSystemMenu(FindWindow("QWidget", null), 0), SC_MINIMIZE, SC_MINIMIZE);
4.C#使用API制作窗体开启关闭特效动画
private const Int32 AW_HOR_POSITIVE = 0x00000001; //从左到右显示 private const Int32 AW_HOR_NEGATIVE = 0x00000002; //从右到左显示 private const Int32 AW_VER_POSITIVE = 0x00000004; //从上到下显示 private const Int32 AW_VER_NEGATIVE = 0x00000008; //从下到上显示 private const Int32 AW_CENTER = 0x00000010; //若使用了AW_HIDE标志,则使窗口向内重叠,即收缩窗口;否则使窗口向外扩展,即展开窗口 private const Int32 AW_HIDE = 0x00010000; //隐藏窗口,缺省则显示窗口 private const Int32 AW_ACTIVATE = 0x00020000; //激活窗口。在使用了AW_HIDE标志后不能使用这个标志 private const Int32 AW_SLIDE = 0x00040000; //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略 private const Int32 AW_BLEND = 0x00080000; //渐变 [DllImport("user32")]//using System.Runtime.InteropServices; private static extern bool AnimateWindow(IntPtr whnd, int dwtime, int dwflag); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) {//实现的效果是:从标题栏处下拉出整个窗体 AnimateWindow(this.Handle, 100, AW_SLIDE | AW_VER_POSITIVE ); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) {//实现的效果是:整个窗体从下上推至标题栏 AnimateWindow(this.Handle, 100, AW_HIDE | AW_SLIDE | AW_VER_NEGATIVE);//因为是关闭窗体,所以参数要写AW_HIDE }
5.鼠标穿透效果
private const uint WS_EX_LAYERED = 0x80000; private const int WS_EX_TRANSPARENT = 0x20; private const int GWL_STYLE = (-16); private const int GWL_EXSTYLE = (-20); private const int LWA_ALPHA = 0; [DllImport("user32", EntryPoint = "SetWindowLong")] private static extern uint SetWindowLong( IntPtr hwnd, int nIndex, uint dwNewLong ); [DllImport("user32", EntryPoint = "GetWindowLong")] private static extern uint GetWindowLong( IntPtr hwnd, int nIndex ); [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")] private static extern int SetLayeredWindowAttributes( IntPtr hwnd, int crKey, int bAlpha, int dwFlags ); /// <summary> /// 设置窗体具有鼠标穿透效果 /// </summary> public void SetPenetrate() { this.TopMost = true; GetWindowLong(this.Handle, GWL_EXSTYLE); SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED); SetLayeredWindowAttributes(this.Handle, 0, 100, LWA_ALPHA); }
6.控制鼠标的位置
[DllImport("User32.dll")] private static extern bool SetCursorPos(int x, int y); private void Form1_Load(object sender, EventArgs e) { SetPos(); } private static void SetPos() { int dx = 0; int dy = 0; SetCursorPos(dx, dy); }
7.拖动移动无标题窗体
[DllImportAttribute("user32.dll")] private extern static bool ReleaseCapture(); [DllImportAttribute("user32.dll")] private extern static int SendMessage(IntPtr handle, int m, int p, int h); protected void MyBaseControl_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.Cursor = Cursors.SizeAll; ReleaseCapture(); SendMessage(this.Handle, 0xA1, 0x2, 0); this.Cursor = Cursors.Default; } } /*注:如果用于运行时的某个控件, * 则可以把上面的代码放入此控件的MouseDown事件中, * 只是SendMessage(this.Handle, 0xA1, 0x2, 0); * 中的this.Handle参数应改为此控件的Handle, * 如this.button1.Handle即可实现。 */
8.窗体窗口置顶,在其他窗体上面
[DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern System.IntPtr GetForegroundWindow(); private void Form1_Load(object sender, EventArgs e) { SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 1 | 2); }
9定义系统热键,自定义快捷键
//注册热键的api [DllImport("user32")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control,Keys vk); [DllImport("user32")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id); private void Form1_Load(object sender, EventArgs e) { RegisterHotKey(this.Handle, 888,2, Keys.A); //注册热键:handle:这个窗体的handle 888:这个热键的标志id 2:crtl键 A: a键 //UnregisterHotKey(this.Handle, 888); //卸载热键:handle:这个窗体的handle 888:上面那个热键的标志id } //监听Windows事件 protected override void WndProc(ref Message m) { switch (m.Msg) { case 0x0312: //这个是window消息定义的 注册的热键消息 if (m.WParam.ToString().Equals("888")) //如果是我们注册的那个热键 MessageBox.Show("你按了ctrl+a"); break; } base.WndProc(ref m); } //None = 0, //Alt = 1, //crtl= 2, //Shift = 4, //Windows = 8 //这是热键的定义 alt+crtl是3 直接相加就可以了