C# 移动不规则窗体

时间:2022-05-20 05:43:22

说明: 解决自定义不规则窗体后,不能拖动问题。

先看效果图:

C# 移动不规则窗体

C# 移动不规则窗体

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;


namespace BT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private Point mouse_offset;
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
mouse_offset = new Point(-e.X, -e.Y);
}

protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Point mousepos = Control.MousePosition;
mousepos.Offset(mouse_offset.X, mouse_offset.Y);
Location = mousepos;
}

}
}
}