C# 无边框窗体移动 点击任务栏实现最小化与还原

来源:互联网 发布:java图形界面编程例子 编辑:程序博客网 时间:2024/06/04 23:31
        [System.Runtime.InteropServices.DllImport("user32.dll")]        public static extern bool ReleaseCapture();        [System.Runtime.InteropServices.DllImport("user32.dll")]        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);        private const int WM_SYSCOMMAND = 0x0112;//点击窗口左上角那个图标时的系统信息        private const int SC_MOVE = 0xF010;//移动信息        private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息        private const int WM_NCHITTEST = 0x84;//鼠标在窗体客户区(除了标题栏和边框以外的部分)时发送的消息        private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息        private const int SC_MAXIMIZE = 0xF030;//最大化信息        private const int SC_MINIMIZE = 0xF020;//最小化信息        protected override void WndProc(ref Message m) //无边框移动,屏蔽双击最大化        {            switch (m.Msg)            {                case WM_NCHITTEST://如果鼠标移动或单击                    base.WndProc(ref m);//调用基类的窗口过程——WndProc方法处理这个消息                    if ((int)m.Result == HTCLIENT)//如果返回的是HTCLIENT                        m.Result = (IntPtr)HTCAPTION;//把它改为HTCAPTION                    return;                case WM_NCLBUTTONDBLCLK:                    break;                default:                    base.WndProc(ref m);                    break;            }        }        protected override CreateParams CreateParams //点击任务栏实现最小化与还原        {            get            {                const int WS_MINIMIZEBOX = 0x00020000;                    CreateParams cp = base.CreateParams;                cp.Style = cp.Style | WS_MINIMIZEBOX;   // 允许最小化操作                  return cp;            }        } 


任务栏添加右键菜单,win7的系统就不需要了,XP需要

现在窗体类中加入:

[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]  public static extern int GetWindowLong(HandleRef hWnd, int nIndex);    [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]  public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong); 

然后在构造函数或者窗体Load事件中加入下面三句代码:

    int WS_SYSMENU = 0x00080000;      int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));      SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU);  

    this.MaximizedBounds = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea; //最大化区域不覆盖任务栏



0 0
原创粉丝点击