Click here to Skip to main content
15,851,387 members
Articles / Desktop Programming / MFC

Ruler Control

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
30 Sep 20021 min read 102.8K   4.3K   57   13
Custom CRulerWnd control

Sample Image - CRulerProject001.jpg


This control is derived from MFC's CWnd class. While you are adding this to your dialogbox, you should select "custom control", whose class name should be CRulerWnd, else, you can't create this control. This control can be located in any dialogbox or View. The RegisterWindowClass() member function will register this window class to Windows. After that, you will not have any problem when you create it. The class will be unregistered in the destructor member function.


#define RULERWINDOW_CLASSNAME  _T( "CRulerWnd" )

    WNDCLASS wndcls;

    HINSTANCE hInst = AfxGetResourceHandle();

    if ( !( ::GetClassInfo( hInst, RULERWINDOW_CLASSNAME , &wndcls ) ) )

       // otherwise we need to register a new class            = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
       wndcls.lpfnWndProc      = ::DefWindowProc;
       wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
       wndcls.hInstance        = hInst;
       wndcls.hIcon            = NULL;
#ifndef _WIN32_WCE_NO_CURSOR
       wndcls.hCursor          = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
       wndcls.hCursor          = 0;
       wndcls.hbrBackground    = (HBRUSH)( COLOR_3DFACE + 1 );
       wndcls.lpszMenuName     = NULL;
       wndcls.lpszClassName    = RULERWINDOW_CLASSNAME;
       if( !AfxRegisterClass( &wndcls ) )

          return FALSE;



You can select your window styles what you want. For instance, WS_EX_MODALFRAME, WS_EX_STATICEDGE etc. to create for miscellaneous appearance. This control has a lot of member functions for setting it up. You can use them to change colors and other things.

//Get Property
DWORD        GetStyle() { return m_dwStyle; }
COLORREF     GetBackGroundColor() { return m_clrBackGround; }
COLORREF     GetMilimeterLineColor() { return m_clrMilimeterLineColor; }
COLORREF     GetTextColor() { return m_clrTextColor; }
UINT         GetStartSeperateSize() { return m_nSeperateSize; }
UINT         GetMargin() { return m_nRulerMargin; }
UINT         GetMilimeterPixel() { return m_nMilimeterPixel; }
UINT         GetSeperatorSize() { return m_nSeperatorSize; }
long         GetScrollPos() { return m_lScrolPos; }
CWnd*      GetMessageTarget() { return m_pMessageTarget; }

//Set Property
BOOL     SetStyle( DWORD dwStyle );
BOOL     SetBackGroundColor( COLORREF clr );
BOOL     SetMilimeterLineColor( COLORREF clr );
BOOL     SetTextColor( COLORREF clr );
BOOL     SetStartSeperateSize( UINT nSize );
BOOL     SetMargin( UINT nMargin );
BOOL     SetMilimeterPixel( UINT nPixel );
BOOL     SetSeperatorSize( UINT nSize );
BOOL     SetScrollPos( long lPos );
BOOL     SetMessageTarget( CWnd *pTarget = NULL );
Additionaly, you can add a separator, delete it, change position etc.
SEPERATOR_TYPE* GetSeperator( int iID );
int DeleteAllSeperator();
int DeleteSeperator( int iID );
int AddSeperator( int iPos , int iID , int iType = 0 , 
   LPARAM lParam = NULL ,
   COLORREF  clrLine = RGB( 0 , 0 , 0 ) ,                                    
   COLORREF clrFill = RGB( 255 ,255 , 220 ) , 
   int iMinMargin = 0 , int iMaxMargin = 0xFFFFFFF ); 
For taking separators, use CPtrArray collection.
typedef struct _tagSEPERATOR_TYPE{

   int      iPos;
   int      iType;
   int      iID;
   COLORREF  clrLine;
   COLORREF  clrFill;

   int      iMinMargin;
   int      iMaxMargin;

   LPARAM lParam;


This structure is separate and to the point for each separator above. Additionaly, you can set up each separator which can have color, position, id number and motion zone. These are shown as below. All notifications can be sent to whichever window will be the target. This notification is as below:

//Main Message

//Sub Notification Message

  typedef struct _tagRULERWNDNOTIFY_INFO{

     NMHDR   hdr;

     UINT    nSubMessage;

     DWORD   dwRulerStyle;

     int     iSepID;
     int     iNewPos;
     int     iOldPos;

     int     iParam1;
     int     iParam2;



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

Written By
Software Developer
Turkey Turkey
I'm working as a software developer in a local software company. My firm is a special solution company. This is work about different projects. Some friends thinking it is amazing. Smile | :) ))

Comments and Discussions

QuestionDoes this come in managed code ( Pin
Robert Gustafson28-Dec-17 16:21
Robert Gustafson28-Dec-17 16:21 
QuestionUse for academic project Pin
Léo Vailati20-Sep-11 11:19
Léo Vailati20-Sep-11 11:19 
AnswerRe: Use for academic project Pin
HAMZADAYI20-Jan-16 5:50
HAMZADAYI20-Jan-16 5:50 
Questionproblem using ruler Pin
Federicoman23-Jun-09 11:13
Federicoman23-Jun-09 11:13 
GeneralChange the position of the 0 Value Pin
JonathanMahut2-Aug-04 3:47
JonathanMahut2-Aug-04 3:47 
GeneralRe: Change the position of the 0 Value Pin
Martial Spirit19-Mar-07 7:53
Martial Spirit19-Mar-07 7:53 
GeneralGood! Pin
Rickard Andersson2029-Mar-03 7:15
Rickard Andersson2029-Mar-03 7:15 
GeneralSo... Pin
kezhu2-Oct-02 13:35
kezhu2-Oct-02 13:35 
GeneralRe: So... Pin
alex.barylski2-Oct-02 21:13
alex.barylski2-Oct-02 21:13 
GeneralFix the formatting Pin
alex.barylski1-Oct-02 23:28
alex.barylski1-Oct-02 23:28 
GeneralRe: Fix the formatting Pin
HAMZADAYI2-Oct-02 0:54
HAMZADAYI2-Oct-02 0:54 
GeneralFunny company description Pin
Stlan1-Oct-02 1:06
Stlan1-Oct-02 1:06 
GeneralRe: Funny company description Pin
alex.barylski1-Oct-02 23:23
alex.barylski1-Oct-02 23:23 

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.