📄 savemapdocument.vb
字号:
' Copyright 1995-2004 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
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.Carto
Public Class Form1
Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Public WithEvents txtMapDocument As System.Windows.Forms.TextBox
Public WithEvents cmdOpen As System.Windows.Forms.Button
Public WithEvents cmdSave As System.Windows.Forms.Button
Public WithEvents cmdSaveAs As System.Windows.Forms.Button
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents AxPageLayoutControl1 As ESRI.ArcGIS.PageLayoutControl.AxPageLayoutControl
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents AxToolbarControl1 As ESRI.ArcGIS.ToolbarControl.AxToolbarControl
Friend WithEvents AxTOCControl1 As ESRI.ArcGIS.TOCControl.AxTOCControl
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.txtMapDocument = New System.Windows.Forms.TextBox
Me.cmdOpen = New System.Windows.Forms.Button
Me.cmdSave = New System.Windows.Forms.Button
Me.cmdSaveAs = New System.Windows.Forms.Button
Me.AxPageLayoutControl1 = New ESRI.ArcGIS.PageLayoutControl.AxPageLayoutControl
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.AxToolbarControl1 = New ESRI.ArcGIS.ToolbarControl.AxToolbarControl
Me.AxTOCControl1 = New ESRI.ArcGIS.TOCControl.AxTOCControl
CType(Me.AxPageLayoutControl1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.AxToolbarControl1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.AxTOCControl1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'txtMapDocument
'
Me.txtMapDocument.AcceptsReturn = True
Me.txtMapDocument.AutoSize = False
Me.txtMapDocument.BackColor = System.Drawing.SystemColors.Window
Me.txtMapDocument.Cursor = System.Windows.Forms.Cursors.IBeam
Me.txtMapDocument.Enabled = False
Me.txtMapDocument.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtMapDocument.ForeColor = System.Drawing.SystemColors.WindowText
Me.txtMapDocument.Location = New System.Drawing.Point(8, 472)
Me.txtMapDocument.MaxLength = 0
Me.txtMapDocument.Name = "txtMapDocument"
Me.txtMapDocument.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.txtMapDocument.Size = New System.Drawing.Size(513, 19)
Me.txtMapDocument.TabIndex = 6
Me.txtMapDocument.Text = ""
'
'cmdOpen
'
Me.cmdOpen.BackColor = System.Drawing.SystemColors.Control
Me.cmdOpen.Cursor = System.Windows.Forms.Cursors.Default
Me.cmdOpen.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.cmdOpen.ForeColor = System.Drawing.SystemColors.ControlText
Me.cmdOpen.Location = New System.Drawing.Point(536, 48)
Me.cmdOpen.Name = "cmdOpen"
Me.cmdOpen.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.cmdOpen.Size = New System.Drawing.Size(121, 33)
Me.cmdOpen.TabIndex = 4
Me.cmdOpen.Text = "Open Document..."
'
'cmdSave
'
Me.cmdSave.BackColor = System.Drawing.SystemColors.Control
Me.cmdSave.Cursor = System.Windows.Forms.Cursors.Default
Me.cmdSave.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.cmdSave.ForeColor = System.Drawing.SystemColors.ControlText
Me.cmdSave.Location = New System.Drawing.Point(536, 128)
Me.cmdSave.Name = "cmdSave"
Me.cmdSave.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.cmdSave.Size = New System.Drawing.Size(121, 33)
Me.cmdSave.TabIndex = 3
Me.cmdSave.Text = "Save Document"
'
'cmdSaveAs
'
Me.cmdSaveAs.BackColor = System.Drawing.SystemColors.Control
Me.cmdSaveAs.Cursor = System.Windows.Forms.Cursors.Default
Me.cmdSaveAs.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.cmdSaveAs.ForeColor = System.Drawing.SystemColors.ControlText
Me.cmdSaveAs.Location = New System.Drawing.Point(536, 88)
Me.cmdSaveAs.Name = "cmdSaveAs"
Me.cmdSaveAs.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.cmdSaveAs.Size = New System.Drawing.Size(121, 33)
Me.cmdSaveAs.TabIndex = 2
Me.cmdSaveAs.Text = "Save Document As..."
'
'AxPageLayoutControl1
'
Me.AxPageLayoutControl1.Location = New System.Drawing.Point(184, 40)
Me.AxPageLayoutControl1.Name = "AxPageLayoutControl1"
Me.AxPageLayoutControl1.OcxState = CType(resources.GetObject("AxPageLayoutControl1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxPageLayoutControl1.Size = New System.Drawing.Size(344, 424)
Me.AxPageLayoutControl1.TabIndex = 7
'
'AxToolbarControl1
'
Me.AxToolbarControl1.Location = New System.Drawing.Point(8, 8)
Me.AxToolbarControl1.Name = "AxToolbarControl1"
Me.AxToolbarControl1.OcxState = CType(resources.GetObject("AxToolbarControl1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxToolbarControl1.Size = New System.Drawing.Size(648, 26)
Me.AxToolbarControl1.TabIndex = 8
'
'AxTOCControl1
'
Me.AxTOCControl1.Location = New System.Drawing.Point(8, 40)
Me.AxTOCControl1.Name = "AxTOCControl1"
Me.AxTOCControl1.OcxState = CType(resources.GetObject("AxTOCControl1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxTOCControl1.Size = New System.Drawing.Size(168, 424)
Me.AxTOCControl1.TabIndex = 9
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.SystemColors.Control
Me.ClientSize = New System.Drawing.Size(662, 503)
Me.Controls.Add(Me.AxTOCControl1)
Me.Controls.Add(Me.AxToolbarControl1)
Me.Controls.Add(Me.AxPageLayoutControl1)
Me.Controls.Add(Me.txtMapDocument)
Me.Controls.Add(Me.cmdOpen)
Me.Controls.Add(Me.cmdSave)
Me.Controls.Add(Me.cmdSaveAs)
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Location = New System.Drawing.Point(4, 23)
Me.Name = "Form1"
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Text = "Save Map Document"
CType(Me.AxPageLayoutControl1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.AxToolbarControl1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.AxTOCControl1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private m_pAoInitialize As IAoInitialize
Private m_pMapDocument As IMapDocument
Private Sub cmdOpen_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOpen.Click
'Open a file dialog for opening map documents
OpenFileDialog1.Title = "Open Map Document"
OpenFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd"
OpenFileDialog1.ShowDialog()
'Exit if no map document is selected
Dim sFilePath As String
sFilePath = OpenFileDialog1.FileName
If sFilePath = "" Then Exit Sub
'Open document
OpenDocument((sFilePath))
If cmdSave.Enabled = False Then cmdSave.Enabled = True
If cmdSaveAs.Enabled = False Then cmdSaveAs.Enabled = True
End Sub
Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
'Save changes to the current document
SaveDocument()
End Sub
Private Sub cmdSaveAs_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSaveAs.Click
'Open a file dialog for saving map documents
SaveFileDialog1.Title = "Save Map Document As"
SaveFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd"
SaveFileDialog1.ShowDialog()
'Exit if no map document is selected
Dim sFilePath As String
sFilePath = SaveFileDialog1.FileName
If sFilePath = "" Then Exit Sub
If sFilePath = m_pMapDocument.DocumentFilename Then
'Save changes to the current document
SaveDocument()
Else
'SaveAs a new document with relative paths
m_pMapDocument.SaveAs(sFilePath, True)
'Open document
OpenDocument((sFilePath))
MsgBox("Document saved successfully!", , "Saved Document")
End If
End Sub
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
'Initialize the application
If CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeEngine) <> esriLicenseStatus.esriLicenseCheckedOut Then
If CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcView) <> esriLicenseStatus.esriLicenseCheckedOut Then
If CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcEditor) <> esriLicenseStatus.esriLicenseCheckedOut Then
If CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcInfo) <> esriLicenseStatus.esriLicenseCheckedOut Then
MsgBox("The initialization failed. This application cannot run!")
Me.Close()
Exit Sub
End If
End If
End If
End If
'Add toolbar definitions to the ToolbarControl
AxToolbarControl1.AddToolbarDef("esriControlCommands.ControlsPageLayoutToolbar", -1, False, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly)
AxToolbarControl1.AddToolbarDef("esriControlCommands.ControlsGraphicElementToolbar", -1, True, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly)
'Set buddy control
AxToolbarControl1.SetBuddyControl(AxPageLayoutControl1)
AxTOCControl1.SetBuddyControl(AxPageLayoutControl1)
cmdSave.Enabled = False
cmdSaveAs.Enabled = False
End Sub
Public Sub OpenDocument(ByRef sFilePath As String)
'Create a new map document
m_pMapDocument = New MapDocument
'Open the map document selected
m_pMapDocument.Open(sFilePath)
'Set the PageLayoutControl page layout to the map document page layout
AxPageLayoutControl1.PageLayout = m_pMapDocument.PageLayout
txtMapDocument.Text = m_pMapDocument.DocumentFilename
End Sub
Public Sub SaveDocument()
'Check that the document is not read only
If m_pMapDocument.IsReadOnly(m_pMapDocument.DocumentFilename) = True Then
MsgBox("This map document is read only!", , "Save Failed")
Exit Sub
End If
'Save with the current relative path setting
m_pMapDocument.Save(m_pMapDocument.UsesRelativePaths)
MsgBox("Changes saved successfully!", , "Saved Document")
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'Release COM objects and shut down the AoInitilaize object
ESRI.ArcGIS.Utility.COMSupport.AOUninitialize.Shutdown()
m_pAoInitialize.Shutdown()
End Sub
Private Function CheckOutLicenses(ByVal productCode As esriLicenseProductCode) As esriLicenseStatus
Dim licenseStatus As esriLicenseStatus
licenseStatus = esriLicenseStatus.esriLicenseUnavailable
m_pAoInitialize = New AoInitialize
If m_pAoInitialize Is Nothing Then CheckOutLicenses = licenseStatus
'Determine if the product is available
licenseStatus = m_pAoInitialize.IsProductCodeAvailable(productCode)
If (licenseStatus = esriLicenseStatus.esriLicenseAvailable) Then
licenseStatus = m_pAoInitialize.Initialize(productCode)
End If
CheckOutLicenses = licenseStatus
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -