real challenge! How to implicitly override a Win form method
-
;P//how to override WndProc() Form aForm = new Form() // after this call, aFrom's WndProc() method are replaced by a another func aForm.foo(); public foo() { // code to override WndProc } Can anyone help?
-
;P//how to override WndProc() Form aForm = new Form() // after this call, aFrom's WndProc() method are replaced by a another func aForm.foo(); public foo() { // code to override WndProc } Can anyone help?
You mean, like:
class MyForm : Form
{
protected override void WndProc(ref Message m)
{
// your implementation...
}
}The virtual method is defined in Control, which Form derives from. Marc MyXaml Advanced Unit Testing YAPO
-
You mean, like:
class MyForm : Form
{
protected override void WndProc(ref Message m)
{
// your implementation...
}
}The virtual method is defined in Control, which Form derives from. Marc MyXaml Advanced Unit Testing YAPO
Hi Marc, the way you just pointed out is the conventional one. What I want is dynamicly override the control's method.
-
Hi Marc, the way you just pointed out is the conventional one. What I want is dynamicly override the control's method.
How much more dynamic can you get than what Marc has shown you? Overriding
WinProc
gives the object the chance to inspect the message and determin what to do including pass it along. -
How much more dynamic can you get than what Marc has shown you? Overriding
WinProc
gives the object the chance to inspect the message and determin what to do including pass it along.Ok, suppose I am writting a dll in C#, the dll will send to current window some message. My customs who will use that dll have to define override WndProc method. So I am thinking of create a function so that customs dont need to explicitly write their WndProc.
-
Ok, suppose I am writting a dll in C#, the dll will send to current window some message. My customs who will use that dll have to define override WndProc method. So I am thinking of create a function so that customs dont need to explicitly write their WndProc.
jinzhecheng wrote: My customs who will use that dll have to define override WndProc method. Then use an event handler that they can attach to, and if they handle WndProc themselves, you don't. Something like this:
class MyForm : Form
{
public delegate bool WndProcOverrideDlgt(ref Message m);
public event WndProcOverrideDlgt WndProcOverride;
protected override void WndProc(ref Message m)
{
bool handled=false;
if (WndProcOverride != null)
{
handled=WndProcOverride(m);
}
if (!handled)
{
// do your usual stuff.
}
}
} -
jinzhecheng wrote: My customs who will use that dll have to define override WndProc method. Then use an event handler that they can attach to, and if they handle WndProc themselves, you don't. Something like this:
class MyForm : Form
{
public delegate bool WndProcOverrideDlgt(ref Message m);
public event WndProcOverrideDlgt WndProcOverride;
protected override void WndProc(ref Message m)
{
bool handled=false;
if (WndProcOverride != null)
{
handled=WndProcOverride(m);
}
if (!handled)
{
// do your usual stuff.
}
}
}Marc, AFAIK, delegates declared as events cannot have a return type except void. This is because more than one client could have subscribed to the event, so there is no way to figure out which of those clients' return values should get back. I think the best way is to pass another object with
WndProcOverride
that encapsulates a bool variable and has logic to ensure that once one client has handled, it is set to true etc.. Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
jinzhecheng wrote: My customs who will use that dll have to define override WndProc method. Then use an event handler that they can attach to, and if they handle WndProc themselves, you don't. Something like this:
class MyForm : Form
{
public delegate bool WndProcOverrideDlgt(ref Message m);
public event WndProcOverrideDlgt WndProcOverride;
protected override void WndProc(ref Message m)
{
bool handled=false;
if (WndProcOverride != null)
{
handled=WndProcOverride(m);
}
if (!handled)
{
// do your usual stuff.
}
}
}Hi Marc, your solution is cool, however , you see, you still defined an override version of WndProc INSIDE myForm class, What I want is like this: Form a = new Form(); // after this call, the WndProc is overrided!! overrideWndProc(a); it looks like crazy, I am now wondering if its possible, maybe I should use a proxy, that can seize message before the form get it
-
Hi Marc, your solution is cool, however , you see, you still defined an override version of WndProc INSIDE myForm class, What I want is like this: Form a = new Form(); // after this call, the WndProc is overrided!! overrideWndProc(a); it looks like crazy, I am now wondering if its possible, maybe I should use a proxy, that can seize message before the form get it
A delegate is a lot simpler than a proxy. :) And delegates/events can have a return type, it's just not standard. :) So yes, the real way to do it is to define your own specialized EventArgs class that has the "handled" flag. But yes, if you want something that can hook a form that doesn't implement WndProc and gives you a way to hook it, then you have to do something like this:
PreFilter preFilter=new PreFilter();
Application.AddMessageFilter(preFilter);
...
class PreFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
// ...
}
}That may be more what you're looking for. You can then test if the message's HWnd is the same as the Form's hWnd that you want to hook, using: form.Handle which should be the same thing as the HWnd value. Marc MyXaml Advanced Unit Testing YAPO
-
Hi Marc, your solution is cool, however , you see, you still defined an override version of WndProc INSIDE myForm class, What I want is like this: Form a = new Form(); // after this call, the WndProc is overrided!! overrideWndProc(a); it looks like crazy, I am now wondering if its possible, maybe I should use a proxy, that can seize message before the form get it
But Marc is raising an event there...
class MyForm : Form { public delegate bool WndProcOverrideDlgt(ref Message m); public event WndProcOverrideDlgt WndProcOverride; protected override void WndProc(ref Message m) { bool handled=false; if (WndProcOverride != null) { handled=WndProcOverride(m); } if (!handled) { // do your usual stuff. } } }
So
Form a = new MyForm(); a.WndProcOverride += new WndProcOverrideDlgt(WndProc_Handler); ... bool WndProc_Handler(ref Message m) { // do client stuff here }
p.s If S. Senthil Kumar is right and you can't have bool delegate (I dunno and lazy to look up) you can do it like
public delegate void WndProcOverrideDlgt(ref Message m, ref bool handled);
At least I hope so :) David Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy -
A delegate is a lot simpler than a proxy. :) And delegates/events can have a return type, it's just not standard. :) So yes, the real way to do it is to define your own specialized EventArgs class that has the "handled" flag. But yes, if you want something that can hook a form that doesn't implement WndProc and gives you a way to hook it, then you have to do something like this:
PreFilter preFilter=new PreFilter();
Application.AddMessageFilter(preFilter);
...
class PreFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
// ...
}
}That may be more what you're looking for. You can then test if the message's HWnd is the same as the Form's hWnd that you want to hook, using: form.Handle which should be the same thing as the HWnd value. Marc MyXaml Advanced Unit Testing YAPO
Marc , Finaly got it . Yeah , I think a message filter is the best solution. Thanks! here is a summary: 1st step: // create a filter public class MsgHandler : System.Windows.Forms.IMessageFilter {public bool PreFilterMessage(ref msg){// do it here}} 2nd step: //create a method public void OverrideWndProc(){MsgHandler msgHandler =new MessageHandler(); Application.AddMessageFilter(msgFilter);} 3rd step: user call this func before App run. Thats it!:-D