Click here to Skip to main content
11,428,876 members (61,940 online)
Click here to Skip to main content

Customize a panel with Autoscroll property

, 2 May 2004
Rate this:
Please Sign up or sign in to vote.
Use all events of scrollbars in a panel with Autoscroll=true

Introduction

This article show how to customize a System.Windows.Forms.Panel to use effectively scrollbars with the AutoScroll property. In this example, you can change enable or visible property of each scrollbar of the panel, receive scrolling events, send events to the panel and change and receive the positions of both scrollbars.

Background

With .Net, use a panel with Autoscroll property is really useful, but you can not receive events of scrollbars and personalize some functionalities like visible or enable properties of only one scrollbar for example.

Using the code

In your solution, add the file ScrollablePanel.cs to your C# project. Then, you can change type of a panel in your project by the ScrollablePanel type. Sorry but I have no time to make a visual component in a DLL.

Now you have new properties to your panel object:

  1. int AutoScrollHPos: To get or set the horizontal scrollbar position
  2. int AutoScrollVPos: To get or set the vertical scrollbar position
  3. int AutoScrollHorizontalMinimum: To get or set the horizontal scrollbar minimum range
  4. int AutoScrollHorizontalMaximum: To get or set the horizontal scrollbar maximum range
  5. int AutoScrollVerticalMinimum: To get or set the vertical scrollbar minimum range
  6. int AutoScrollVerticalMaximum: To get or set the vertical scrollbar maximum range
  7. bool EnableAutoScrollHorizontal: Enable horizontal scrollbar
  8. bool EnableAutoScrollVertical: Enable vertical scrollbar
  9. bool VisibleAutoScrollHorizontal: Visible horizontal scrollbar
  10. bool VisibleAutoScrollVertical: Visible vertical scrollbar

And your panel have now some new events:

  • public event System.Windows.Forms.ScrollEventHandler ScrollHorizontal: receive when the horizontal scroll bar move
  • public event System.Windows.Forms.ScrollEventHandler ScrollVertical: receive when the vertical scroll bar move
  • public event System.Windows.Forms.MouseEventHandler ScrollMouseWheel: receive when mouse wheel move

Explanations about code in the ScrollablePanel class:

First you must create a new class that overrode the System.Windows.Forms.Panel. So, you must override the WndProc function to receive API32 scrolling messages and then to send .Net ScrollEvents.

//
// WndProc function
//
protected override void WndProc(ref Message msg)
{
  base.WndProc(ref msg);
  if (msg.HWnd != this.Handle)
    return;
  switch (msg.Msg)
  {
    //
        // ...
        //

    case WM_VSCROLL:

      try
      {
        ScrollEventType type = getScrollEventType(msg.WParam);
        ScrollEventArgs arg = new ScrollEventArgs(type, 
          GetScrollPos(this.Handle, (int)SB_VERT));
        this.ScrollVertical(this, arg);
      }
      catch (Exception) { }

      break;

    case WM_HSCROLL:

      try
      {
        ScrollEventType type = getScrollEventType(msg.WParam);
        ScrollEventArgs arg = new ScrollEventArgs(type,
         GetScrollPos(this.Handle, (int)SB_HORZ));
        this.ScrollHorizontal(this, arg);
      }
      catch (Exception) { }

      break;

    default:
      break;
  }
}
Then, you can see that you are using two main functions: GetScrollPos and some const from Win32 API. Analyze the source code to see how are calle the Win32 API functions, like it for example:
[DllImport("user32.dll")]
static public extern int GetScrollPos(System.IntPtr hWnd, 
 int nBar);

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, 
 UIntPtr wParam, IntPtr lParam);

...
Note that sometimes, you need the C++ MACRO to get HIWORD or LOWORD from a WParam of a message for example. I use these two functions that seem to be good:
private static int HiWord(int number)
{
  if ((number & 0x80000000) == 0x80000000)
    return (number >> 16);
  else
    return (number >> 16) & 0xffff ;
}

private static int LoWord(int number)
{
  return number & 0xffff;
}

private static int MakeLong(int LoWord, int HiWord)
{
  return (HiWord << 16) | (LoWord & 0xffff);
}

private static IntPtr MakeLParam(int LoWord, int HiWord)
{
  return (IntPtr) ((HiWord << 16) | (LoWord & 0xffff));
}

That's all, enjoy with it in the hope that it will be useful for you...

History

  • Version 1.0: receive all scrolling events and mouse wheel. Send scroll events to your panel. Get or obtain the mouse wheel delta, and ScrollEventType. The range doesn't look like to work for the moment.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Manalee software
Web Developer
France France
Professional in software industry, Olivier Carpentier works in Manalee corporation. Manalee has developed software for RichMedia applications like SMOX Editor, a design software for streaming presentations.

Comments and Discussions

 
Generallicensing issue Pin
johnd123451-Apr-08 9:43
memberjohnd123451-Apr-08 9:43 
GeneralThat's just what I need. Pin
lzy1564-Jan-08 22:15
memberlzy1564-Jan-08 22:15 
GeneralThank you. Pin
Muaddubby7-Dec-07 9:22
memberMuaddubby7-Dec-07 9:22 
QuestionThe VisibleAutoScrollHorizontal is ignored [modified] Pin
tbenami13-Jun-07 7:13
membertbenami13-Jun-07 7:13 
GeneralSynchronizing Scroll Panels...Works on some machines not others. [modified] Pin
Solution_007-Dec-06 7:15
memberSolution_007-Dec-06 7:15 
GeneralRe: Synchronizing Scroll Panels...Works on some machines not others. Pin
Solution_007-Dec-06 14:14
memberSolution_007-Dec-06 14:14 
GeneralDoesn't capture all scroll events Pin
Timmers25-Sep-06 9:54
memberTimmers25-Sep-06 9:54 
GeneralAlways on Pin
Jimdango27-Jun-06 1:33
memberJimdango27-Jun-06 1:33 
GeneralDisposing the panel Pin
pjhanb19-Apr-06 4:53
memberpjhanb19-Apr-06 4:53 
GeneralKool Pin
kumagKayo8-Mar-06 0:49
memberkumagKayo8-Mar-06 0:49 
GeneralScrollbars look in a Win98-style under .NET Framework 2.0 Pin
asm12312-Feb-06 14:06
memberasm12312-Feb-06 14:06 
GeneralAnother approach Pin
NetSpore9-Oct-05 8:09
memberNetSpore9-Oct-05 8:09 
GeneralMouse whell Scroll Pin
ststeven16-Sep-05 12:00
memberststeven16-Sep-05 12:00 
GeneralRe: Mouse whell Scroll Pin
ensrimad5-Jul-06 7:33
memberensrimad5-Jul-06 7:33 
GeneralA few bugs found Pin
gdalgas29-Aug-05 8:37
membergdalgas29-Aug-05 8:37 
GeneralRe: A few bugs found Pin
Cyrus the Virus20-Jul-08 1:01
memberCyrus the Virus20-Jul-08 1:01 
QuestionRe: A few bugs found Pin
Dester3114-Apr-09 4:11
memberDester3114-Apr-09 4:11 
GeneralAnother small bug Pin
GaryZZZZZ27-Aug-05 15:40
memberGaryZZZZZ27-Aug-05 15:40 
GeneralC++ code for this example Pin
yy67821-Apr-05 10:44
memberyy67821-Apr-05 10:44 
GeneralQuestion Pin
verylonguniquename23-Feb-05 4:40
sussverylonguniquename23-Feb-05 4:40 
GeneralToo bad range doesn't work. Pin
kilroytrout7-Dec-04 10:10
memberkilroytrout7-Dec-04 10:10 
GeneralRe: Too bad range doesn't work. Pin
Control Vertex10-Oct-05 22:54
memberControl Vertex10-Oct-05 22:54 
GeneralRe: Fix for setting scroll position Pin
Manalee software30-Sep-04 4:34
memberManalee software30-Sep-04 4:34 
GeneralRe: Fix for setting scroll position Pin
Holger Schmid20-Jun-05 21:39
memberHolger Schmid20-Jun-05 21:39 
GeneralRe: Fix for setting scroll position Pin
annalady5-May-06 5:18
memberannalady5-May-06 5:18 
GeneralRe: Fix for setting scroll position Pin
handa2929-Aug-06 18:35
memberhanda2929-Aug-06 18:35 
GeneralRe: Fix for setting scroll position Pin
Cyrus the Virus25-Jul-08 22:49
memberCyrus the Virus25-Jul-08 22:49 
GeneralFix for setting scroll position Pin
Mathew Hall30-Sep-04 4:12
memberMathew Hall30-Sep-04 4:12 
QuestionHow About WebBroser Control Pin
ggmemo26-Sep-04 18:06
sussggmemo26-Sep-04 18:06 
AnswerRe: How About WebBroser Control Pin
inshua12-Apr-06 0:44
memberinshua12-Apr-06 0:44 
GeneralQuestion about SetScrollPos Pin
Brenton House20-Aug-04 8:01
sussBrenton House20-Aug-04 8:01 
Generalbug in AutoScrollVPos property Pin
Jerome Bellanger6-Jul-04 4:02
sussJerome Bellanger6-Jul-04 4:02 
GeneralRe: bug in AutoScrollVPos property Pin
Anonymous11-Mar-05 9:31
sussAnonymous11-Mar-05 9:31 
GeneralThe mouse wheel event ! Pin
J Joshi29-Jun-04 12:12
sussJ Joshi29-Jun-04 12:12 
GeneralRe: The mouse wheel event ! Pin
Solution_007-Dec-06 7:01
memberSolution_007-Dec-06 7:01 
GeneralStrange... Pin
Jakub Florczyk21-Jun-04 1:50
memberJakub Florczyk21-Jun-04 1:50 
GeneralJust a little bug.. Pin
Manalee software3-May-04 22:12
memberManalee software3-May-04 22:12 
GeneralRe: Just a little bug.. Pin
Mathew Hall30-Sep-04 5:36
memberMathew Hall30-Sep-04 5:36 
GeneralRe: Just a little bug.. Pin
Omri Shaked25-Apr-05 3:46
memberOmri Shaked25-Apr-05 3:46 
QuestionHow about... Pin
leppie3-May-04 10:04
memberleppie3-May-04 10:04 
AnswerRe: How about... Pin
Grut3-May-04 13:44
memberGrut3-May-04 13:44 
AnswerRe: How about... Pin
Manalee software3-May-04 22:08
memberManalee software3-May-04 22:08 
GeneralRe: How about... Pin
leppie4-May-04 1:06
memberleppie4-May-04 1:06 
GeneralRe: How about... Pin
Carnby4-May-04 1:34
memberCarnby4-May-04 1:34 
GeneralRe: How about... Pin
leppie4-May-04 7:53
memberleppie4-May-04 7:53 
GeneralRe: How about... Pin
SteelBugs11-Apr-05 22:53
memberSteelBugs11-Apr-05 22:53 
Generalzoom Pin
Rui Dias Lopes3-May-04 0:29
memberRui Dias Lopes3-May-04 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 3 May 2004
Article Copyright 2004 by Manalee software
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid