Why I can't get the WM_CREATE msg in the PreFilterMessage method
-
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
-
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
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
-
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
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();
toApplication.Run(new Form1());
May be I didn't clear what you mean. -
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
-
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?
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 theWndProc
there. You can derive all your forms from this class. :)Navaneeth How to use google | Ask smart questions
-
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?
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 areSendMessage
andPostMessage
available.SendMessage
doesn't post the message to queue and the messages sent bySendMessage
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
-
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 areSendMessage
andPostMessage
available.SendMessage
doesn't post the message to queue and the messages sent bySendMessage
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