WPF 分析 Dispatcher PushFrame

千里之行,始于足下

剖析 Dispatcher PushFrame函数,一般用更新动画特效

private void PushFrameImpl(DispatcherFrame frame)
{
 ....
 //一直更新frame 并从window消息队列中获取消息 直到 continue=false
 while (frame.Continue && this.GetMessage(ref msg, IntPtr.Zero, 0, 0))
 {
	this.TranslateAndDispatchMessage(ref msg);
 }
 ....
}

GetMessage  是从Win32API 得到消息,如果有消息没有处理就发送给Win32 Translate和DIspatch消息队列(翻译和派遣)

private void TranslateAndDispatchMessage(ref MSG msg)
{
	if (!ComponentDispatcher.RaiseThreadMessage(ref msg))
	{
	   UnsafeNativeMethods.TranslateMessage(ref msg);
	   UnsafeNativeMethods.DispatchMessage(ref msg);
	}
}

 

public static bool RaiseThreadMessage(ref MSG msg)
{
   ComponentDispatcherThread currentThreadData = CurrentThreadData;
   return currentThreadData.RaiseThreadMessage(ref msg);
}

 其中 ComponentDispatcherThread中_threadFilterMessage是委托,具体发送给 HwndSource

 确认是否处理这个消息,

public bool RaiseThreadMessage(ref MSG msg)
{
	bool flag = false;
	if (this._threadFilterMessage != null)
	{
	  this._threadFilterMessage(ref msg, ref flag);
	}
	if (flag)
	{
	  return flag;
	}
	if (this._threadPreprocessMessage != null)
	{
	  this._threadPreprocessMessage(ref msg, ref flag);
	}
	return flag;
}

 

switch (msgdata.msg.message)
{
	case 256:
	case 260:
	{
	  HwndSource._eatCharMessages = true;
	  DispatcherOperation dispatcherOperation = base.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback(HwndSource.RestoreCharMessages), null);
	  base.Dispatcher.CriticalRequestProcessing(true);
	  msgdata.handled = this.CriticalTranslateAccelerator(ref msgdata.msg, systemModifierKeys);
	  if (!msgdata.handled)
	    {
		  HwndSource._eatCharMessages = false;
		  dispatcherOperation.Abort();
		}
		if (this.IsInExclusiveMenuMode)
		  {
			if (!msgdata.handled)
			  {
				UnsafeNativeMethods.TranslateMessage(ref msgdata.msg);
		      }
		  msgdata.handled = true;
	      }
	break;
}

   在 HwndSource中处理 private object OnPreprocessMessage(object param),具体怎么处理就不讨论了。

总结一下

在Window下开发不管你是用什么界面开发库,你始终是通过系统和用户交互,绕不管window系统消息机制,区别是谁的界面开发库利用 win32API在各种场合下占用资源更少,速度更快。WPF这个方面有大区别以前的winfom界面开发库。

----怎么更新界面

----

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值