这个问题很简单,但是蹊跷!我在线等着:)

幽兔 2008-09-08 09:51:48
我把我的程序贴上来。
很简单的一个Simple程序。
问题出在最小化时
为什么最小化的时候在Taskbar延迟显示?我是新手

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CsharpLearnStep1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.TopMost = true;
}
private bool isMouseDown = false;
private Point FormLocation;
private Timer timer1;
private IContainer components; //form的location
private Point mouseOffset;
private Button button1;
private Button button2;
private Timer timer2;
private bool isLoadOver = false;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = true;
FormLocation = this.Location;
mouseOffset = Control.MousePosition;
}
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
int _x = 0;
int _y = 0;
if (isMouseDown)
{
Point pt = Control.MousePosition;
_x = mouseOffset.X - pt.X;
_y = mouseOffset.Y - pt.Y;

this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
}
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer2 = new System.Windows.Forms.Timer(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 30;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// timer2
//
this.timer2.Interval = 25;
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
this.button1.Location = new System.Drawing.Point(330, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(19, 18);
this.button1.TabIndex = 0;
this.button1.Text = "×";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
this.button2.Location = new System.Drawing.Point(310, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(19, 18);
this.button2.TabIndex = 0;
this.button2.Text = "-";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoSize = true;
this.BackColor = System.Drawing.SystemColors.MenuHighlight;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.ClientSize = new System.Drawing.Size(349, 78);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form1";
this.Opacity = 0.1;
this.ShowInTaskbar = false;
this.TopMost = true;
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Resize += new System.EventHandler(this.Form1_Resize);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.ResumeLayout(false);

}

/// <summary>
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

private void Form1_Load(object sender, EventArgs e)
{
}

private void button1_Click(object sender, EventArgs e)
{

timer1.Enabled = false;
timer2.Start();

}
private void button2_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}

private void timer1_Tick(object sender, EventArgs e)
{
this.Opacity += 0.05;
if (this.Opacity >= 1.0)
{
isLoadOver = true;
this.timer1.Tick -= timer1_Tick;
}


}

private void timer2_Tick(object sender, EventArgs e)
{
this.Opacity -= 0.05;
if (Opacity <= 0) Application.Exit();
}

private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{

}

private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{

}

private void Form1_Resize(object sender, EventArgs e)
{
if (isLoadOver)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = true;
}
else
{
this.ShowInTaskbar = false;

}
}
}

}
}
...全文
156 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
吟风 2008-09-08
  • 打赏
  • 举报
回复
我没有测试你的例子,没有你的代码哦,呵呵。
幽兔 2008-09-08
  • 打赏
  • 举报
回复
3Q不过我这里还是不行啊。
能把代码贴上来吗?
gaocumt 2008-09-08
  • 打赏
  • 举报
回复
学习学习
try530 2008-09-08
  • 打赏
  • 举报
回复
同意楼上
吟风 2008-09-08
  • 打赏
  • 举报
回复
刚才试验了下,如果将this.ShowInTaskbar = true
在this.WindowState = FormWindowState.Minimized之后运行就会出现你所说的延迟显示的现象,
但是如果在this.WindowState = FormWindowState.Minimized之前就将this.ShowInTaskbar = true,那么就不会出现该延迟。

我理解的是:前者最小化的时候,判断该窗体不在状态栏显示,所以隐藏了,而后面的代码却又将该窗体的状态栏显示设置为true,所以重新绘制显示出来,这次重绘就是延迟的原因。
个人看法,请大家指教
幽兔 2008-09-08
  • 打赏
  • 举报
回复
这个我试过呀,不过点最小化按钮到TaskBar显示这段时间哪里都不走。所有断点没反应啊
whitechololate 2008-09-08
  • 打赏
  • 举报
回复
可以设断点跟一下,没做过这样C/S的
幽兔 2008-09-08
  • 打赏
  • 举报
回复
怎么没人呢??????????
wdgphc 2008-09-08
  • 打赏
  • 举报
回复
呵呵,看到最后自己解决了.GX
幽兔 2008-09-08
  • 打赏
  • 举报
回复
谢谢大家,现在问题已经解决了。
把代码贴上来。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CsharpLearnStep1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.TopMost = true;
this.DoubleBuffered = true;
}
private bool isMouseDown = false;
private Point FormLocation;
private Timer timer1;
private IContainer components; //form的location
private Point mouseOffset;
private Button button1;
private Button button2;
private Timer timer2;
private bool isMin = false; //表示当前窗口状态

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = true;
FormLocation = this.Location;
mouseOffset = Control.MousePosition;
}
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
int _x = 0;
int _y = 0;
if (isMouseDown)
{
Point pt = Control.MousePosition;
_x = mouseOffset.X - pt.X;
_y = mouseOffset.Y - pt.Y;

this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
}
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer2 = new System.Windows.Forms.Timer(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 30;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// timer2
//
this.timer2.Interval = 25;
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
this.button1.Location = new System.Drawing.Point(330, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(19, 18);
this.button1.TabIndex = 0;
this.button1.Text = "×";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
this.button2.Location = new System.Drawing.Point(310, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(19, 18);
this.button2.TabIndex = 0;
this.button2.Text = "-";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoSize = true;
this.BackColor = System.Drawing.SystemColors.MenuHighlight;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.ClientSize = new System.Drawing.Size(349, 78);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form1";
this.Opacity = 0.1;
this.ShowInTaskbar = false;
this.TopMost = true;
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Resize += new System.EventHandler(this.Form1_Resize);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.ResumeLayout(false);

}

/// <summary>
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

private void Form1_Load(object sender, EventArgs e)
{
}

private void button1_Click(object sender, EventArgs e)
{

timer1.Enabled = false;
timer2.Start();

}
private void button2_Click(object sender, EventArgs e)
{
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Minimized;
this.isMin = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
this.Opacity += 0.05;
if (this.Opacity >= 1.0)
{
this.timer1.Tick -= timer1_Tick;
}


}

private void timer2_Tick(object sender, EventArgs e)
{
this.Opacity -= 0.05;
if (Opacity <= 0) Application.Exit();
}


private void Form1_Resize(object sender, EventArgs e)
{
if (isMin == true)
{
isMin = false;
this.ShowInTaskbar = false;
}
}




}
}
结贴了!哈哈!
幽兔 2008-09-08
  • 打赏
  • 举报
回复
Oh my God !
我发现
设置
ShowInTaskbar
WindowState
这两个属性的时候会触发Resize和sizeChanged事件。
现在最小化没有问题了,可是恢复大小的时候我想让状态栏消失。
可是如果在Resize事件处理方法中写ShowInTaskbar=false
那这句话又会触发Resize事件,这样就会进入死循环。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧