COXRulerBar & COXRulerOrganizer
Copyright © The Code Project 1997 - 2005, All Rights Reserved

We introduce a couple of classes that allows you to display horizontal and/or vertical ruler bars attached to any window. By definition the ruler bar is rectangular area with scale. Ruler bar has base unit (by default we use 1 inch as base unit) and every base unit is designated by corresponding number on the scale. If distance between two units is big enough the ticks will be drawn on the scale.

Ruler bars are drawn in the non-client area of the attached window, much the same like scroll bars. 

Let's go to the details about the classes that implement the support for ruler bars.

First of all, we've got the class that implements all the functionality that has to do with the appearance of the ruler bar. This class is called COXRulerBar. Refer to the description of the class and function reference for details on this class. Briefly, COXRulerBar provides the functionality of drawing the ruler bar. using this class you can highlight any area on the ruler bar, define any base unit to be used on ruler bar scale, zoom and scroll the contents of the ruler bar.

COXRulerBar itself is useful control but in most cases you would be interested in using it in combination with another window. The analogy with scroll bar comes here in handy.You can use the CScrollBar control independently but in most cases you want to associate it with some window that supports scrolling functionality and use it without caring to much about creating the scroll bars, positioning them and constantly updating them. 

The same story with ruler bars. Wouldn't it be nice to have automatic support for horizontal and/or vertical ruler bars for any window?

The COXRulerOrganizer class provides that kind of functionality.

In order to provide ruler bars for any window you have to do the following:

1)    declare an object of COXRulerOrganizer class so the lifetime of such object would be at least the same as the lifetime of the window for which ruler bars support must be provided.
    class CMyView : public CView
    {
    . . . . . . . . . . . . . . . 
    protected:
        COXRulerOrganizer m_ruler;
    . . . . . . . . . . . . . . . 
    }

2)    Attach the window to COXRulerOrganizer object using Attach() function. In this function you can specify what ruler bars (horizontal and/or vertical) should be shown from the very beginning.
    int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
        if (CMyView::OnCreate(lpCreateStruct) == -1)
            return -1;
        . . . . . . . . . . . . . . . 
        // atach view to the COXRulerOrganizer object and display 
        // bot horizontal and vertical ruler bars
        VERIFY(m_ruler.Attach(this,TRUE,TRUE));
        . . . . . . . . . . . . . . . 
        return 0;
    }

That's it. 
After attaching the window all its mouse movements will be traced and displayed on the ruler bars and if this window supports scrolling the ruler bars will be scrolled correspondingly.

After a window is attached to the COXRulerOrganizer object you can use a lot of COXRulerOrganizer functions that would allow you to utilize this class functionality to the full extent.

1)    Show/hide ruler bars. In order to do that call the following functions:
SetShowHorzRulerBar();
SetShowVertRulerBar();
2)   Change height/width of the bars. Call the following functions:
SetHorzRulerBarHeight();
SetVertRulerBarWidth();
3)    Retrieve the position of the ruler bars:
GetHorzRulerBarRect();
GetVertRulerBarRect();
4)   Direct access to the underlying COXRulerBar objects:
GetHorzRulerBar();
GetVertRulerBar();
5)   Set of wrapper functions that call corresponding COXRulerBar functions:
// hilite area on the ruler bar
HiliteRuler();

// change base unit
CalibrateRuler();

// scroll the contents of the ruler bar
ScrollRuler();

// zoom in or zoom out the contents of the ruler
ZoomRuler();
 
You can use these function at any time in your code as long as some window is attached to the corresponding COXRulerOrganizer object. For example: 
// hide horizontal ruler 
m_ruler.SetShowHorzRulerBar(FALSE);

//use centimeters as base units for the horizontal and vertical ruler bars
m_ruler.CalibrateRuler(254,TRUE);
m_ruler.CalibrateRuler(254,FALSE);

Dependency:
"OXRulerOrganizer.h"
 
Source code files:
"OXRulerOrganizer.cpp"
"OXHookWnd.cpp"             implementation of the COXRulerOrganizer parent class - COXHookWnd