How to make moveable on the desktop a borderless form in c#? [duplicate]

为君一笑 提交于 2021-02-16 15:24:53

问题


I want to be able to move with the mouse a form without border and title bar in c#.

I looked over youtube but i can't find anything that is working.

Someone can help me?


回答1:


You can use the form's MouseDown, Up and Move events with a conditional variable like that:

private bool IsMoving;
private Point LastCursorPosition;

private void FormTest_MouseDown(object sender, MouseEventArgs e)
{
  IsMoving = true;
  LastCursorPosition = Cursor.Position;
}

private void FormTest_MouseUp(object sender, MouseEventArgs e)
{
  IsMoving = false;
}

private void FormTest_MouseMove(object sender, MouseEventArgs e)
{
  if ( IsMoving )
  {
    int x = Left - ( LastCursorPosition.X - Cursor.Position.X );
    int y = Top - ( LastCursorPosition.Y - Cursor.Position.Y );
    Location = new Point(x, y);
    LastCursorPosition = Cursor.Position;
  }
}

It works with the background of the form but you can add this to any other control too.



来源:https://stackoverflow.com/questions/62730027/how-to-make-moveable-on-the-desktop-a-borderless-form-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!