Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
8 Mar 2005 1  
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

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 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