发表用户:讨论贴
收集整理:James.Liu
相关讨论:http://www.mygis.com.cn/forum/dispbbs.asp?boardID=4&ID=2795
信息原始来源:James MapInfo技术论坛

文章标题:在两个Symbol之间画线,怎样才能实现移动其中的一个Symbol,线也跟着移动?

zhaoxl2004

在两个Symbol之间画线,怎样才能实现移动其中的一个Symbol,线也跟着移动?谢谢!

MILearner

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
namespace scrollMap
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private AxMapXLib.AxMap axMap1;
  private System.Windows.Forms.VScrollBar vScrollBar1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;
  private System.Windows.Forms.Button button1;
        private double oldZoom;
  private bool drown=false;
  private MapXLib.Feature dashLine=null;
  
  private MapXLib.PointClass pnt=null;
  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null) 
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.axMap1 = new AxMapXLib.AxMap();
   this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
   this.button1 = new System.Windows.Forms.Button();
   ((System.ComponentModel.ISupportInitialize)(this.axMap1)).BeginInit();
   this.SuspendLayout();
   // 
   // axMap1
   // 
   this.axMap1.Enabled = true;
   this.axMap1.Location = new System.Drawing.Point(16, 16);
   this.axMap1.Name = "axMap1";
   this.axMap1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMap1.OcxState")));
   this.axMap1.Size = new System.Drawing.Size(400, 300);
   this.axMap1.TabIndex = 0;
   this.axMap1.PolyToolUsed += new AxMapXLib.CMapXEvents_PolyToolUsedEventHandler(this.axMap1_PolyToolUsed);
   this.axMap1.ToolUsed += new AxMapXLib.CMapXEvents_ToolUsedEventHandler(this.axMap1_ToolUsed);
   this.axMap1.MouseDownEvent += new AxMapXLib.CMapXEvents_MouseDownEventHandler(this.axMap1_MouseDownEvent);
   this.axMap1.AddFeatureToolUsed += new AxMapXLib.CMapXEvents_AddFeatureToolUsedEventHandler(this.axMap1_AddFeatureToolUsed);
   this.axMap1.MouseMoveEvent += new AxMapXLib.CMapXEvents_MouseMoveEventHandler(this.axMap1_MouseMoveEvent);
   // 
   // vScrollBar1
   // 
   this.vScrollBar1.Location = new System.Drawing.Point(416, 16);
   this.vScrollBar1.Name = "vScrollBar1";
   this.vScrollBar1.Size = new System.Drawing.Size(16, 304);
   this.vScrollBar1.TabIndex = 1;
   this.vScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.vScrollBar1_Scroll);
   // 
   // button1
   // 
   this.button1.Location = new System.Drawing.Point(216, 320);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(112, 24);
   this.button1.TabIndex = 2;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   // 
   // Form1
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(440, 350);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.vScrollBar1);
   this.Controls.Add(this.axMap1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   ((System.ComponentModel.ISupportInitialize)(this.axMap1)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main() 
  {
   Application.Run(new Form1());
  }

  private void vScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
  {
      MapXLib.Map map=axMap1.GetOcx() as MapXLib.Map;
   if(map==null)
    throw new System.ArgumentNullException("map");
   if(e.Newvalue>0)
    map.ZoomTo(oldZoom/e.Newvalue,map.CenterX,map.CenterY);
      
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
      axMap1.GeoSet=""; 
   MapXLib.Layer lyr=axMap1.Layers.CreateLayer("temp","",1,Missing.value,Missing.value);
   lyr.Editable=true;
   axMap1.Layers.InsertionLayer=lyr;
   oldZoom=axMap1.Zoom;
   axMap1.CurrentTool=MapXLib.ToolConstants.miAddLineTool;
   axMap1.SnapToNodeSupport=true;
   axMap1.CreateCustomTool(101,MapXLib.ToolTypeConstants.miToolTypePoly,MapXLib.CursorConstants.miCrossCursor,Missing.value,Missing.value,Missing.value);
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   MapXLib.Feature line=null;
   if(axMap1.CurrentTool!=((MapXLib.ToolConstants)101))
   {
    if(axMap1.Layers[1].AllFeatures.Count==1)
     line=axMap1.Layers[1].AllFeatures._Item(1);
    MapXLib.Points pnts=line.Parts._Item(1);
    /*for(int i=1;i<=2;i++)
    {
     MapXLib.Feature symbol=axMap1.FeatureFactory.CreateSymbol(pnts._Item(i),axMap1.DefaultStyle);
     axMap1.Layers[1].AddFeature(symbol,Missing.value);
    }*/
    axMap1.CurrentTool=((MapXLib.ToolConstants)101);
   }
   else
    axMap1.CurrentTool=MapXLib.ToolConstants.miAddLineTool;
  }

  private void axMap1_ToolUsed(object sender, AxMapXLib.CMapXEvents_ToolUsedEvent e)
  {
   
   switch(e.toolNum)
   {
    case ((short)MapXLib.ToolConstants.miAddLineTool):
     pnt=new MapXLib.PointClass();
     pnt.Set(e.x1,e.y1);
     break;
    case 101:
                    Console.Write("used\n");
     break;
   }
  }

  private void axMap1_PolyToolUsed(object sender, AxMapXLib.CMapXEvents_PolyToolUsedEvent e)
  {
   MapXLib.StyleClass lineStyle=null;
   try
   {
    switch(e.flags)
    {
     case ((int)MapXLib.PolyToolFlagConstants.miPolyToolEndEscaped):
      lineStyle=new MapXLib.StyleClass();
      lineStyle.LineStyle=MapXLib.PenStyleConstants.miPenSolid;
      MapXLib.Points pnts=dashLine.Parts._Item(1);
      int i=axMap1.Layers[1].AllFeatures.Count;
      axMap1.Layers[1].DeleteFeature(axMap1.Layers[1].AllFeatures._Item(1));
      axMap1.Layers[1].AddFeature(axMap1.FeatureFactory.CreateLine(pnts,axMap1.DefaultStyle),Missing.value);
      this.drown=false;
      axMap1.Refresh();
      break;
    }
   }
   catch(Exception exc)
   {
    MessageBox.Show(exc.Message);
   }
  }

  private void axMap1_AddFeatureToolUsed(object sender, AxMapXLib.CMapXEvents_AddFeatureToolUsedEvent e)
  {
      
   switch(e.flags)
   {
    case ((int)MapXLib.PolyToolFlagConstants.miPolyToolBegin):
     if(axMap1.Layers[1].AllFeatures.Count==1)
      axMap1.Layers[1].DeleteFeature(axMap1.Layers[1].AllFeatures._Item(1));
     
     break;
   }
  }

  private void axMap1_MouseMoveEvent(object sender, AxMapXLib.CMapXEvents_MouseMoveEvent e)
  {
   double mapx=0;
   double mapy=0;
   if(this.dashLine==null)
    return;
   if(axMap1.CurrentTool==((MapXLib.ToolConstants)101)&&(this.drown))
   {
    axMap1.ConvertCoord(ref e.x,ref e.y,ref mapx,ref mapy,MapXLib.ConversionConstants.miScreenToMap);
    dashLine=axMap1.Layers[1].AllFeatures._Item(1);
    dashLine.Parts._Item(1)._Item(2).Set(mapx,mapy);
                dashLine.Update(dashLine,Missing.value);
    
   }

  }
  
  private void axMap1_MouseDownEvent(object sender, AxMapXLib.CMapXEvents_MouseDownEvent e)
  {
   double mapx=0;
   double mapy=0;
   MapXLib.StyleClass lineStyle=null;
   if(pnt==null)
    return;
   //if((e.button==1))
   {
    axMap1.ConvertCoord(ref e.x,ref e.y,ref mapx,ref mapy,MapXLib.ConversionConstants.miScreenToMap);
    MapXLib.PointsClass pnts=new MapXLib.PointsClass();
    pnts.AddXY(this.pnt.X,this.pnt.Y,1);
    pnts.AddXY(mapx,mapy,2);
    lineStyle=new MapXLib.StyleClass();
    lineStyle.LineStyle=(MapXLib.PenStyleConstants)3;
    dashLine=axMap1.FeatureFactory.CreateLine(pnts,lineStyle);
    if(axMap1.Layers[1].AllFeatures.Count==1)
     axMap1.Layers[1].DeleteFeature(axMap1.Layers[1].AllFeatures._Item(1));
    axMap1.Layers[1].AddFeature(dashLine,Missing.value);
    this.drown=true;
   }
  }
 }

MILearner

刚写完的,俺下午没事就写了,为了怕麻烦,注释掉了symbol部分:)
因为觉得用MAPX来写这个功能真的有点受累了,如果用普通的Graphics来写可能会很简单!
这个写的很粗糙,就当是抛砖引玉了:)

俺还发现一个问题,注意到俺用dashLine这么一个变量来保存图元,在俺最后要删除该线时,如果使用deletefeature(dashLine);就会出错,显示不能删除。但是如果俺用 

MILearner

对不起,没写完怎么就提交了:(

但是如果俺用axMap1.Layers[1].DeleteFeature(axMap1.Layers[1].AllFeatures._Item(1));
就没有问题。
似乎俺保存的dashLine只是一个副本?只能获得图元属性而不能用于操作图元!

最后还买一送一,副赠一个用垂直滚动条放大缩小的例子,很小的东西,不值得一提:)
欢迎大家拍板砖!