Control.DefWndProc

Sends the specified message to the default window procedure.

参数说明:m:The Windows Message to process.

protected virtual void DefWndProc(ref Message m)

备注:

For more information about processing Windows messages, see the WindowProc function.

Control.WndProc

Processes Windows messages.

参数说明:m:The Windows Message to process.

protected virtual void WndProc(ref Message m)

备注:

All messages are sent to the WndProc method after getting filtered through the PreProcessMessage method.

在PreProcessMessage方法对消息进行筛选之后,所有的消息都发送到了WndProc方法

The WndProc method corresponds exactly to the Windows WindowProc function. For more information about processing Windows messages, see the WindowProc function.

WndProc 方法对应于windows系统中的WindowProc 函数

Notes to Inheritors

Inheriting controls should call the base class's WndProc method to process any messages that they do not handle.

如果继承自Control的类,在override的WndProc方法中有没有处理的消息,那么就需要调用base.WndProc 方法来处理消息

http://stackoverflow.com/questions/19529878/where-to-call-base-wndproc-or-base-defwndproc

What exactly is the difference between WndProc and DefaultWndProc?

There is no method named "DefaultWndProc", I'll assume you are talking about DefWndProc. This question is difficult to answer since there is very little difference between the two. The DefWndProc() method corresponds to the way you write code in a language like C, the ability to call base.WndProc() is specific to .NET. They do the same thing, call the original window procedure of the window but with a small difference. The base.WndProc() method is capable of altering the message completely, DefWndProc() can only alter the Message.Result value. I can't think of a case where this matters.

The MSDN Library article for Control.WndProc() otherwise helps to remove the doubt, it stipulates that if you override the method then you should always use base.WndProc().

what is DefaultWndProc for, which I can call anytime?

Focusing on the "anytime" part of the phrase, this is very rarely the correct thing to do. You should almost always pinvoke SendMessage() to send a message to a window. Calling DefWndProc() should only ever be used if you intentionally want to bypass a custom WndProc() method. That is rare.

And where to call base.WndProc in my overridden method?

That depends on what you want to accomplish. There are three basic strategies:

  • Look at the m argument and implement your own custom behavior, then call base.WndProc(). This is the most common way and ought to be your default choice.
  • Call base.WndProc() first, then alter the m argument or execute code to customize the default processing of the message. This is appropriate for certain kind of messages, WM_NCHITTEST is the best example. Your WM_PAINT case is another one, if you need to paint on top of what was painted by the default window procedure then you have to do it this way.
  • Not call base.WndProc() at all. Appropriate if you completely customize the message handling and don't want to use the default behavior. Very common for filtering message, this is how KeyPressEventArgs.Handled works for example.

Exactly which bullet is appropriate requires insight in the way the message gets handled normally. This entirely depends on the specific control you derive from and the specific message so its impossible to give generic guidance. Getting it wrong however is almost always easy to diagnose.

http://www.cnblogs.com/08shiyan/archive/2012/01/06/2313864.html

谈到Winform的消息处理,多数时候是通过事件处理程序进行的,但当没有对应的事件时通常的做法是声明DefWndProc或者WndProc或者IMessageFilter

所有的有用户界面的控件都继承自Control,这种方式需要创建对应控件的派生类,不能统一对各个窗口的消息进行拦截处理,因为从根本上说这两者都是Windows的窗口过程,只有收到针对本窗口自身的消息。

通过复习Windows的消息处理机制,对这三者的关系可以有更好的理解。

应用程序的消息来自于系统消息队列,被应用程序的主程序中的消息循环所处理。

这个消息循环从应用程序的消息队列中取出消息,进行预处理,然后派发到消息对应的窗口过程,窗口过程在被调用后根据消息的类型进行相应的处理,有些可以由Windows默认处理的消息就调用Windows的DefWindowProc。

这里的WndProc就是对应控件窗口的窗口过程,而DefWndProc会被WndProc调用,处理那些WndProc中未处理的消息(包括WndProc未吞掉的),因此DefWndProc收到的消息会比WndProc少。

IMessageFilter的调用发生在应用程序的消息循环中,是消息预处理的一部分,所以它收到的消息是更全的(除了直接发送到窗口过程不进入消息队列的那些消息)。

三者都有一个共同的参数类型Message,它封装了Windows消息。同时还包括一个很方便的ToString方法,可以将Message对象转换成包括消息名称(WM_XXX)在内的字符串,

通过Reflector可以看到实现是通过一个内部类MessageDecoder,使用一个很长的switch语句将消息ID转换成消息名称。

What exactly is the difference between WndProc and DefaultWndProc?的更多相关文章

  1. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  2. What's the difference between a stub and mock?

    I believe the biggest distinction is that a stub you have already written with predetermined behavio ...

  3. [转载]Difference between <context:annotation-config> vs <context:component-scan>

    在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:componen ...

  4. What's the difference between <b> and <strong>, <i> and <em> in HTML/XHTML? When should you use each?

    ref:http://stackoverflow.com/questions/271743/whats-the-difference-between-b-and-strong-i-and-em The ...

  5. difference between forward and sendredirect

    Difference between SendRedirect and forward is one of classical interview questions asked during jav ...

  6. win32 wndproc 返回值

    LRESULT CALLBACK WndProc(...) { case WM_CREATE: .... return 0; case WM_DESTROY: PostQuitMessage (0) ...

  7. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

  8. MySQL: @variable vs. variable. Whats the difference?

    MySQL: @variable vs. variable. Whats the difference?   up vote351down votefavorite 121 In another qu ...

  9. Distribute numbers to two “containers” and minimize their difference of sum

    it can be solved by Dynamical Programming.Here are some useful link: Tutorial and Code: http://www.c ...

随机推荐

  1. Net预编译 真的好用与否

    公司手机网站上千个 ASP.NET开发一套程序只是配置不一样,所有站点呈现的内容就不一样了, 以前的程序是ASP的,现在ASP程序猿少之又少了,所以公司要求转.NET,新开发也用NET.所有现在上千个 ...

  2. ASP.NET问题处理---“数据请求超时错误“”

    数据请求超时,一般有2中解决方式: 1.页面AJAX处理数据时延长时间: 2.后台数据库连接取数据时延长时间. 由于我的后台数据库连接取数据为循环读取数据,所以不存在超时问题,这里具体说说如何修改AJ ...

  3. c语言学习之基础知识点介绍(十六):文件操作

    一.文件的分类 1.文本文件:打开之后能看得懂的文件 2.二进制文件:打开之后看不懂,类似乱码之类的文件(视频,音频打开之后,能看.听,是应为电脑中装有播放器,播放器中含有解码器). 二.操作文件的步 ...

  4. linux下apache-tomcat的安装

    一.JDK安装 1.安装JDK软件包 本例使用的JDK安装包为jdk-6u19-linux-x64.bin,该包是一个编译好的二进制可执行程序包,只需要执行即可安装. 首先进入存放JDK安装包的目录( ...

  5. Visual C++ 打印编程技术-编程基础-获取打印机

    标准方法是用: EnumPrinters() 函数获取 #define PRINTER_ENUM_DEFAULT 0x00000001 #define PRINTER_ENUM_LOCAL 0x000 ...

  6. Apache配置命令

    Apache的主配置文件: 1.DocumentRoot——指定网站的根目录 提示:该目录必须存在.目录上不能有汉字或空格. 2.DirectoryIndex (1)描述:设置网站的默认首页文件.访问 ...

  7. 手把手教你写电商爬虫-第四课 淘宝网商品爬虫自动JS渲染

    版权声明:本文为博主原创文章,未经博主允许不得转载. 系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 手把手教你写电商爬虫-第三课 ...

  8. Spring+Maven+Eclipse构建Web工程

    转载请注明出处:http://www.cnblogs.com/lidabnu/p/5657439.html 1 环境准备 下载Eclipse:http://www.eclipse.org/downlo ...

  9. 第5章标准I/O库总结

    1 fwide函数试图设置流的定向(流的定向决定了读写单字节还是多字节字符) int fwide(FILE *fp,int mode) 宽定向返回正值,字节定向返回负值,为定向返回0 已定向流不会改变 ...

  10. centOS 6.4 mysql安装

    1 直接安装 yum install mysql mysql-server 2 启动mysql /usr/bin/mysqld_safe --user=mysql & 启动时,如果出现Can' ...