當你的程式不需要外框, 或著需要自行設計外框圖樣時
可以透過FormBorderStyle設定(如下)製造無框的效果, 如上圖:
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
但是... 少了標題列(Title Bar)也就沒了原本的拖曳的效果, 視窗無法移動
這時我們需要自行設計拖曳視窗的事件, 以下是簡單範例:
需要三個事件, 依事件發生次序排列分別為:
MouseDown, MouseMove, MouseUp
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.curr_x = e.X;
this.curr_y = e.Y;
this.isWndMove = true;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.isWndMove)
this.Location = new Point(this.Left + e.X - this.curr_x, this.Top + e.Y - this.curr_y);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
this.isWndMove = false;
}
最後, 再放上關閉視窗的Click事件, 就算完成啦~
其他變化就自行發揮囉~