Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why I can't get the WM_CREATE msg in the PreFilterMessage method

Why I can't get the WM_CREATE msg in the PreFilterMessage method

Scheduled Pinned Locked Moved C#
graphics
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hwswin
    wrote on last edited by
    #1

    I can get the WM_CREATE msg in WndProc when I click button1,but it didn't fired at the PreFilterMessage. Follow is the source code.Thanks!

    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        \[STAThread\]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
    
            filter ft = new filter();
            Application.AddMessageFilter(ft);
    
            Form1 f = new Form1();
            f.Show();
            Application.Run();
        }
    }
    
    public class filter : IMessageFilter
    {
    
        #region IMessageFilter 成员
    
        const int WM\_CREATE = 0x00001;
        public bool PreFilterMessage(ref Message m)
        {
            `if (m.Msg == WM_CREATE)             {                 MessageBox.Show("A");                 return true;             }`
            return false;
        }
    
        #endregion
    }
    
    public partial class Form1 : Form
    {
        #region Designer
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;
    
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    
        #region Windows 窗体设计器生成的代码
    
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(0, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1\_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new
    
    N 1 Reply Last reply
    0
    • H hwswin

      I can get the WM_CREATE msg in WndProc when I click button1,but it didn't fired at the PreFilterMessage. Follow is the source code.Thanks!

      static class Program
      {
          /// <summary>
          /// 应用程序的主入口点。
          /// </summary>
          \[STAThread\]
          static void Main()
          {
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
      
              filter ft = new filter();
              Application.AddMessageFilter(ft);
      
              Form1 f = new Form1();
              f.Show();
              Application.Run();
          }
      }
      
      public class filter : IMessageFilter
      {
      
          #region IMessageFilter 成员
      
          const int WM\_CREATE = 0x00001;
          public bool PreFilterMessage(ref Message m)
          {
              `if (m.Msg == WM_CREATE)             {                 MessageBox.Show("A");                 return true;             }`
              return false;
          }
      
          #endregion
      }
      
      public partial class Form1 : Form
      {
          #region Designer
          /// <summary>
          /// 必需的设计器变量。
          /// </summary>
          private System.ComponentModel.IContainer components = null;
      
          /// <summary>
          /// 清理所有正在使用的资源。
          /// </summary>
          /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
          protected override void Dispose(bool disposing)
          {
              if (disposing && (components != null))
              {
                  components.Dispose();
              }
              base.Dispose(disposing);
          }
      
          #region Windows 窗体设计器生成的代码
      
          /// <summary>
          /// 设计器支持所需的方法 - 不要
          /// 使用代码编辑器修改此方法的内容。
          /// </summary>
          private void InitializeComponent()
          {
              this.button1 = new System.Windows.Forms.Button();
              this.SuspendLayout();
              // 
              // button1
              // 
              this.button1.Location = new System.Drawing.Point(0, 0);
              this.button1.Name = "button1";
              this.button1.Size = new System.Drawing.Size(75, 23);
              this.button1.TabIndex = 0;
              this.button1.Text = "button1";
              this.button1.UseVisualStyleBackColor = true;
              this.button1.Click += new System.EventHandler(this.button1\_Click);
              // 
              // Form1
              // 
              this.AutoScaleDimensions = new
      
      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Because you have already overridden it in the Form. So I guess additional message filters won't execute. Also use this[^] overload of Application.Run.

      Navaneeth How to use google | Ask smart questions

      H 2 Replies Last reply
      0
      • N N a v a n e e t h

        Because you have already overridden it in the Form. So I guess additional message filters won't execute. Also use this[^] overload of Application.Run.

        Navaneeth How to use google | Ask smart questions

        H Offline
        H Offline
        hwswin
        wrote on last edited by
        #3

        Thanks, I remove the override method of WndProc from form1,but it didn't work fine. PS:We can capture all msgs except the WM_CREATE in PreFilterMessage method.

        N a v a n e e t h wrote:

        Also use this[^] overload of Application.Run.

        I have changed Form1 f = new Form1(); f.Show(); Application.Run(); to Application.Run(new Form1()); May be I didn't clear what you mean.

        1 Reply Last reply
        0
        • N N a v a n e e t h

          Because you have already overridden it in the Form. So I guess additional message filters won't execute. Also use this[^] overload of Application.Run.

          Navaneeth How to use google | Ask smart questions

          H Offline
          H Offline
          hwswin
          wrote on last edited by
          #4

          It seems it wasn't catch by PreFilterMessage. Is there some way to catch WM_CREATE for all forms in my application without override those WndProc in each winform?

          N 2 Replies Last reply
          0
          • H hwswin

            It seems it wasn't catch by PreFilterMessage. Is there some way to catch WM_CREATE for all forms in my application without override those WndProc in each winform?

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            I reproduced the problem you are saying. Sadly, I don't have any solution to offer.

            hwswin wrote:

            Is there some way to catch WM_CREATE for all forms in my application without override those WndProc in each winform?

            Yes. Just create a class which derives from System.Windows.Forms.Form and override the WndProc there. You can derive all your forms from this class. :)

            Navaneeth How to use google | Ask smart questions

            1 Reply Last reply
            0
            • H hwswin

              It seems it wasn't catch by PreFilterMessage. Is there some way to catch WM_CREATE for all forms in my application without override those WndProc in each winform?

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              Ok, Here is my thought on this The filter added using Application.AddMessageFilter will only get called for messages posted to the message queue. Some win32 messages like WM_CREATE will be sent directly to the control. If you see the win32 APIs, there are SendMessage and PostMessage available. SendMessage doesn't post the message to queue and the messages sent by SendMessage won't be caught by the filter. PostMessage posts the message to the application message queue and filter gets called. I guess this would be the reason for filter not getting called. :)

              Navaneeth How to use google | Ask smart questions

              H 1 Reply Last reply
              0
              • N N a v a n e e t h

                Ok, Here is my thought on this The filter added using Application.AddMessageFilter will only get called for messages posted to the message queue. Some win32 messages like WM_CREATE will be sent directly to the control. If you see the win32 APIs, there are SendMessage and PostMessage available. SendMessage doesn't post the message to queue and the messages sent by SendMessage won't be caught by the filter. PostMessage posts the message to the application message queue and filter gets called. I guess this would be the reason for filter not getting called. :)

                Navaneeth How to use google | Ask smart questions

                H Offline
                H Offline
                hwswin
                wrote on last edited by
                #7

                Thks again! Greate,Use the hook can figure out my "Question". [See here](http://support.microsoft.com/default.aspx?scid=kb;en-us;318804)[[^](http://support.microsoft.com/default.aspx?scid=kb;en-us;318804 "New Window")]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups