当前位置:首页 > 编程语言 > C# > 正文内容

C# Winform 使用控件移动窗口

C#2年前 (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);          
            }
        }



返回列表

上一篇:C# 热键

下一篇:C#一些重写

相关文章

C# 数据类型

Type ByteLenghtMinMax.NET Framework Typedefau...

ListView RetrieveVirtualListItem 事件需要每个 ListView 列的列表视图子项。”

System.InvalidOperationException:“处于 VirtualMode 中...

C# 时间操作

获取系统已运行时间    System.Environment.Tic...

C# 获取网页源代码

private static string GetHtml(str...

C# 获取带有焦点的控件

this.ActiveControl...

C# 捕获鼠标

方式一-API    /// <summary>...