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. real challenge! How to implicitly override a Win form method

real challenge! How to implicitly override a Win form method

Scheduled Pinned Locked Moved C#
helptutorialquestion
11 Posts 6 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.
  • J Offline
    J Offline
    jinzhecheng
    wrote on last edited by
    #1

    ;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?

    M 1 Reply Last reply
    0
    • J jinzhecheng

      ;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?

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • M Marc Clifton

        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

        J Offline
        J Offline
        jinzhecheng
        wrote on last edited by
        #3

        Hi Marc, the way you just pointed out is the conventional one. What I want is dynamicly override the control's method.

        T 1 Reply Last reply
        0
        • J jinzhecheng

          Hi Marc, the way you just pointed out is the conventional one. What I want is dynamicly override the control's method.

          T Offline
          T Offline
          Tom Larsen
          wrote on last edited by
          #4

          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.

          J 1 Reply Last reply
          0
          • T Tom Larsen

            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.

            J Offline
            J Offline
            jinzhecheng
            wrote on last edited by
            #5

            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.

            M 1 Reply Last reply
            0
            • J jinzhecheng

              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.

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #6

              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 MyXaml Advanced Unit Testing YAPO

              S J 2 Replies Last reply
              0
              • M Marc Clifton

                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 MyXaml Advanced Unit Testing YAPO

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • M Marc Clifton

                  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 MyXaml Advanced Unit Testing YAPO

                  J Offline
                  J Offline
                  jinzhecheng
                  wrote on last edited by
                  #8

                  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

                  M D 2 Replies Last reply
                  0
                  • J jinzhecheng

                    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

                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #9

                    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

                    L 1 Reply Last reply
                    0
                    • J jinzhecheng

                      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

                      D Offline
                      D Offline
                      DavidNohejl
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • M Marc Clifton

                        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

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        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

                        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