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



Click here for a larger image.

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


Comments

  • transparent Scalebar control

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

    Originally posted by: kim

    Hi,

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

    Regards,
    kim

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

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds