• C#移动无标题栏窗体的三种代码:


    C#移动无标题栏窗体的三种代码:
    第一种采用,需注意窗体上的控件是否把窗体覆盖了。。。MouseDown、MouseMove、MouseUp事件应该是鼠标所处位置最顶层的控件的事件
    在窗体的类中声明两个变量
    private Point mouseOffset; //记录鼠标指针的坐标
    private bool isMouseDown = false; //记录鼠标按键是否按下

    创建该窗体 MouseDown、MouseMove、MouseUp事件的相应处理程序
    private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        int xOffset;
        int yOffset;
        if (e.Button == MouseButtons.Left)
        {
            xOffset = -e.X ;
            yOffset = -e.Y ;
            mouseOffset = new Point(xOffset, yOffset);
            isMouseDown = true;
        }
    }

    private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (isMouseDown)
        {
            Point mousePos = Control.MousePosition;
            mousePos.Offset(mouseOffset.X, mouseOffset.Y);
            Location = mousePos;
        }
    }

    private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        // 修改鼠标状态isMouseDown的值
        // 确保只有鼠标左键按下并移动时,才移动窗体
        if (e.Button == MouseButtons.Left)
        {
            isMouseDown = false;
        }
    }

    第二种调用API 未验证
            using System.Runtime.InteropServices;

            [DllImport("user32.dll")]
            public static extern bool ReleaseCapture();
            [DllImport("user32.dll")]
            public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
            public const int WM_SYSCOMMAND = 0x0112;
            public const int SC_MOVE = 0xF010;
            public const int HTCAPTION = 0x0002;
          
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }

    第三种未验证
            private bool isMouseDown = false;
            private Point FormLocation; //form的location
            private Point mouseOffset; //鼠标的按下位置
            [DllImport("user32.dll")]
            public static extern bool ReleaseCapture();

            [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_SYSCOMMAND:
                        if (m.WParam == (IntPtr)SC_MAXIMIZE)
                        {
                            m.WParam = (IntPtr)SC_MINIMIZE;
                        }
                        break;
                    case WM_NCHITTEST: //如果鼠标移动或单击
                        base.WndProc(ref m);//调用基类的窗口过程——WndProc方法处理这个消息
                        if (m.Result == (IntPtr)HTCLIENT)//如果返回的是HTCLIENT
                        {
                            m.Result = (IntPtr)HTCAPTION;//把它改为HTCAPTION
                            return;//直接返回退出方法
                        }
                        break;
                }
                base.WndProc(ref m);//如果不是鼠标移动或单击消息就调用基类的窗口过程进行处理
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }
  • 相关阅读:
    iptables在不重新编译内核以及iptables的情况下 加载iptables模块--在proxmox5中没有测试成功
    ip rule以及ip route的使用--非常重要,需要持续研究!!
    debian版本的相关说明
    分析 linux服务器单网卡在可配置多网关 提供服务的可能性--重要!!!!!
    关于pn定期执行的相关问题分析
    man在线手册-推荐使用debian官方网站
    牛客网暑期ACM多校训练营(第三场) J Distance to Work 计算几何求圆与多边形相交面积模板
    杭电多校第二场 hdu 6315 Naive Operations 线段树变形
    牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径
    牛客网暑期ACM多校训练营(第三场) E Sort String 哈希处理字符串(模板)
  • 原文地址:https://www.cnblogs.com/linmilove/p/1500881.html
Copyright © 2020-2023  润新知