5,699,431 members and growing! (16,103 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

Customize a panel with Autoscroll property

By Manalee software

Use all events of scrollbars in a panel with Autoscroll=true
C#, Windows, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 2 May 2004
Updated: 2 May 2004
Views: 130,198
Bookmarked: 58 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
28 votes for this Article.
Popularity: 6.34 Rating: 4.38 out of 5
2 votes, 7.1%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
4 votes, 14.3%
4
22 votes, 78.6%
5

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

About the Author

Manalee software


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.
Occupation: Web Developer
Location: France France

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 46 (Total in Forum: 46) (Refresh)FirstPrevNext
Generallicensing issuememberjohnd123459:43 1 Apr '08  
GeneralThat's just what I need.memberlzy15622:15 4 Jan '08  
GeneralThank you.memberMuaddubby9:22 7 Dec '07  
QuestionThe VisibleAutoScrollHorizontal is ignored [modified]membertbenami7:13 13 Jun '07  
GeneralSynchronizing Scroll Panels...Works on some machines not others. [modified]memberSolution_007:15 7 Dec '06  
GeneralRe: Synchronizing Scroll Panels...Works on some machines not others.memberSolution_0014:14 7 Dec '06  
GeneralDoesn't capture all scroll eventsmemberTimmers9:54 25 Sep '06  
GeneralAlways onmemberJimdango1:33 27 Jun '06  
GeneralDisposing the panelmemberpjhanb4:53 19 Apr '06  
GeneralKoolmemberkumagKayo0:49 8 Mar '06  
GeneralScrollbars look in a Win98-style under .NET Framework 2.0memberasm12314:06 12 Feb '06  
GeneralAnother approachmemberNetSpore8:09 9 Oct '05  
GeneralMouse whell Scrollmemberststeven12:00 16 Sep '05  
GeneralRe: Mouse whell Scrollmemberensrimad7:33 5 Jul '06  
GeneralA few bugs foundmembergdalgas8:37 29 Aug '05  
GeneralRe: A few bugs foundmemberCyrus the Virus1:01 20 Jul '08  
GeneralAnother small bugmemberGaryZZZZZ15:40 27 Aug '05  
GeneralC++ code for this examplememberyy67810:44 21 Apr '05