C#拖动自己的定义标题栏(panel)以及实现窗体拖动关闭和最小化

//没有标题
this.FormBorderStyle = FormBorderStyle.None;
//任务栏不显示
this.ShowInTaskbar = false;

//实现拖动

1.在窗体类里面加入,下面两个,按提示加入命名空间

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern bool ReleaseCapture();

2.拖动一个panel然后,闪电事件中加入下面代码,实现拖动
private void panel1_MouseDown(object sender, MouseEventArgs e)//在panel1中添加mouse_down事件
{
  const int WM_NCLBUTTONDOWN = 0x00A1;
  const int HTCAPTION = 2;

  if (e.Button == MouseButtons.Left) // 按下的是鼠标左键
  {
    ReleaseCapture();
    SendMessage(this.Handle, WM_NCLBUTTONDOWN, (IntPtr)HTCAPTION, IntPtr.Zero); // 拖动窗体
  }
}

//形状实现

最小化

private void button4_Click(object sender, EventArgs e)
{
  this.WindowState = FormWindowState.Minimized;
}

最大化

private void button5_Click(object sender, EventArgs e)
{
  this.WindowState = FormWindowState.Maximized;
}

关闭

private void button6_Click(object sender, EventArgs e)
{
  this.Close();
}

上一篇:pytest_runtest_makereport实现断言失败自动截图并集成到allure报告


下一篇:图解 Redis | 差点崩溃了,还好有主从复制