📄 form1.cs
字号:
/*
Copyright 1995-2005 ESRI
All rights reserved under the copyright laws of the United States.
You may freely redistribute and use this sample code, with or without modification.
Disclaimer: THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESRI OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY
WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
For additional information contact: Environmental Systems Research Institute, Inc.
Attn: Contracts Dept.
380 New York Street
Redlands, California, U.S.A. 92373
Email: contracts@esri.com
*/
using System;
using System.Windows.Forms;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.MapControl;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
namespace ArrowKeyPan
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.TextBox Text1;
public System.Windows.Forms.Button Command1;
public System.Windows.Forms.Label Label1;
public System.Windows.Forms.Label Shape1;
private ESRI.ArcGIS.MapControl.AxMapControl axMapControl1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.Text1 = new System.Windows.Forms.TextBox();
this.Command1 = new System.Windows.Forms.Button();
this.Label1 = new System.Windows.Forms.Label();
this.Shape1 = new System.Windows.Forms.Label();
this.axMapControl1 = new ESRI.ArcGIS.MapControl.AxMapControl();
((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).BeginInit();
this.SuspendLayout();
//
// Text1
//
this.Text1.AcceptsReturn = true;
this.Text1.AutoSize = false;
this.Text1.BackColor = System.Drawing.SystemColors.Window;
this.Text1.Cursor = System.Windows.Forms.Cursors.IBeam;
this.Text1.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Text1.ForeColor = System.Drawing.SystemColors.WindowText;
this.Text1.Location = new System.Drawing.Point(160, 8);
this.Text1.MaxLength = 0;
this.Text1.Name = "Text1";
this.Text1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Text1.Size = new System.Drawing.Size(329, 25);
this.Text1.TabIndex = 5;
this.Text1.TabStop = false;
this.Text1.Text = "..\\..\\..\\..\\..\\..\\Data\\ServerData\\Yellowstone\\Yellowstone.mxd";
//
// Command1
//
this.Command1.BackColor = System.Drawing.SystemColors.Control;
this.Command1.Cursor = System.Windows.Forms.Cursors.Default;
this.Command1.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Command1.ForeColor = System.Drawing.SystemColors.ControlText;
this.Command1.Location = new System.Drawing.Point(8, 8);
this.Command1.Name = "Command1";
this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Command1.Size = new System.Drawing.Size(145, 25);
this.Command1.TabIndex = 4;
this.Command1.TabStop = false;
this.Command1.Text = "Load Map Document";
this.Command1.Click += new System.EventHandler(this.Command1_Click);
//
// Label1
//
this.Label1.BackColor = System.Drawing.SystemColors.Control;
this.Label1.Cursor = System.Windows.Forms.Cursors.Default;
this.Label1.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Label1.ForeColor = System.Drawing.SystemColors.ControlText;
this.Label1.Location = new System.Drawing.Point(40, 48);
this.Label1.Name = "Label1";
this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Label1.Size = new System.Drawing.Size(449, 17);
this.Label1.TabIndex = 6;
this.Label1.Text = "Load a map document and then when map has focus use the arrow keys to pan around " +
"map";
//
// Shape1
//
this.Shape1.BackColor = System.Drawing.Color.Transparent;
this.Shape1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Shape1.Location = new System.Drawing.Point(8, 72);
this.Shape1.Name = "Shape1";
this.Shape1.Size = new System.Drawing.Size(488, 312);
this.Shape1.TabIndex = 7;
//
// axMapControl1
//
this.axMapControl1.Location = new System.Drawing.Point(16, 80);
this.axMapControl1.Name = "axMapControl1";
this.axMapControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMapControl1.OcxState")));
this.axMapControl1.Size = new System.Drawing.Size(472, 296);
this.axMapControl1.TabIndex = 8;
this.axMapControl1.Enter += new System.EventHandler(this.axMapControl1_Enter);
this.axMapControl1.Leave += new System.EventHandler(this.axMapControl1_Leave);
this.axMapControl1.OnKeyDown += new ESRI.ArcGIS.MapControl.IMapControlEvents2_OnKeyDownEventHandler(this.axMapControl1_OnKeyDown);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 390);
this.Controls.Add(this.axMapControl1);
this.Controls.Add(this.Shape1);
this.Controls.Add(this.Text1);
this.Controls.Add(this.Command1);
this.Controls.Add(this.Label1);
this.Name = "Form1";
this.Text = "ArrowKeyPan";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
//Allow the MapControl to intercept arrowkeys when it has focus
axMapControl1.KeyIntercept = 1;
//Dotted rectangle is used to illustrates when map has focus
Shape1.Visible = false;
}
private void Command1_Click(object sender, System.EventArgs e)
{
axMapControl1.LoadMxFile(Text1.Text, Type.Missing, "");
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//Release COM objects
ESRI.ArcGIS.Utility.COMSupport.AOUninitialize.Shutdown();
}
private void axMapControl1_OnKeyDown(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnKeyDownEvent e)
{
//Set current tool
switch (e.keyCode)
{
case (int) System.Windows.Forms.Keys.Up:
PanMap(0, 0.5);
break;
case (int) System.Windows.Forms.Keys.Down:
PanMap(0, -0.5);
break;
case (int) System.Windows.Forms.Keys.Left:
PanMap(-0.5, 0);
break;
case (int) System.Windows.Forms.Keys.Right:
PanMap(0.5, 0);
break;
}
}
private void axMapControl1_Enter(object sender, System.EventArgs e)
{
Shape1.Visible = true;
}
private void axMapControl1_Leave(object sender, System.EventArgs e)
{
Shape1.Visible = false;
}
private void PanMap(double ratioX, double ratioY)
{
//Pans map by amount specified given in a fraction of the extent e.g. rationX=0.5, pan right by half a screen
IEnvelope envelope = axMapControl1.Extent;
double h = envelope.Width;
double w = envelope.Height;
envelope.Offset(h * ratioX, w * ratioY);
axMapControl1.Extent = envelope;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -