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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds