Creating a ScaleBar Control

Environment: This control was developed with VC6 and ATL.


Scale bar is a basic tool for GIS and other systems. ESRI has published a scale bar control with VB source code, but it doesn't meet our need. So, I decided to develop a new control (named WScalebarServer) with some improvements and more features. It supports several units, such as meter, foot, kilometer, mile, yard, and of course, degree. And you can define your own unit, too. It also supports regional number settings. And there are several styles of appearances you can choose from.

The programming is quite routine. The only tricky part is to decide the length of each tic of the scale bar. We have to let the tics represent map length such as 250, 500, 750, 1000, and so forth—these are nice numbers. Functions CalcBarScale() and GetRoundIncrement() in file util.cpp will do this job.

Appearance of the Control

Click here for a larger image.

Properties of the Control

BarColor1 Color for the first tic.
BarColor2 Color for the second tic.
BarStyle ws_bsStandard


BarUnit Possible values include ws_suCustom(0), ws_suMeter(9001), ws_suFootUS(9003), ws_suYardSears(9012), ws_suYardIndian(9013), ws_suMileUS(9035), and ws_suKilometer(9036).
This is the unit that's shown with the scalebar. If you are going to use Custom Unit, please call the SetCustomUnit method.
FormatNumber Set it to true if you are going to use thousand separators and regional decimal point character.
MapUnit Possible values include ws_muCustom(0), ws_muMeter(9001), ws_muFootUS(9003), ws_muYardSears(9012), ws_muYardIndian(9013), ws_muMileUS(9035), ws_muKilometer(9036), and ws_muDegree(9102).
This is the unit used on the map. We use this unit to calculate the map scale.
MarginLeft Left margin of the scale bar.
MarginRight Right margin of the scale bar.
NumTics How many tics for the scale bar.
Scale If you know the real scale of the map, you can set the scale directly here; otherwise, use methods SetScaleD( )/SetScale( ).
ScaleText ws_stNoText means will show on test.
ws_stUnitsOnly will show the unit name with with the bar.
Ws_stFraction will show the scale (1:xxxx).

Methods of the Control

(double MapWidth, long WidthInPixel)
Calculate the scale for the map. MapWidth is the width of the map extent (in map units). WidthInPixel is the the screen width of the map (in pixels).
(double lon1, double lon2, double lat, long WidthInPixel)
Similar to method SetScale( ), but the map is in degrees (latitude-longitude). Please use the vertical center line to measure the width of the map. lon1 and lon2 are the longitude values of the left border and right borders. lat is the latitude of the line.
(double factor, BSTR name, BSTR short_name)
Set a custom unit. It will apply to both map units and bar units. Factor is meter-based; for example, 0.01 for centimeters (because 1cm=0.01m).
(double *factor, BSTR *name, BSTR *short_name)
Get information about the bar unit.
(double *factor, BSTR *name, BSTR *short_name)
Get information about the map unit.


  1. Set the MapUnit and BarUnit. If the unit you use is not listed, you can use SetCustomUnit() method to set your custom unit.
  2. Call method SetScale() or SetScaleD() (for latitude/longitude map) to calculate the scale, or you can set the Scale property directly if you know the exact scale.


Download demo project - 5 Kb
Download control - 84 Kb
Download source - 94 Kb


  • transparent Scalebar control

    Posted by Legacy on 11/05/2003 12:00am

    Originally posted by: kim


    I would like to ask can i set Scalebar control to transparent. thanks


Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds