当前位置:首页 > 开发 > C# > 正文内容

C# Winform 使用控件移动窗口

C#3年前 (2022-11-07)
前置    

将窗口的FormBorderStyle属性设置为FixedSingle

为窗口添加一个Panel控件,并将Dock属性设置为Top

移动    
        private Point point;
        
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            point = e.Location;
        }
        
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point pt = Control.MousePosition;
                pt.Offset(-point.X, -point.Y);
                Location = pt;
            }
        }


最小化    
            this.WindowState = FormWindowState.Minimized;
            this.Invalidate();


最大化    
            this.WindowState = WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;
            this.Invalidate();


关闭    
            this.Close();


绘制边框    
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            Rectangle rect = new Rectangle(0, 0, Width, Height);
            ControlPaint.DrawBorder3D(e.Graphics, rect,Border3DStyle.Flat);
        }


拖动窗体移动    
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wparam, int lparam);
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)           
            {
                Capture = false;                
                SendMessage((sender as Control).Handle, 161, 2, 0);          
            }
        }



转载请注明出处。

本文链接:http://pythonopen.com/?id=247

返回列表

上一篇:C# 热键

下一篇:C#一些重写

相关文章

CS0052 可访问性不一致: 字段类型“n1”的可访问性低于字段“n2”

问题CS0052 可访问性不一致: 字段类型“n1”的可访问性低于字段“n2”原因n1未使用publ...

C# 枚举类型enum 例子

/// <summary> /// 枚举类型 /// ...

C# for,while,do while,switch

for    #region for for ...

C# 跳出foreach循环

在 C# 中,如果你想在 foreach 循环内部提前跳出当前这一轮循环,继续执行下一轮循环,可以使...

C# BackgroundWorker

1.概述BackgroundWorker是一个在 WinForms 应用程序中用于简化在后台线程执行...

C# System.IO.Path

System.IO.Path.GetExtension返回指定的路径字符串的扩展名。string&n...