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

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds