Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 991633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:08:27+00:00 2026-05-16T06:08:27+00:00

I have two questions. 1) I found a little gem of code for how

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:08:28+00:00Added an answer on May 16, 2026 at 6:08 am

    If I understand your question correctly, all you want to do is override WndProc at runtime. If so, all you need is a little Win32 magic.

    Every control has a “handle” which identifies it so the operating system can send messages to it. This handle is exposed through the Handle property on every control. The underlying Win32 system actually allows you to listen to any control’s WndProc as long as you have its handle. This means that you don’t have to inherit from a Winforms control to modify its Win32 behavior. System.Windows.Forms.NativeWindow in .NET wraps this underlying functionality.

    Here’s an example of how you might accomplish this:

    class SmoothScrollIntercept : System.Windows.Forms.NativeWindow
    {
        public SmoothScrollIntercept(IntPtr hWnd)
        {
            // assign the handle and listen to this control's 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);
        } 
    }
    

    Then in the code behind, attach the intercept to the control:

    SmoothScrollIntercept intercept = new SmoothScrollIntercept(myControl.Handle);
    
    // myControl is now using smooth scrolling, without inheriting from the control
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions: 1) How can I make an array which points to
I actually have two questions regarding the same problem but I think it is
I have two questions. Do realloc() and memcpy() copy the entries in an array
This might be a bit of a silly question but; If I have two
I have a two part question Best-Practice I have an algorithm that performs some
I have two String.printable mysteries in the one question. First, in Python 2.6: >>>
I have two tables (well, two relevant for this question) : Bets (holds the
Actually, this question seems to have two parts: How to implement pattern matching? How
Ok, NHibernate question here. I have two objects that I would like to map
I have a two classes: public class Question { public IList<Answer> Answers { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.