C#无边框窗体实现以及拖动代码

时间:2021-08-17 07:04:37

实现效果:

C#无边框窗体实现以及拖动代码

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
 
namespace noborderform
{
  public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }
    
    private void form1_load(object sender, eventargs e)
    {
      
    }
 
    point mouseoff;//鼠标移动位置变量
    bool leftflag;//标记是否为左键
    private void form1_mousedown(object sender, mouseeventargs e)
    {
      if (e.button == mousebuttons.left)
      {
        mouseoff = new point(-e.x, -e.y); //得到变量的值
        leftflag = true;         //点击左键按下时标注为true;
      }
    }
 
    private void form1_mousemove(object sender, mouseeventargs e)
    {
      if (leftflag)
      {
        point mouseset = control.mouseposition;
        mouseset.offset(mouseoff.x, mouseoff.y); //设置移动后的位置
        location = mouseset;
      }
    }
 
    private void form1_mouseup(object sender, mouseeventargs e)
    {
      if (leftflag)
      {
        leftflag = false;//释放鼠标后标注为false;
      }
    }
 
    private void button1_click(object sender, eventargs e)
    {
      dialogresult dr = messagebox.show("yes/no", "exit", messageboxbuttons.yesno, messageboxicon.warning);
      if (dr == dialogresult.yes)
      {
        this.close();
      }
      else
      {
        return;
      }
    }
 
    private void button2_click(object sender, eventargs e)
    {
      this.windowstate = formwindowstate.minimized;
    }
 
    private void checkbox1_checkedchanged(object sender, eventargs e)
    {
 
    }
 
    private void form1_click(object sender, eventargs e)
    {
 
    }
 
    private void panel1_click(object sender, eventargs e)
    {
      
    }
 
    private void textbox1_click(object sender, eventargs e)
    {
      
    }
 
    private void linklabel1_linkclicked(object sender, linklabellinkclickedeventargs e)
    {
      system.diagnostics.process.start("http://www.baidu.com");
    }
 
    private void button4_click(object sender, eventargs e)
    {
      
    }
  }
}