处理消息队列中的所有 Windows 消息

关于Windows 消息队列,相信屏幕前的你,必定多少了解一下

那Windows 是如何处理消息中的消息队列的呢

我们提到如下关键类

Application 

Application.DoEvents Method,微软中的的解释为:处理当前在消息队列中的所有 Windows 消息。

 

示例

下面的代码示例演示如何使用DoEvents方法。 当示例运行时,用户可以从中OpenFileDialog选择图形文件。 选择的文件将显示在窗体中。 DoEvents方法为打开的每个图形文件强制重绘窗体。 若要运行此示例,请将以下代码粘贴到包含PictureBoxPictureBox1OpenFileDialog为的、名OpenFileDialog1为的和名为fileButton的按钮的窗体中。 从窗体InitializeOpenFileDialog的构造函数或Load方法调用和方法。InitializePictureBox

 备注

在 Visual Studio 中,如果您通过OpenFileDialog使用拖动操作将添加到您的窗体中,则您必须通过InitializeOpenFileDialog删除OpenFileDialog创建的新实例的行来修改以下方法。

该示例还Control.Click要求将Button控件的事件OpenFileDialogFileOk的事件连接到示例中定义的事件处理程序。 当示例运行时,单击按钮即可显示该对话框。

C#复制

private void InitializePictureBox()
{
	this.pictureBox1 = new System.Windows.Forms.PictureBox();
	this.pictureBox1.BorderStyle = 
		System.Windows.Forms.BorderStyle.FixedSingle;
	this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
	this.pictureBox1.Location = new System.Drawing.Point(72, 112);
	this.pictureBox1.Name = "pictureBox1";
	this.pictureBox1.Size = new System.Drawing.Size(160, 136);
	this.pictureBox1.TabIndex = 6;
	this.pictureBox1.TabStop = false;
}

private void InitializeOpenFileDialog()
{
	this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();

	// Set the file dialog to filter for graphics files.
	this.openFileDialog1.Filter = 
		"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + 
		"All files (*.*)|*.*";

	// Allow the user to select multiple images.
	this.openFileDialog1.Multiselect = true;
	this.openFileDialog1.Title = "My Image Browser";
	
}

private void fileButton_Click(System.Object sender, System.EventArgs e)
{
	openFileDialog1.ShowDialog();
}


// This method handles the FileOK event.  It opens each file 
// selected and loads the image from a stream into pictureBox1.
private void openFileDialog1_FileOk(object sender, 
	System.ComponentModel.CancelEventArgs e)
{

	this.Activate();
	 string[] files = openFileDialog1.FileNames;

	// Open each file and display the image in pictureBox1.
	// Call Application.DoEvents to force a repaint after each
	// file is read.        
	foreach (string file in files )
	{
		System.IO.FileInfo fileInfo = new System.IO.FileInfo(file);
		System.IO.FileStream fileStream = fileInfo.OpenRead();
		pictureBox1.Image = System.Drawing.Image.FromStream(fileStream);
		Application.DoEvents();
		fileStream.Close();

		// Call Sleep so the picture is briefly displayed, 
		//which will create a slide-show effect.
		System.Threading.Thread.Sleep(2000);
	}
	pictureBox1.Image = null;
}

注解

运行 Windows 窗体时,它将创建新的窗体,然后将等待事件处理。 每次窗体处理一个事件时,它都会处理与该事件关联的所有代码。 所有其他事件都在队列中等待。 当代码处理事件时,应用程序不会响应。 例如,如果将另一窗口拖到顶部,则窗口不会重新绘制。

如果在代码DoEvents中调用,则应用程序可以处理其他事件。 例如,如果你有一个将数据添加到ListBox并添加DoEvents到你的代码中的窗体,则在将另一窗口拖到窗体上时,窗体将重新绘制。 如果从代码DoEvents中删除,则窗体将不会重新绘制,直到按钮的 click 事件处理程序执行完毕。 有关消息传递的详细信息,请参阅中的用户输入 Windows 窗体

与 Visual Basic 6.0 不同, DoEvents方法不会Thread.Sleep调用方法。

通常,在循环中使用此方法来处理消息。

 注意

调用此方法将导致在处理所有等待窗口消息时暂停当前线程。 如果消息导致触发事件,则可能会执行应用程序代码的其他区域。这可能导致应用程序出现难以调试的意外行为。 如果执行的操作或计算需要较长时间,则通常更愿意在新线程上执行这些操作。有关异步编程的详细信息,请参阅异步编程模型(APM)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuyue5945

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值