'--------------------------------------------------------------------------------------------------------------------------------------- 'Form1.designer.vb '--------------------------------------------------------------------------------------------------------------------------------------- Partial Class Form1 Inherits System.Windows.Forms.Form 'Form overrides dispose to clean up the component list. Protected Overrides Sub Dispose(ByVal disposing As Boolean) Try If disposing AndAlso components IsNot Nothing Then components.Dispose() End If Finally MyBase.Dispose(disposing) End Try End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer '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. Private Sub InitializeComponent() Me.btnQuit = New System.Windows.Forms.Button() Me.Button2 = New System.Windows.Forms.Button() Me.Label1 = New System.Windows.Forms.Label() Me.Button1 = New System.Windows.Forms.Button() Me.pbmifframe = New System.Windows.Forms.Panel() Me.Button3 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'btnQuit ' Me.btnQuit.Location = New System.Drawing.Point(655, 520) Me.btnQuit.Name = "btnQuit" Me.btnQuit.Size = New System.Drawing.Size(206, 57) Me.btnQuit.TabIndex = 1 Me.btnQuit.Text = "close" Me.btnQuit.UseVisualStyleBackColor = True ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(655, 12) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(206, 145) Me.Button2.TabIndex = 2 Me.Button2.Text = "Select" Me.Button2.UseVisualStyleBackColor = True ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(99, 601) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(77, 25) Me.Label1.TabIndex = 3 Me.Label1.Text = "Label1" ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(655, 163) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(206, 145) Me.Button1.TabIndex = 4 Me.Button1.Text = "Pan" Me.Button1.UseVisualStyleBackColor = True ' 'pbmifframe ' Me.pbmifframe.Location = New System.Drawing.Point(9, 12) Me.pbmifframe.Margin = New System.Windows.Forms.Padding(2, 2, 2, 2) Me.pbmifframe.Name = "pbmifframe" Me.pbmifframe.Size = New System.Drawing.Size(641, 565) Me.pbmifframe.TabIndex = 5 ' 'Button3 ' Me.Button3.Location = New System.Drawing.Point(655, 314) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(206, 145) Me.Button3.TabIndex = 6 Me.Button3.Text = "Zoom to 1000 km" Me.Button3.UseVisualStyleBackColor = True ' 'Form1 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(924, 660) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.pbmifframe) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.btnQuit) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents btnQuit As Button Friend WithEvents Button2 As Button Friend WithEvents Label1 As Label Friend WithEvents Button1 As Button Friend WithEvents pbmifframe As Panel Friend WithEvents Button3 As Button End Class '--------------------------------------------------------------------------------------------------------------------------------------- 'Form1.vb '--------------------------------------------------------------------------------------------------------------------------------------- Imports System.ComponentModel Imports System.Windows.Forms.Integration Imports MapInfo.Application Imports MapInfo.Types Public Class Form1 Implements IIntegratedMappingApplication Public Property FrontDocumentWindow As IWindowInfo Implements IIntegratedMappingApplication.FrontDocumentWindow Get Throw New NotImplementedException() End Get Set(value As IWindowInfo) Throw New NotImplementedException() End Set End Property Public ReadOnly Property IsApplicationClosing As Boolean Implements IIntegratedMappingApplication.IsApplicationClosing Get Throw New NotImplementedException() End Get End Property Public Sub PreviewWindowClosed(window As IWindowInfo) Implements IIntegratedMappingApplication.PreviewWindowClosed Dim x As Integer = 0 'Throw New NotImplementedException() End Sub Public Sub WindowClosed(window As IWindowInfo) Implements IIntegratedMappingApplication.WindowClosed Dim x As Integer = 0 'Throw New NotImplementedException() End Sub Public Sub WindowShowing(window As IWindowInfo, bShow As Boolean) Implements IIntegratedMappingApplication.WindowShowing Dim x As Integer = 0 'Throw New NotImplementedException() End Sub Public Sub ShowNotification(message As String, location As Windows.Point, Optional timeToShow As Integer = 2500) Implements IIntegratedMappingApplication.ShowNotification Dim x As Integer = 0 Throw New NotImplementedException() End Sub Public Sub SetApplicationTitle(title As String) Implements IIntegratedMappingApplication.SetApplicationTitle Dim x As Integer = 0 Throw New NotImplementedException() End Sub Public Sub SetApplicationIcon(icon As Uri) Implements IIntegratedMappingApplication.SetApplicationIcon Dim x As Integer = 0 Throw New NotImplementedException() End Sub Public Sub SetWindowProperty(win As IWindowInfo, [property] As WindowProps, value As String) Implements IIntegratedMappingApplication.SetWindowProperty Dim x As Integer = 0 'Throw New NotImplementedException() End Sub Public Declare Function SetDllDirectoryA Lib "kernel32" (ByVal lpPathName As String) As Boolean Public Shared _application As IMapInfoApplication Private _activeWindow As IWindowInfo Private _lastDocumentWindow As WindowInfo Private _mapWindow As WindowInfo Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim x = SetDllDirectoryA("C:\Program Files\MapInfo\Professional\") System.Windows.Forms.Application.EnableVisualStyles() MapInfoCore.StartUp(Nothing) _application = MapInfoCore.Initialize(Handle, Me) If _application Is Nothing Then Throw New ApplicationException() End If Dim msg = "Open Table ""C:\Users\pault.COOK\Desktop\testdata\fdtrk.tab"" As ""fdtrk""" _application.RunMapBasicCommand(msg) msg = "map from fdtrk" _application.RunMapBasicCommand(msg) msg = "Open Table ""c:\Users\pault.COOK\Desktop\testdata\cook_i_rpoly.tab"" as ""rpoly""" _application.RunMapBasicCommand(msg) msg = "Add Map Layer rpoly" _application.RunMapBasicCommand(msg) msg = "set map zoom entire layer fdtrk" _application.RunMapBasicCommand(msg) End Sub Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click MapInfoCore.ShutDown(True) Me.Close() End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Commands.selectToolStripMenuItem1_Click(Nothing, Nothing) 'This below does the same thing 'Dim msg As String 'msg = "Run Menu Command 1701" '_application.RunMapBasicCommand(msg) Label1.Text = _activeWindow.Name + " " + _activeWindow.Caption End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'panning Dim msg As String msg = "Run Menu Command 1702" MapInfoCore.RunMapBasicCommand(msg) 'This below does the same thing 'Commands.panToolStripMenuItem_Click(sender, e) Label1.Text = _activeWindow.Name + " " + _activeWindow.Caption End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 'set zoom level Dim msg As String msg = "Set Map Zoom 1000 Units ""m""" MapInfoCore.RunMapBasicCommand(msg) End Sub '' '' Method called to choose next active window when '' current active window is closed. '' Public Sub SetNextActiveWindow() Implements IIntegratedMappingApplication.SetNextActiveWindow SetNextActiveWindowInternal() End Sub Public ReadOnly Property ActiveWindow() As IWindowInfo Implements IIntegratedMappingApplication.ActiveWindow Get Return _activeWindow End Get End Property ''' ''' Gets the window client area coordinates. ''' Public Function GetApplicationClientRect() As System.Windows.Rect Implements IIntegratedMappingApplication.GetApplicationClientRect Return New System.Windows.Rect() With { .X = pbmifframe.ClientRectangle.X, .Y = pbmifframe.ClientRectangle.Y, .Width = pbmifframe.ClientRectangle.Width, .Height = pbmifframe.ClientRectangle.Height } End Function ''' ''' Returns the properties of window in application ''' to mapinfo core code. ''' ''' Type of the value required. ''' for which window. ''' property type ''' Value of the window property in given type. Public Function GetWindowProperty(Of TReturnType)(win As IWindowInfo, [property] As WindowProps) As TReturnType Implements IIntegratedMappingApplication.GetWindowProperty Dim returnValue = Nothing Dim winInfo = TryCast(win, WindowInfo) If winInfo Is Nothing Then Return returnValue End If If win.IsReparented OrElse win.IsEmbedded Then Return returnValue End If ' if in a more complicated example you had multiple windows and the information stored in an array or list etc. then ' you would have to search till you found the right window and corresponding forms control to provide the properties ' extracted below (which was populated when the mi window and forms control was created). ' Again, this Is a simple example so refer directly to the control. ie pbmifframe ' only respond to our one map window If win.WindowId <> _mapWindow.WindowId Then Return returnValue End If Dim value As New System.Windows.Rect() Dim empty As Boolean = True If [property] = WindowProps.ContentHeight OrElse [property] = WindowProps.Height Then value.Height = pbmifframe.RectangleToScreen(pbmifframe.ClientRectangle).Height empty = False ElseIf [property] = WindowProps.ContentWidth OrElse [property] = WindowProps.Width Then value.Width = pbmifframe.RectangleToScreen(pbmifframe.ClientRectangle).Width empty = False ElseIf [property] = WindowProps.Position Then value.Height = pbmifframe.RectangleToScreen(pbmifframe.ClientRectangle).Height value.Width = pbmifframe.RectangleToScreen(pbmifframe.ClientRectangle).Width empty = False ElseIf [property] = WindowProps.PositionX Then value.X = pbmifframe.RectangleToScreen(pbmifframe.ClientRectangle).X empty = False ElseIf [property] = WindowProps.PositionY Then value.Y = pbmifframe.RectangleToScreen(pbmifframe.ClientRectangle).Y empty = False End If Return If(empty, returnValue, Convert(Of TReturnType)(value)) End Function ''' ''' Set active window. ''' ''' window to set active ''' Do you need to update the active window in core code also. ''' Active window can change in two ways. Tab switching from UI or mapbasic code. ''' When done via UI then update the active window in core code also. Public Sub SetActiveWindow(win As IWindowInfo, Optional internalUse As Boolean = True) Implements IIntegratedMappingApplication.SetActiveWindow ' If no active window then make the active window as null. If win Is Nothing Then SetActiveWindowInternal(Nothing, internalUse) Return End If Dim wi = TryCast(win, WindowInfo) If wi Is Nothing Then Return End If SetActiveWindowInternal(wi, internalUse) End Sub ''' ''' Called when a window is created. ''' ''' WindowInfo object of newly created window. ''' Default window property. ''' You can use these properties as per the need. Public Sub WindowCreated(window As IWindowInfo, properties As IEnumerable(Of KeyValuePair(Of String, String))) Implements IIntegratedMappingApplication.WindowCreated Dim windowInfo = TryCast(window, WindowInfo) If windowInfo Is Nothing Then Return End If 'These next statements are the trick - here you assign the wpf mapinfo uc to your win forms control. 'With more complicated examples, each time a new mi window is created, create a control to hold it, 'and a variable to hold the mi window info and add it to your new control. Can store all info in an array or list etc. - like in the PB example 'But this is a simple example so refer directly to the one forms control. _mapWindow = windowInfo Dim ctrlHost = New ElementHost() With { .Child = windowInfo.UserControl, .Dock = DockStyle.Fill } pbmifframe.Controls.Add(ctrlHost) '------------------------------------------------------------------------------------------------------------- 'Check if the window created is embedded window (Add map or browser to layout), then set frontwindow & return. If windowInfo.IsReparented OrElse windowInfo.IsEmbedded Then MapInfoCore.SetFrontDocumentWindow(windowInfo) Return End If End Sub ''' ''' Set the window as current active window. ''' ''' Window Info ''' Set window active internally Private Sub SetActiveWindowInternal(windowInfo As WindowInfo, setInCore As Boolean) If windowInfo IsNot Nothing AndAlso windowInfo.IsReparented Then Return End If If _activeWindow Is windowInfo Then Return End If _activeWindow = windowInfo ' if setting frontwindow to null, also make sure the active window is null ' so that a call to SetNextFrontWindow will pick the next window If windowInfo Is Nothing Then If setInCore Then MapInfoCore.SetActiveWindow(_activeWindow) End If Return Else Label1.Text = _activeWindow.Name + " " + _activeWindow.Caption End If If setInCore Then MapInfoCore.SetActiveWindow(_activeWindow) End If End Sub ''' ''' Get last activatable visible window ''' Prefered document windows over any tool windows ''' ''' Ignore window being closed ''' Private Function GetNextWindow(ignoreWindow As WindowInfo) As WindowInfo ' first look for document windows Dim list = _application.Windows.OfType(Of WindowInfo)().Where(Function(x) x IsNot ignoreWindow AndAlso Not x.IsEmbedded AndAlso x.Visible AndAlso x.IsDocumentWindow) Dim windowInfos = If(TryCast(list, WindowInfo()), list.ToArray()) If windowInfos.Any() Then Return windowInfos.First() End If ' ' then consider tool windows list = _application.Windows.OfType(Of WindowInfo)().Where(Function(x) x IsNot ignoreWindow AndAlso Not x.IsEmbedded AndAlso x.Visible) Dim enumerable = If(TryCast(list, WindowInfo()), list.ToArray()) If enumerable.Any() Then Return enumerable.First() End If Return Nothing End Function ''' ''' Choose the next active window ''' ''' When closing a window it may still exist, so we pass it in to make sure it is not picked Private Sub SetNextActiveWindowInternal(Optional ignore As IWindowInfo = Nothing) Dim nextWin = GetNextWindow(TryCast(ignore, WindowInfo)) SetActiveWindow(nextWin) End Sub ''' ''' Convert a type to another type. ''' Private Function Convert(Of T)(v As Object) As T Try If (GetType(T) Is GetType(Integer) AndAlso TypeOf v Is [Enum]) OrElse v.[GetType]() Is GetType(T) Then Return DirectCast(v, T) End If Return DirectCast(TypeDescriptor.GetConverter(GetType(T)).ConvertFrom(v), T) Catch Return Nothing End Try End Function End Class '--------------------------------------------------------------------------------------------------------------------------------------- 'Commands.vb '--------------------------------------------------------------------------------------------------------------------------------------- Imports System.Reflection Imports MapInfo.Application Imports MapInfo.Commands Public Class Commands Inherits Form1 Private Shared ReadOnly _executedCtor As ConstructorInfo Shared Sub New() End Sub Public Sub ExecuteCommand(cmd As MapInfoCommand, sender As Object, Optional param As Object = Nothing) If cmd.Enabled Then Dim ep As System.Windows.Input.ExecutedRoutedEventArgs = Nothing cmd.Execute(Nothing, ep) End If End Sub Public Sub workspaceToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles workspaceToolStripMenuItem.Click ExecuteCommand(MapInfo.Commands.ApplicationCommands.OpenWorkspace, sender) End Sub Public Sub tableToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles tableToolStripMenuItem.Click ExecuteCommand(MapInfo.Commands.ApplicationCommands.OpenTable, sender) End Sub Public Sub closeAllToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles closeAllToolStripMenuItem.Click ExecuteCommand(MapInfo.Commands.ApplicationCommands.CloseAll, sender) End Sub Public Sub closeTableToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles closeTableToolStripMenuItem.Click ExecuteCommand(MapInfo.Commands.ApplicationCommands.CloseTable, sender) End Sub Public Sub layersToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles layersToolStripMenuItem.Click ExecuteCommand(WindowCommands.LayerControl, sender) End Sub Public Sub mapBasicToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles mapBasicToolStripMenuItem.Click ExecuteCommand(WindowCommands.MapBasicWindow, sender) End Sub Public Sub tableListToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles tablesToolStripMenuItem.Click ExecuteCommand(WindowCommands.TableList, sender) End Sub Public Sub newMapToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles mapToolStripMenuItem.Click ExecuteCommand(WindowCommands.NewMapWindow, sender) End Sub Public Sub newBrowserToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles browserToolStripMenuItem.Click ExecuteCommand(WindowCommands.NewBrowserWindow, sender) End Sub Public Sub layoutDesignerToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles layoutDesignerToolStripMenuItem.Click ExecuteCommand(WindowCommands.NewLayoutWindow, sender) End Sub Public Sub windowListToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles windowsToolStripMenuItem.Click ExecuteCommand(WindowCommands.WindowList, sender) End Sub Public Sub selectToolStripMenuItem1_Click(sender As Object, e As EventArgs) 'Handles SelectTool.Click ExecuteCommand(ToolCommands.Select, sender) End Sub Public Sub marqueeSelectToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles MarSelectTool.Click ExecuteCommand(ToolCommands.SearchRect, sender) End Sub Public Sub boundarySelectToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles BoundSelectTool.Click ExecuteCommand(ToolCommands.SearchBoundary, sender) End Sub Public Sub radiusSelectionToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles RadSelectTool.Click ExecuteCommand(ToolCommands.SearchRadius, sender) End Sub Public Sub polygonSelectionToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles PolySelectTool.Click ExecuteCommand(ToolCommands.SearchPolygon, sender) End Sub Public Sub zoomInToolStripMenuItem_Click(sender As Object, e As EventArgs) ' Handles ZoomIn.Click ExecuteCommand(ToolCommands.ZoomIn, sender) End Sub Public Sub zoomOutToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles ZoomOut.Click ExecuteCommand(ToolCommands.ZoomOut, sender) End Sub Public Sub panToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles Pan.Click ExecuteCommand(ToolCommands.Recenter, sender) End Sub Public Sub explorerToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles explorerToolStripMenuItem.Click ExecuteCommand(WindowCommands.WorkspaceExplorer, Nothing) End Sub Public Sub preferencesToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles preferencesToolStripMenuItem.Click MapInfoCore.RunMapBasicCommand("run menu command 208") End Sub Public Sub addMapToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles addMapToolStripMenuItem.Click ExecuteCommand(LayoutCommands.AddMapFrame, sender) End Sub Public Sub addBrowserToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles addBrowserToolStripMenuItem.Click ExecuteCommand(LayoutCommands.AddBrowserFrame, sender) End Sub Public Sub clearToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles ClearSelection.Click ExecuteCommand(QueryCommands.UnSelect, sender) End Sub Public Sub textToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles textToolStripMenuItem.Click ExecuteCommand(LayoutCommands.InsertText, sender) End Sub Public Sub LineToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles LineToolStripMenuItem.Click ExecuteCommand(LayoutCommands.AddLine, sender) End Sub Public Sub EllipseToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles EllipseToolStripMenuItem.Click ExecuteCommand(LayoutCommands.AddEllipse, sender) End Sub Public Sub RectangleToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles RectangleToolStripMenuItem.Click ExecuteCommand(LayoutCommands.AddRectangle, sender) End Sub Public Sub RoundedToolStripMenuItem_Click(sender As Object, e As EventArgs) 'Handles RoundedToolStripMenuItem.Click ExecuteCommand(LayoutCommands.AddRoundedRectangle, sender) End Sub Public Sub InfoToolButon_Click(sender As Object, e As EventArgs) 'Handles InfoToolButon.Click ExecuteCommand(ToolCommands.PointQuery, sender) End Sub ' Public Sub OnInfoToolCommand(param As Object) Dim x1 As String = _application.EvalMapBasicCommand("commandinfo(1)") Dim y1 As String = _application.EvalMapBasicCommand("commandinfo(2)") Label1.Text = String.Format("X: {0} Y: {1}", x1, y1) End Sub Public Sub AreaInfo_Click(sender As Object, e As EventArgs) 'Handles AreaInfo.Click Try Dim numSelections = Int32.Parse(_application.EvalMapBasicCommand("SelectionInfo(3)", False)) If numSelections <= 0 Then Return End If Dim tableName = _application.EvalMapBasicCommand("SelectionInfo(2)", False) Dim fSqMiles As Single = 0 _application.RunMapBasicCommand("fetch first from " + tableName) For i As Integer = 0 To numSelections - 1 fSqMiles += Single.Parse(_application.EvalMapBasicCommand("Area(Selection.obj, ""sq mi"")", False)) _application.RunMapBasicCommand("fetch next from " + tableName) Next _application.RunMapBasicCommand("close table " & tableName, False, False) MessageBox.Show(String.Format("Area of {0} selected object(s) : {1} sq mi.", numSelections, fSqMiles)) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub End Class