private void Form2_Resize(object sender, EventArgs e)//最小化时隐藏窗口显示托盘
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false;
this.notifyIcon1.Visible = true;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)//双击托盘恢复窗口
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
protected override void OnClosing(CancelEventArgs e) // 重写了OnClosing事件
{
base.OnClosing(e);
if (MessageBox.Show("您确定要退出?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.No)
{ e.Cancel = true; }
}奇怪的是这个程序运行时,第一次最小化操作会出现MessageBox.Show("您确定要退出?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.No
产生的对话框询问是否要退出,但是非第一次进行最小化操作就不会。
哪位遇到过类似问题?
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false;
this.notifyIcon1.Visible = true;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)//双击托盘恢复窗口
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
protected override void OnClosing(CancelEventArgs e) // 重写了OnClosing事件
{
base.OnClosing(e);
if (MessageBox.Show("您确定要退出?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.No)
{ e.Cancel = true; }
}奇怪的是这个程序运行时,第一次最小化操作会出现MessageBox.Show("您确定要退出?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.No
产生的对话框询问是否要退出,但是非第一次进行最小化操作就不会。
哪位遇到过类似问题?
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false; //这行注释掉试试 this.notifyIcon1.Visible = true;
}
}
this.Visible = false; 试验了以一下如果在单窗口中就不会出现这种情况。出问题的form是别的from
这样 this.Visible = false;
Form2 form2 = new Form2();
form2.ShowDialog();
生成的。
为什么会这样呢?
我认为你不应该在Form2_Resize事件来使窗体不可见,而是在窗体的OnClosing的时候直接把窗体最小化且不在任务栏显示就是了。
环境vs2005 c# .net framework2.050727namespace deleteable3
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
this.Visible = false;
form2.ShowDialog();
form2.Visible = true;
//this.Dispose();//如果执行这个form2中执行form2.visible=false;则程序会完全退出
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{ }
protected override void OnClosing(CancelEventArgs e) // 重写了OnClosing事件
{
base.OnClosing(e);
if (MessageBox.Show("您确定要退出?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.No)
{ e.Cancel = true; }
} private void button1_Click(object sender, EventArgs e)
{
this.Visible = false;
}
}
}
这个问题感觉一部分原因是对microsoft .net的winform 内部机制不够了解,另一部分感觉ms这里确实有点问题,至少有点别扭,继续研究中,看来有比要对winform内部机制详细了解一下!