首页 > 代码库 > 无Border可移动窗体

无Border可移动窗体

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace NoBoarder11 {12     public partial class FormParent : Form13     {14         public FormParent()15         {16             InitializeComponent();17         }18 19         protected bool isMouseDown = false;20         protected Point FormLocation;21         protected Point mouseOffset;22 23         private void FormParent_MouseDown(object sender, MouseEventArgs e)24         {25             if (e.Button == MouseButtons.Left)26             {27                 isMouseDown = true;28                 FormLocation = this.Location;29                 mouseOffset = Control.MousePosition;30 31             }  32         }33 34         private void FormParent_MouseMove(object sender, MouseEventArgs e)35         {36             int x = 0;37             int y = 0;38             if (isMouseDown)39             {40                 Point pt = Control.MousePosition;41                 x = mouseOffset.X - pt.X;42                 y = mouseOffset.Y - pt.Y;43                 this.Location = new Point(FormLocation.X - x, FormLocation.Y - y);44             }  45         }46 47         private void FormParent_MouseUp(object sender, MouseEventArgs e)48         {49             isMouseDown = false; 50         }51     }52 }

        将Form的FormBorderStyle设置为None, 运行就是没边界的窗口可移动的窗口啦

 

  程序中多个无Border的窗口就可以直接继承这个类就可以了, 为了便于理解, 这个继承自Form的类叫FormParent, 且在上面添加一个Button, Text = "ParentForm"

  其Click事件添加代码

1 private void button1_Click(object sender, EventArgs e)2         {3             FormSon f = new FormSon();4             f.Show();5             this.Hide();6         }

  新建一个Windows Form, 取名叫FormSon, 让其继承于FormParent

1 public partial class FormSon : FormParent //!!!!!!!!!!!!!!!!!!!!!!!!!!!!2     { 3         public FormSon()4         {5             InitializeComponent();6         }7     }

  运行程序点击FormParent就会弹出一模一样的FormSon出来, 说明还真是亲生的

 

  但是人总是要有个性的, 我们修改FormSon的Button控件, 发现修改不了, 其实这不是家族遗传病, 是因为FormParent的Modifires属性设置为了Private, 改成Public就可以啦!