I have two questions.
1) I found a little gem of code for how to make a control scroll smoothly.
Great. But it overrides the WndProc method, so to use it, I had to tear out the FlowLayoutPanel I'd dropped on the form at design time, subclass FlowLayoutPanel, and then finally instantiate my new class and create all the properties manually and change all references to the control to be this.Controls["ControlName"]. (Or I guess I could make a class-level variable which is essentially what the control originally was, though how do they let you use intellisense on it when it's not declared anywhere?)
So now I'm just wondering if there was in fact a runtime way to do it.
Can I do something simple as this, where MainPanel is the name of the control:
MainPanel = (SmoothScrollingFlowLayoutPanel)MainPanel
It can't be that easy, can it? Even so, it's annoying because I still have to have the subclass (which may be a good design decision but I'd like the freedom to one-off it). So would it be possible to put the code into the parent 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 realize this is probably pretty naive. I'm treating the WndProc method like it is an event, and it's not.
2) So then my second question is, if WndProc was an event instead of a method, how would I do the same thing—store a copy of the original list of handlers for an event, install my own event handler to run first, then call all the original event handlers?
TASTY BITS
In case anyone is interested I noticed an optimization possible in the smooth scrolling code:
//m.WParam = (IntPtr)(((int)m.WParam & ~0xFFFF) | 4);
m.WParam = (IntPtr)((int)m.WParam ^ 1);
Since we want to turn the last 16 bits from 5 to 4, we can just flip the last bit (XOR) rather than AND then OR.