C#Winform窗口特效源码(1)
本文基于.Net开发,使用C#作为开发语言,分别包含以下效果: 移动无边框窗口、窗口移动限制(限制在屏幕内)、桌面贴边自动隐藏(仿QQ隐藏窗口)
1、移动无边框窗口 采用了消息的方式,可以实现通过窗口内的任何控件来移动窗口
- private const int WM_NCLBUTTONDOWN = 0x00A1;
- private const int HT_CAPTION = 0x002;
- private void Form_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- ((Control)sender).Capture = false;
- Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
- base.WndProc(ref msg);
- }
- }
使用方法: 将以上代码复制到Form的class内,将Form或Form中需要控制Form移动的控件的MouseDown事件关联到这个方法上,如: 对于Form:this.MouseDown+=new System.Windows.Forms.MouseEventHandler(Form_MouseDown); 对于其它控件:Control.MouseDown+=new System.Windows.Forms.MouseEventHandler(Form_MouseDown);
2、窗口移动限制
- private const int WM_MOVING = 0x0216;
- protected override void WndProc(ref Message m)
- {
- switch (m.Msg)
- {
- case WM_MOVING: // 窗体移动的消息,控制窗体不会移出屏幕外
- int left = Marshal.ReadInt32(m.LParam, 0);
- int top = Marshal.ReadInt32(m.LParam, 4);
- int right = Marshal.ReadInt32(m.LParam, 8);
- int bottom = Marshal.ReadInt32(m.LParam, 12);
- left = Math.Min(Math.Max(0, left), Screen.PrimaryScreen.Bounds.Width - Width);
- top = Math.Min(Math.Max(0, top), Screen.PrimaryScreen.Bounds.Height - Height);
- right = Math.Min(Math.Max(Width, right), Screen.PrimaryScreen.Bounds.Width);
- bottom = Math.Min(Math.Max(Height, bottom), Screen.PrimaryScreen.Bounds.Height);
- Marshal.WriteInt32(m.LParam, 0, left);
- Marshal.WriteInt32(m.LParam, 4, top);
- Marshal.WriteInt32(m.LParam, 8, right);
- Marshal.WriteInt32(m.LParam, 12, bottom);
- break;
- }
- base.WndProc(ref m);
- }
使用方法: 添加using System.Runtime.InteropServices; 在Form类定义中加入以上代码即可 3、桌面贴边自动隐藏
- public class FormAutoDock
- {
- public static void SideHideOrShow(Form DockableForm, ref int DockFormHeight, Timer _dockTimer)
- {
- if (DockableForm.WindowState != FormWindowState.Minimized)
- {
- _dockTimer.Interval = 1500;
- if (Cursor.Position.X > DockableForm.Left - 1 && Cursor.Position.X < DockableForm.Right && Cursor.Position.Y > DockableForm.Top - 1 && Cursor.Position.Y < DockableForm.Bottom)
- {
- if (DockableForm.Top <= 0 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
- {
- DockableForm.Top = 0;
- }
- else if (DockableForm.Left <= 0)
- {
- DockableForm.Left = 0;
- }
- else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width)
- {
- DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width;
- }
- else
- {
- if (DockFormHeight > 0)
- {
- DockableForm.Height = DockFormHeight;
- DockFormHeight = 0;
- }
- }
- }
- else
- {
- if (DockFormHeight < 1)
- {
- DockFormHeight = DockableForm.Height;
- }
- if (DockableForm.Top <= 4 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
- {
- DockableForm.Top = 3 - DockableForm.Height;
- if (DockableForm.Left <= 4)
- {
- DockableForm.Left = -5;
- }
- else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
- {
- DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width + 5;
- }
- }
- else if (DockableForm.Left <= 4)
- {
- DockableForm.Left = 3 - DockableForm.Width;
- }
- else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
- {
- DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - 3;
- }
- _dockTimer.Interval = 200;
- }
- }
- }
- }
使用方法: 将FormAutoDock.cs文件复制到与需要处理的Form相同目录下,并添加到项目中 在Form的类中增加以下内容: private Timer _dockTimer = null; private int DockFormHeight = 0; private void DockTimerAutoHide_Tick(object sender, EventArgs e) { FormAutoDock.SideHideOrShow(this, ref DockFormHeight, _dockTimer); Application.DoEvents(); }
在Form类构造方法中添加以下内容: _dockTimer = new Timer(); _dockTimer.Enabled = false; _dockTimer.Interval = 200; _dockTimer.Tick += new EventHandler(DockTimerAutoHide_Tick); _dockTimer.Start();