C#创建不规则窗体教程中有点疑问。

来源:百度知道 编辑:UC知道 时间:2024/06/19 01:46:42
namespace 不规则窗口
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private Point mouse_offset;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouse_offset = new Point(-e.X, -e.Y);
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point mousepos = Control.MousePosition;
mousepos.Offset(mouse_offset.X, mouse_offset.Y);
Location = mousepos;
}
}

Point mouseOffset;//记录按下鼠标左键时坐标
bool isMouseDown = false;//鼠标是否被按下

private void Form1_MouseDown(object sender, MouseEventArgs e)
{//鼠标按下时
int x;
int y;
if (e.Button == MouseButtons.Left)//判断鼠标左键是否被按下
{
x = -e.X - SystemInformation.FrameBorderSize.Width;
y = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(x, y);//记录了按下鼠标左键时的坐标 isMouseDown = true;

}
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{//鼠标移动时
if (isMouseDown)//如果鼠标左键被按下,并未被释放
{ //当前鼠标位置
Point mousepos = Control.MousePosition;
mousepos.Offset(mouseOffset.X+2, mouseOffset.Y+25);
//当前窗口位置