using DevExpress.XtraBars; using ESRI.ArcGIS.ADF.BaseClasses; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using KGIS.Framework.Menus; using KGIS.Framework.Utils; using System; using System.Runtime.InteropServices; namespace Kingo.Plugin.MapView.Commands { public class ControlMapScaleListCommandClass : BaseCommand, ICombox { private IHookHelper m_hookHelper; private EngineEditorClass editor = new EngineEditorClass(); private BarEditItem _baritem = null; private ControlsEditingEditTool editTool = null; private double scaleValue = 0; public object BarEditItem { get { return _baritem; } } public bool IsShowCaption { get { return true; } } public override string Caption { get { return "比例尺"; } } public override bool Enabled { get { return true; } } public override void OnClick() { } DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboxEdit = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox(); public override void OnCreate(object hook) { try { if (m_hookHelper == null) { m_hookHelper = new HookHelper(); m_hookHelper.Hook = hook; } if (m_hookHelper.Hook is IMapControl2) { ((ESRI.ArcGIS.Controls.IMapControlEvents2_Event)m_hookHelper.Hook).OnExtentUpdated += ControlsMapScaleZoomToolCommand_OnExtentUpdated; } CreateCombox(); } catch (Exception ex) { LogAPI.Debug("初始化 比例尺 命令时异常,异常信息如下:"); LogAPI.Debug(ex); LogAPI.Debug("初始化 比例尺 命令时异常信息结束"); } } private void ControlsMapScaleZoomToolCommand_OnExtentUpdated(object displayTransformation, bool sizeChanged, object newEnvelope) { if (this.m_hookHelper.ActiveView.FocusMap.SpatialReference == null) return; try { #region 防止获取Map的MapScale异常 int hWnd = GetDesktopWindow().ToInt32(); IActiveView pAv = m_hookHelper.ActiveView; pAv.Activate(hWnd); #endregion SetComboxText(this.m_hookHelper.ActiveView.FocusMap.MapScale); } catch (Exception ex) { LogAPI.Debug(" 执行 ControlsMapScaleZoomToolCommand_OnExtentUpdated 时候失败,异常原因: " + ex + " ; "); } } private void SetComboxText(double scale) { this._baritem.EditValue = ConvertNumberToMapScale(scale); } private void CreateCombox() { comboxEdit.SelectedIndexChanged += ComboxEdit_SelectedIndexChanged; comboxEdit.Leave += comboxEdit_Leave; comboxEdit.Items.Add("1:250"); comboxEdit.Items.Add("1:500"); comboxEdit.Items.Add("1:1000"); comboxEdit.Items.Add("1:5000"); comboxEdit.Items.Add("1:10000"); comboxEdit.Items.Add("1:24000"); comboxEdit.Items.Add("1:100000"); comboxEdit.Items.Add("1:250000"); comboxEdit.Items.Add("1:500000"); comboxEdit.Items.Add("1:750000"); comboxEdit.Items.Add("1:1500000"); } private void comboxEdit_Leave(object sender, EventArgs e) { if ((sender as DevExpress.XtraEditors.ComboBoxEdit) != null && (sender as DevExpress.XtraEditors.ComboBoxEdit).EditValue != null) { SetMapScale((sender as DevExpress.XtraEditors.ComboBoxEdit).EditValue.ToString()); } } private void SetMapScale(string scale) { if (string.IsNullOrWhiteSpace(scale)) { return; } double scaled = 0; if (ConvertMapScaleToNumber(scale, out scaled)) { if (scaleValue == scaled) { return; } scaleValue = scaled; this.m_hookHelper.ActiveView.FocusMap.MapScale = scaled; this.m_hookHelper.ActiveView.Refresh(); } } private bool ConvertMapScaleToNumber(string strInput, out double dblResult) { //1.移除逗号 string strText = strInput.Replace(",", ""); //2.移除1: if (strText.StartsWith("1:")) { strText = strText.Remove(0, 2); } double dblTemp; //3.判断是否数字 if (double.TryParse(strText, out dblTemp)) { if (dblTemp < 0) { dblResult = double.NaN; return false; } //在0和1之间不再四舍五入,但要保留3位精度 if ((dblTemp < 10) && (dblTemp > 0)) { dblTemp = 1000 * dblTemp; dblTemp = (double)Math.Round(dblTemp); dblResult = dblTemp / 1000; } else { dblResult = (double)Math.Round(dblTemp); } return true; } else { dblResult = double.NaN; return false; } } private string ConvertNumberToMapScale(double num) { if (num < 0) { return "1:0"; } double dblResult; //如果在0和10之间不再四舍五入,但要保留3位精度 if ((num < 10) && (num > 0)) { double dblTemp = 1000 * num; dblTemp = (double)Math.Round(dblTemp); dblResult = dblTemp / 1000; } else { dblResult = (double)Math.Round(num); } //小数点前每隔三位插入"," string strResult = dblResult.ToString(); int pointIndex = strResult.IndexOf("."); if (pointIndex < 0) { pointIndex = strResult.Length; } if (pointIndex > 3) { for (int i = pointIndex; i > 3; i = i - 3) { strResult = strResult.Insert(i - 3, ","); } } return "1:" + strResult; } private void ComboxEdit_SelectedIndexChanged(object sender, EventArgs e) { try { if (this._baritem.EditValue != null) { SetMapScale(this._baritem.EditValue.ToString()); } } catch (Exception ex) { //LogAPI.Debug("图层切换时工具切换失败:" + ex.Message); LogAPI.Debug("比例尺 切换失败,异常信息如下:"); LogAPI.Debug(ex); LogAPI.Debug("比例尺 切换失败异常信息结束"); } } public void SetBarEditItem(object barItem) { if (barItem is BarEditItem) { _baritem = barItem as BarEditItem; comboxEdit.AutoHeight = true; comboxEdit.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor; comboxEdit.DropDownRows = 20; comboxEdit.SelectedIndexChanged += ComboxEdit_SelectedIndexChanged; _baritem.EditWidth = 120; _baritem.Edit = comboxEdit; } } [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")] protected static extern IntPtr GetDesktopWindow(); } }