C# winform无边框窗体移动的四种方法


一、重写WndProc

protected override void WndProc(ref Message m)
{       
        const int WM_NCHITTEST = 0x84;
        const int HTCLIENT = 0x01;
        const int HTCAPTION = 0x02;
         if (m.Msg == WM_NCHITTEST)
        {
                this.DefWndProc(ref m);
                if (m.Result.ToInt32() == HTCLIENT)
                    m.Result = new IntPtr(HTCAPTION);
                else
                    base.WndProc(ref m);
        }
        else
        {
                base.WndProc(ref m);
        }
}
GarsonZhang www.yesdotnet.com

二、创建消息

private const int WM_NCLBUTTONDOWN = 0x00A1;
private const int WM_NCHITTEST = 0x84;
private const int HT_CAPTION = 0x2;
private const int HT_CLIENT = 0x1;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    //this.Capture = false;
    pictureBox1.Capture = false;
    Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
    WndProc(ref   msg);   
} 
GarsonZhang www.yesdotnet.com

三、调用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;


//在窗体的_MouseDown中加入如下代码: 
如: private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); }
GarsonZhang www.yesdotnet.com

四、鼠标事件:MouseDown, MouseMove, MouseUp

/// <summary>
/// 拖动鼠标移动窗体Form通用类
/// </summary>
public class FormDragDrop
{

    /// <summary>
    /// 窗体拖动注册类
    /// </summary>
    /// <param name="form">窗体对象</param>
    /// <param name="col">拖动控件对象,空的话代表拖动窗体</param>
    public static void Register(Form form, Control col = null)
    {
        FormDragDrop F = new FormDragDrop(form, col);
        F.Init();
    }

    private Point _MouseOffset;
    private bool _IsMouseDown = false;
    private Form _form;
    private Control _control;

    private FormDragDrop(Form form, Control col = null)
    {
        _form = form;
        _control = col;
    }


    private void Init()
    {
        Control col = _control != null ? _control : _form;
        col.MouseDown += On_MouseDown;
        col.MouseMove += On_MouseMove;
        col.MouseUp += On_MouseUp;
    }

    private void On_MouseDown(object sender, 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 On_MouseMove(object sender, MouseEventArgs e)
    {
        if (_IsMouseDown)
        {
            Point mousePos = Control.MousePosition;
            mousePos.Offset(_MouseOffset.X, _MouseOffset.Y);
            _form.Location = mousePos;
        }
    }

    private void On_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _IsMouseDown = false;
        }
    }

}
GarsonZhang www.yesdotnet.com

使用方法:

在窗体Load事件中调用方法,启用鼠标拖拽移动窗体

// 注册鼠标拖拽移动窗体
FormDragDrop.Register(this, this.panelControl1);
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
YES开发框架
上一篇:ASP.NET MVC使用@Url.Action 多个参数中间&被URL编码了
下一篇:EntityFramework Linq 获得所有数据,包括子节点
评论列表

发表评论

评论内容
昵称:
关联文章

C# winform边框移动方法
权限
状态
css两颜色边框设置,边框设置多种颜色
自定义权限操作按钮
YESWin Winform开发框架 Form继承关系
.NET Core Winform 打开设计器报错
python配置文件
Winform设置组件可用状态
GridView布局自定义
编辑中关联赋值操作
.net core winform继承后设计器异常,看不到控件,并且页无法添加控件
LabVIEW生成.NETDLL——C#下调用NI数据采集设备功能方法 [原创www.cnblogs.com/helesheng]
C# Graphics画有填充和填充形状
【OpenXml】Pptx边框虚线转为WPF边框虚线
JS合并两个数组3方法详解
DevExpress Winform统一设置字大小
C#使用Thrift作为RPC框架实战()之TSocket
快速删除node_modules文件夹方法
C# Winform 自定义异常处理方法

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码