Creating a ScaleBar Control

Environment: This control was developed with VC6 and ATL.

Description

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

Properties of the Control

























NameDescription
BackColor  
BarColor1 Color for the first tic.
BarColor2 Color for the second tic.
BarOutline  
BarOutlineColor  
BarStyle ws_bsStandard

ws_bsMeridian

ws_bsMeridian1
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.
BarWidth  
BorderColor  
BorderStyle  
BorderVisible  
BorderWidth  
Font  
ForeColor  
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









NameDescription
SetScale
(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).
SetScaleD
(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.
SetCustomUnit
(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).
GetBarUnitInfo
(double *factor, BSTR *name, BSTR *short_name)
Get information about the bar unit.
GetMapUnitInfo
(double *factor, BSTR *name, BSTR *short_name)
Get information about the map unit.

Usage

  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.

Downloads


Download demo project – 5 Kb


Download control – 84 Kb


Download source – 94 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read