当前位置:首页 > 开发 > 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#一些重写

相关文章

C# 将函数作为参数传递

无参数    static void Main(s...

C# [OnPaint]和[OnPaintBackground]的区别

OnPaint和OnPaintBackground的主要功能区别OnPaint:OnPaint方法主...

C# 类接口

定义接口是一种抽象类型,它定义了一组方法签名(方法名称、参数列表和返回类型),但没有方法体。接口用于...

c# Invalidate、Refresh、Refreshitem、Refreshitems的功能

Invalidate方法功能概述Invalidate方法主要用于使控件的特定区域(整个控件或部分区域...

C# i++和++i的区别

核心区别操作顺序            ...

C# 比较两个Image对象是否相同

方法思路基础检查:先检查空引用和图像尺寸像素格式验证:确保两个图像的像素格式相同内存锁定:使用Loc...