User Tools

Site Tools


source_code_serviceseditionsample_wordwrappedtextstyle_cs_091020.zip

Source Code ServicesEditionSample WordWrappedTextStyle CS 091020.zip

Program.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace WordWrappedTextStyle  
 {  
     static class Program  
     {  
         /// <summary>  
         /// The main entry point for the application.  
         /// </summary>  
         [STAThread]  
         static void Main()  
         {  
             Application.EnableVisualStyles();  
             Application.SetCompatibleTextRenderingDefault(false);  
             Application.Run(new TestForm());  
         }  
     }  
 }  
 

TestForm.cs

 using System;  
 using System.Drawing;  
 using System.Windows.Forms;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace WordWrappedTextStyle  
 {  
     public partial class TestForm : Form  
     {  
         private MapEngine mapEngine = new MapEngine();  
         private Bitmap bitmap = null;  
         ShapeFileFeatureLayer worldLayer = null;  
 
         public TestForm()  
         {  
             InitializeComponent();  
         }  
 
         private void TestForm_Load(object sender, EventArgs e)  
         {  
             // Sets the full extent and the background color  
             mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(42.3575,34.4278,67.2602,15.2560), Map.Width, Map.Height);  
             mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);  
 
             // Adds the static layers to the MapEngine  
             //Adds the background countries layer.  
             worldLayer = new ShapeFileFeatureLayer(@"..\..\Data\countries02.shp", ShapeFileReadWriteMode.ReadOnly);  
             worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;  
             worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             mapEngine.StaticLayers.Add("WorldLayer", worldLayer);  
 
 
             // Adds the countries layer for labeling.  
             ShapeFileFeatureLayer Layer2 = new ShapeFileFeatureLayer(@"..\..\Data\countries02.shp", ShapeFileReadWriteMode.ReadOnly);  
             Layer2.ZoomLevelSet.ZoomLevel01.CustomStyles.Clear();  
             //Adds the WordWrappedTextStyle to the CustomStyles collection.  
             WordWrappedTextStyle wordWrappedTextStyle = new WordWrappedTextStyle();  
             wordWrappedTextStyle.Font = new GeoFont("Arial", 10);  
             wordWrappedTextStyle.TextSolidBrush = new GeoSolidBrush(GeoColor.SimpleColors.Black);  
             wordWrappedTextStyle.TextColumnName = "LONG_NAME";  
             Layer2.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(wordWrappedTextStyle);  
 
             Layer2.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             mapEngine.StaticLayers.Add("Layer2", Layer2);  
 
             DrawImage();  
         }  
 
 
         private void DrawImage()  
         {  
             if (bitmap != null) { bitmap.Dispose(); }  
             bitmap = new Bitmap(Map.Width, Map.Height);  
             mapEngine.OpenAllLayers();  
             mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);  
             mapEngine.DrawDynamicLayers(bitmap, GeographyUnit.DecimalDegree);  
             mapEngine.CloseAllLayers();  
 
             Map.Image = bitmap;  
         }  
 
         private void ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)  
         {  
             switch (e.Button.Tag.ToString())  
             {  
                 case "Zoom In":  
                     mapEngine.CurrentExtent.ScaleDown(50);  
                     break;  
                 case "Zoom Out":  
                     mapEngine.CurrentExtent.ScaleUp(50);  
                     break;  
                 case "Full Extent":  
                     mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);  
                     break;  
                 case "Pan Left":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20);  
                     break;  
                 case "Pan Right":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);  
                     break;  
                 case "Pan Up":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);  
                     break;  
                 case "Pan Down":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20);  
                     break;  
                 default:  
                     break;  
             }  
             DrawImage();  
         }  
 
         private void btnClose_Click(object sender, EventArgs e)  
         {  
             this.Close();  
         }  
     }  
 }  
 

WordWrappedTextStyle.cs

 using System.Collections.Generic;  
 using System.Collections.ObjectModel;  
 using System;  
 using System.Collections;  
 
 using ThinkGeo.MapSuite.Core;  
 
 namespace WordWrappedTextStyle  
 {  
     class WordWrappedTextStyle : TextStyle  
     {  
         protected override void DrawCore(IEnumerable<Feature> features, GeoCanvas canvas, Collection<SimpleCandidate> labelsInThisLayer, Collection<SimpleCandidate> labelsInAllLayers)  
         {  
             // Loop through all of the features being passed in to draw.  
             //Notice that for the drawing, it calls base.DrawCore from the base class meaning that all the properties such as Overlapping, Duplicate rules etc  
             //are taken into account.  
             foreach (Feature feature in features)  
             {  
                 // This gets the bounding box for the feature.  
                 RectangleShape boundingBox = feature.GetBoundingBox();  
                 //Gets the bounding box of the feature in screen coordinates.  
                 double boundingBoxScreenWidth = ExtentHelper.GetScreenDistanceBetweenTwoWorldPoints(canvas.CurrentWorldExtent,  
                                                new PointShape(boundingBox.UpperLeftPoint.X, (boundingBox.UpperLeftPoint.Y + boundingBox.LowerLeftPoint.Y) / 2),  
                                                new PointShape(boundingBox.LowerRightPoint.X, (boundingBox.UpperRightPoint.Y + boundingBox.LowerRightPoint.Y) / 2),  
                                                canvas.Width, canvas.Height);  
 
                 string text = feature.ColumnValues[TextColumnName];  
                 DrawingRectangleF drawingRectangleF= canvas.MeasureText(text, this.Font);  
 
                 //Compares the bounding box in screen coordinates with the size of the text.  
                 if (boundingBoxScreenWidth > drawingRectangleF.Width)  
                 {  
                     //If fully within, it draws in one line.  
                     base.DrawCore(new Feature[1] { feature }, canvas, labelsInThisLayer, labelsInAllLayers);  
                 }  
                 else  
                 {  
                     //If not, applies the word wrapping.  
                     string[] Words = Wrap(text,GetMaxStringLength(canvas,drawingRectangleF.Width));  
                     string newLineText = "";  
                     for (int i = 0; i < Words.Length; i++)  
                     {  
                         newLineText = newLineText + Words[i] + Environment.NewLine;  
                     }  
                     feature.ColumnValues[TextColumnName] = newLineText;  
                     base.DrawCore(new Feature[1] { feature }, canvas, labelsInThisLayer, labelsInAllLayers);  
                 }  
             }  
         }  
 
         //Function to get the maximum length of the string without having a new line.  
         private int GetMaxStringLength(GeoCanvas canvas, float currentWidth)  
         {  
             //For now, the value is 10. You can write your own logic to calculate that value based on  
             // the size of the text and the bounding box to hold it.  
             int result = 10;  
             return result;  
         }  
 
 
         //Function taken from http://www.velocityreviews.com/forums/t20370-word-wrap-line-break-code-and-algorithm-for-c.html  
         private string[] Wrap(string text, int maxLength)  
         {  
             text = text.Replace("\n", " ");  
             text = text.Replace("\r", " ");  
             text = text.Replace(".", ". ");  
             text = text.Replace(">", "> ");  
             text = text.Replace("\t", " ");  
             text = text.Replace(",", ", ");  
             text = text.Replace(";", "; ");  
             text = text.Replace("
 ", " ");  
             text = text.Replace(" ", " ");  
 
             string[] Words = text.Split(' ');  
             int currentLineLength = 0;  
             ArrayList Lines = new ArrayList(text.Length / maxLength);  
             string currentLine = "";  
             bool InTag = false;  
 
             foreach (string currentWord in Words)  
             {  
                 //ignore html  
                 if (currentWord.Length > 0)  
                 {  
 
                     if (currentWord.Substring(0, 1) == "<")  
                         InTag = true;  
 
                     if (InTag)  
                     {  
                         //handle filenames inside html tags  
                         if (currentLine.EndsWith("."))  
                         {  
                             currentLine += currentWord;  
                         }  
                         else  
                             currentLine += " " + currentWord;  
 
                         if (currentWord.IndexOf(">") > -1)  
                             InTag = false;  
                     }  
                     else  
                     {  
                         if (currentLineLength + currentWord.Length + 1 < maxLength)  
                         {  
                             currentLine += " " + currentWord;  
                             currentLineLength += (currentWord.Length + 1);  
                         }  
                         else  
                         {  
                             Lines.Add(currentLine);  
                             currentLine = currentWord;  
                             currentLineLength = currentWord.Length;  
                         }  
                     }  
                 }  
 
             }  
             if (currentLine != "")  
                 Lines.Add(currentLine);  
 
             string[] textLinesStr = new string[Lines.Count];  
             Lines.CopyTo(textLinesStr, 0);  
             return textLinesStr;  
         }  
 
         protected override System.Collections.ObjectModel.Collection<string> GetRequiredColumnNamesCore()  
         {  
             // Here we grab the column from the textStyle and then add  
             // the required columns to make sure we pull back the column  
             //  that we need for labeling.  
             Collection<string> columns = new Collection<string>();  
             if (!columns.Contains(TextColumnName))  
             {  
                 columns.Add(TextColumnName);  
             }  
             return columns;  
         }  
     }  
 }  
 
source_code_serviceseditionsample_wordwrappedtextstyle_cs_091020.zip.txt · Last modified: 2015/09/08 08:04 by admin