C # Runtime Override Method

I have two questions.

1) I found a small gem of code for how to make control scroll smoothly .

Great. But it overrides the WndProc method, so to use it I had to pull out the FlowLayoutPanel, which I dropped onto the form at design time, subclass FlowLayoutPanel, and then finally instantiate a new class and create all the properties manually and change all the links for the this control. Controls ["ControlName"]. (Or, I think, I could create a class level variable that was essentially a control, but how do they allow you to use intellisense on it when it is not declared anywhere?)

So, now I'm just wondering if there really is a way to execute.

Is it possible to do something simple like this, where MainPanel is the name of the control:

MainPanel = (SmoothScrollingFlowLayoutPanel)MainPanel

It can't be that easy, can it? However, this is annoying because I still have to have a subclass (which may be a good design decision, but I would like it to be free of it). Thus, we could put the code in the parent element of the FlowLayoutPanel something like this:

private Delegate void WndProcHandler(ref Message m);
private WndProcHandler w;

public void SomeCode() {
   w = MainPanel.WndProc; // get reference to existing wndproc method
   MainPanel.WndProc = WndProcSmoothScroll; //replace with new method
}

private void WndProcSmoothScroll(ref Message m) { // make smooth scrolling work
   if (
      (m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL)
      && (((int)m.WParam & 0xFFFF) == 5)
   ) {
      m.WParam = (IntPtr)(((int)m.WParam & ~0xFFFF) | 4);
   }
   if (w != null) { w(); }
   base.WndProc(ref m);
  }

I understand that this is probably quite naive. I view the WndProc method as an event, and it is not.

2) So, the second question: if WndProc was an event instead of a method, how would I do the same - save a copy of the original list of event handlers for the event, set my own event handler to trigger first call all the original event handlers?

BITS INCLUDED

, - , :

//m.WParam = (IntPtr)(((int)m.WParam & ~0xFFFF) | 4);
m.WParam = (IntPtr)((int)m.WParam ^ 1);

16 5 4, (XOR), AND, OR.

+5
2

, , , WndProc . , , , Win32.

"", , . Handle . Win32 WndProc, . , Winforms Win32. System.Windows.Forms.NativeWindow .NET .

, :

class SmoothScrollIntercept : System.Windows.Forms.NativeWindow
{
    public SmoothScrollIntercept(IntPtr hWnd)
    {
        // assign the handle and listen to this control WndProc
        this.AssignHandle(hWnd);
    }

    protected override void WndProc(ref Message m)
    {
        // listen to WndProc here, do things

        if ((m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL)
            && (((int)m.WParam & 0xFFFF) == 5)) 
        {
            m.WParam = (IntPtr)(((int)m.WParam & ~0xFFFF) | 4);
        }

        base.WndProc(ref m);
    } 
}

:

SmoothScrollIntercept intercept = new SmoothScrollIntercept(myControl.Handle);

// myControl is now using smooth scrolling, without inheriting from the control
+8

, , , . , .

, , . add remove; (-), .

, , IMessageFilter , .

IMessageFilter , PreFilterMessage; . WndProc Form . .

+2

All Articles