Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Trap scrolling messages for the ScrollBars of a TScrollBox


How can I trap the scrolling messages for the ScrollBars of a TScrollBox component?   

    The following example traps the scrolling messages for a TScrollBox component, keeping the two scroll bars in sync. If either of the scroll bars are scrolled, then the position of the other scroll bar is changed to the same value. The scroll messages are trapped by subclassing the scroll box's WinProc.

    Example:
    type
    {$IFDEF WIN32}
      WParameter = LongInt;
    {$ELSE}
      WParameter = Word;
    {$ENDIF}
      LParameter = LongInt;
    {Declare a variable to hold the window procedure we are replacing}
    var
      OldWindowProc : Pointer;
    function NewWindowProc(WindowHandle : hWnd;
                           TheMessage   : WParameter;
                           ParamW       : WParameter;
                           ParamL       : LParameter) : LongInt
    {$IFDEF WIN32} stdcall; {$ELSE} ; export; {$ENDIF}
    var
      TheRangeMin : integer;
      TheRangeMax : integer;
      TheRange : integer;
    begin
      if TheMessage = WM_VSCROLL then begin
      {Get the min and max range of the horizontal scroll box}
        GetScrollRange(WindowHandle,
                       SB_HORZ,
                       TheRangeMin,
                       TheRangeMax);
      {Get the vertical scroll box position}
        TheRange := GetScrollPos(WindowHandle,
                                 SB_VERT);
      {Make sure we wont exceed the range}
        if TheRange < TheRangeMin then
          TheRange := TheRangeMin else
        if TheRange > TheRangeMax then
          TheRange := TheRangeMax;
      {Set the horizontal scroll bar}
        SetScrollPos(WindowHandle,
                     SB_HORZ,
                     TheRange,
                     true);
      end;
      if TheMessage = WM_HSCROLL then begin
      {Get the min and max range of the horizontal scroll box}
        GetScrollRange(WindowHandle,
                       SB_VERT,
                       TheRangeMin,
                       TheRangeMax);
      {Get the horizontal scroll box position}
        TheRange := GetScrollPos(WindowHandle,
                                 SB_HORZ);
      {Make sure we wont exceed the range}
        if TheRange < TheRangeMin then
          TheRange := TheRangeMin else
        if TheRange > TheRangeMax then
          TheRange := TheRangeMax;
      {Set the vertical scroll bar}
        SetScrollPos(WindowHandle,
                     SB_VERT,
                     TheRange,
                     true);
      end;
    { Call the old Window procedure to }
    { allow processing of the message. }
      NewWindowProc := CallWindowProc(OldWindowProc,
                                      WindowHandle,
                                      TheMessage,
                                      ParamW,
                                      ParamL);
    end;
    procedure TForm .FormCreate(Sender: TObject);
    begin
    { Set the new window procedure for the control }
    { and remember the old window procedure.       }
      OldWindowProc := Pointer(SetWindowLong(ScrollBox .Handle,
                                             GWL_WNDPROC,
                                             LongInt(@NewWindowProc)));
    end;
    procedure TForm .FormDestroy(Sender: TObject);
    begin
    { Set the window procedure back }
    { to the old window procedure.  }
      SetWindowLong(ScrollBox .Handle,
                    GWL_WNDPROC,
                    LongInt(OldWindowProc));
    end;
    

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use