请问大神们,这样之后怎么关闭窗体。。小弟刚搞这个,不是很会。求解ing。。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Dynamic
{
public partial class Dynamic : Form
{
public Dynamic()
{
InitializeComponent();
} int offset = 25;//垂直增量
int offset1 = 25;//水平增量 private void Dynamic_FormClosing(object sender, FormClosingEventArgs e)
{
timer_dynamic.Enabled = true;
e.Cancel = true;
} private void timer_dynamic_Tick(object sender, EventArgs e)
{
this.Top += offset;
this.Height -= 70;
if (this.Top > 300)
{
offset = 0;
this.Left += offset1;
this.Width = -50;
if (this.Left > 600)
{
offset1 = 0; }
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Dynamic
{
public partial class Dynamic : Form
{
public Dynamic()
{
InitializeComponent();
} int offset = 25;//垂直增量
int offset1 = 25;//水平增量 private void Dynamic_FormClosing(object sender, FormClosingEventArgs e)
{
timer_dynamic.Enabled = true;
e.Cancel = true;
} private void timer_dynamic_Tick(object sender, EventArgs e)
{
this.Top += offset;
this.Height -= 70;
if (this.Top > 300)
{
offset = 0;
this.Left += offset1;
this.Width = -50;
if (this.Left > 600)
{
offset1 = 0; }
}
}
}
}
是我为了捕获关闭事件。你可以测试下我的代码,我是想在实现那种效果之后在关闭。。
e.Cancel = true;
是我为了捕获关闭事件。你可以测试下我的代码,我是想在实现那种效果之后在关闭。。
{
base.OnFormClosing(e);
if (MessageBox.Show(this, "确定退出系统?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
this.Dispose();
GC.Collect();
Application.Exit();
}
else
{
e.Cancel = true;
}
}
那你直接,在效果演示之后, 就把e.Cancel = false;就行了.
(1)如果自动关闭,那么添加两个临时变量,用于检测何时关闭窗体。并在FormClosing事件中,将e.Cancel=true;改为根据timer的Enabled来调整。具体代码如下(具体变量名与你的不一样,自己改动):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} int offset = 25;//垂直增量
int offset1 = 25;//水平增量 int tempHeight;
int tempWidth; private void timer1_Tick(object sender, EventArgs e)
{
tempHeight=this.Height;
tempWidth=this.Width;
this.Top += offset;
this.Height -= 70;
if (this.Top > 300)
{
offset = 0;
this.Left += offset1;
this.Width -=50;
if (this.Left > 600)
{
offset1 = 0; }
}
if(this.Width==tempWidth&&this.Height==tempHeight)
{
this.Close();
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Enabled = !timer1.Enabled;
if (timer1.Enabled)
{
e.Cancel = true;
}
}
}
(2)如果手动关闭,那很简单,只需更改FormClosing的内容即可。
private void Dynamic_FormClosing(object sender, FormClosingEventArgs e)
{
timer_dynamic.Enabled = true;
timer_dynamic_Tick(null, null);
}