Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / MFC

Add XP Style Scrollbar to listctrl, listbox and so on (by self-draw)

Rate me:
Please Sign up or sign in to vote.
3.61/5 (13 votes)
8 Mar 2005CPOL 179.2K   1.9K   21   15
Implement XP style scrollbar by self-draw
Sample Image - self-draw_XPScrollbar.jpg

Introduction

One of my recent projects needed to beautify UI, but when the app does not run on WinXP, the intrinsic scrollbar of common controls is too ugly. So I use self-draw CScrollWnd instead.

At present, only VSCROLL is supported. HSCROLL will be added when I have time to do it, and any help will be appreciated (you can add it by yourself).

Trick

The biggest problem to implement a scrollbar is to locate thumb properly. To solve this problem, I have used many ways, but at the end, a very convenient and reliable method was found: step by step approach.

Code

C++
void CScrollWnd::MoveThumb(CPoint ptOffset)

{ if(!IsMouseInRange(WM_VSCROLL))
  return;
 CPoint ptCursor;
 GetCursorPos(&ptCursor);
 ScreenToClient(&ptCursor); 

 CPoint ptCurPos = ptCursor;
 int nMaxTimes=200;

 int nTimes = 0;
 int nStep = 1;//accuracy

 CPoint ptNewPos = ptOffset + GetRect(HT_THUMB).CenterPoint();
 while(abs(ptNewPos.y - ptCurPos.y)>nStep && nTimes++<nMaxTimes)
 {
  BOOL bDirDown = ptCurPos.y>ptNewPos.y;
  NotifyBuddy(WM_VSCROLL,MAKEWPARAM(bDirDown?SB_LINEDOWN:SB_LINEUP,0));
  
  UpdateScrollBar();
  ptNewPos = ptOffset + GetRect(HT_THUMB).CenterPoint();
 }
}

See the attached source code for more details.

Sorry for my poor English. My mother tongue is Chinese. :)

History

  • 8th March, 2005: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
xwp
Software Developer www.MayGion.com
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Answerwhat is this. Pin
zonesowhat12-Jun-14 22:02
zonesowhat12-Jun-14 22:02 
GeneralMy vote of 5 Pin
dlsyaim6-Nov-11 16:58
dlsyaim6-Nov-11 16:58 
GeneralCombbox Pin
lroels20-Aug-07 20:48
lroels20-Aug-07 20:48 
Generalsocksv5 specification, details Pin
avinu28-Jul-07 21:56
avinu28-Jul-07 21:56 
GeneralError in Zip File Pin
Srirama Kalyan7-May-07 4:46
Srirama Kalyan7-May-07 4:46 
GeneralRe: Error in Zip File Pin
Svetoslav Chekanov9-May-07 21:27
Svetoslav Chekanov9-May-07 21:27 
GeneralError in ZIP file Pin
ConradB5-Oct-06 19:38
ConradB5-Oct-06 19:38 
Hi XWP,

Is it possible to get a copy of the source for this project from you? The zip file publish ed here has serious error.

I appreciate your help.
Blush | :O

Regards,
CBB
GeneralGood Pin
vcfans23-Mar-06 15:02
vcfans23-Mar-06 15:02 
GeneralCan't unzip the zip file Pin
chinni_uday1-Mar-06 20:10
chinni_uday1-Mar-06 20:10 
GeneralFailed to add HScroll Pin
Sandeep A.C15-Feb-06 0:15
Sandeep A.C15-Feb-06 0:15 
GeneralUnable to Unzip Pin
K.Senthil Babu14-Jul-05 20:59
K.Senthil Babu14-Jul-05 20:59 
GeneralRe: Unable to Unzip Pin
ckTan8128-Jul-05 18:59
ckTan8128-Jul-05 18:59 
Generalcant download Pin
Sam Chris5-May-05 2:58
Sam Chris5-May-05 2:58 
GeneralRedraw bug on Win2000 Pin
Kochise8-Mar-05 21:19
Kochise8-Mar-05 21:19 
GeneralRe: Redraw bug on Win2000 Pin
xwp9-Mar-05 14:35
xwp9-Mar-05 14:35 

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

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