Hi,
I am trying to dynamically install/deinstall a message handler to a
System.Windows.Forms.Form using NativeWindow.
I do not use IMessageFilter derived class because it intercept only the
PostMessage messages.
My problem is that when I install the message handler the second time a
"Fatal stack overflow error" occurs.
To reproduce my problem you have only to start the program at the end of
this post, click the left mouse button on the form and wait for a while.
My test program install the NativeWindow message handler on start.
Clicking the left mouse button the program deinstall and reinstall the
message handler.
Many thanks for any help.
Marco.
////////////////////
//Code start
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TestNativeWindows
{
/// <summary>
/// Summary description for MyForm.
/// </summary>
public class MyForm
: System.Windows.Forms.Form
{
private MyNativeWindow m_MessageHandler;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MyForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
m_MessageHandler = new MyNativeWindow(this);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
m_MessageHandler.ReleaseHandle();
m_MessageHandler = null;
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// MyForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(480, 88);
this.Name = "MyForm";
this.Text = "MyForm";
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyForm _MouseDown);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MyForm());
}
private void MyForm_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
m_MessageHandler.Acquire(this);
else if (e.Button == System.Windows.Forms.MouseButtons.Middle)
System.Diagnostics.Debug.WriteLine("WM_MBUTTONDOWN handled
by the FORM message handler");
else if (e.Button == System.Windows.Forms.MouseButtons.Right)
m_MessageHandler.ReleaseHandle();
}
}; // MyForm
public class MyNativeWindow
: NativeWindow
{
private const int WM_MBUTTONDOWN = 0x0207;
private System.Windows.Forms.Form m_parent = null;
public void Acquire(System.Windows.Forms.Form parent)
{
this.ReleaseHandle();
m_parent = parent;
AssignHandle(m_parent.Handle);
System.Diagnostics.Debug.WriteLine("Acquire");
}
public override void ReleaseHandle()
{
// Window was destroyed, release hook.
if (m_parent != null)
{
base.ReleaseHandle();
m_parent = null;
System.Diagnostics.Debug.WriteLine("Release");
}
}
public MyNativeWindow(System.Windows.Forms.Form parent)
{
System.Diagnostics.Debug.WriteLine("MyNativeWindow ");
Acquire(parent);
}
~MyNativeWindow()
{
System.Diagnostics.Debug.WriteLine("~MyNativeWindo w");
this.ReleaseHandle();
}
protected override void OnHandleChange()
{
System.Diagnostics.Debug.WriteLine("OnHandleChange (" +
Handle.ToString() + ") ");
}
[System.Security.Permissions.PermissionSet(System.S ecurity.Permissions.SecurityAction.Demand,
Name="FullTrust")]
protected override void WndProc(ref Message m)
{
// Listen for operating system messages
switch (m.Msg)
{
case WM_MBUTTONDOWN:
System.Diagnostics.Debug.WriteLine("WM_MBUTTONDOWN
handled by NATIVE_WINDOW message handler");
return;
}
base.WndProc(ref m);
}
}; //class MyNativeWindow
}
//Code end
////////////////////
--
For direct reply change underscore to dot