MapWindowのPlugin作成 [Chapter 1]

MapWindowのPlugin作成 [Chapter 1]

MapWinGISについては、後Projectionの操作を残すところとなりました。
今回は、新年第一弾として、MapWindowのPlugin機能を試してみたいと思います。

Plugin機能とは、「MapWindowから利用できる追加のプログラム」と考えれば良いでしょうか。
例えば、MapWindowのPluginでも紹介しています。

わざわざMapWinGISを使わなくても、MapWindowがinstallしてあれば、足りない機能や自分だけのカスタマイズなどを記述することができます。
使用する言語は、C#とvbを使うことができます。

MyFirstPluginのDownload

[Chapter 1]では、ここよりMyFirstPluginを使ってPlugin機能を追加していきます。

まずは、MyFirstPluginをダウンロードします。
Templateはvb2005ですが、vb2010でも変換して使うことができます。

Plugin機能追加に必要なもの

変換したvb2010のMyFirstPluginを開いてみます。
まずは、MyFirstPluginの参照は古いので、新しく参照設定を変更します。

  1. MapWinGIS.ocx
  2. MapWinInterface.dll

をC:¥program files¥MapWindow¥にあるものに置き換えます。
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image01.jpg
MyFirstPluginのソリューションエクスプローラ

ここでは、DefaultでC:¥program files¥MapWindowにMapWindow.exeがinstallされていることを前提として話を進めます。

一度、保存します。
ここで早速実行したいところですが、Pluginで作成されるdllの保存場所を変更します。
MapWindowのpluginを実行させる場合、必ずC:¥program files¥MapWindow¥Plugins内にpluginがなければなりません。
したがって、DebugとReleaseの出力先を変更します。
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image02.jpg
MyFirstPluginの保存場所の変更

また、Debug時には、dll単体では動かないので、MapWindows.exeを起動させてDebugするように変更します。
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image05.jpg
デバッグの変更

最後に、コンパイル→詳細コンパイルオプションを変更します。
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image03.jpg
デバッグの変更

一度保存して、ビルドします。
C:¥program files¥MapWindow¥Pluginsの中身を確認します。
こんな感じで
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image04.jpg
MyFirstPlugin.dllの作成されています。

次に、Debug実行すると、MapWindows.exeが起動します。
その後、メニューのプラグインをクリックすると
こんな感じで
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image11.jpg
MyFirstPluginがメニューに表示されています。

このPluginは単純にメニューに表示するだけですので、選択しても何も起こりません。

PluginのCode

MyFirstPluginのclassのcodeを見てみると

Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
 
Imports MapWindow.Interfaces
Imports MapWinGIS
 
' You must change the name of this class to something unique!
Public Class MyFirstPlugin
    Implements MapWindow.Interfaces.IPlugin
 
    Public g_MapWin As MapWindow.Interfaces.IMapWin
 
    Public ReadOnly Property Name() As String Implements MapWindow.Interfaces.IPlugin.Name
        'This is one of the more important plug-in properties because if it is not set to something then
        'your plug-in will not load at all. This is the name that appears in the Plug-ins menu to identify
        'this plug-in.
        Get
            Return "My First Plugin"
        End Get
    End Property
 
    Public ReadOnly Property Author() As String Implements MapWindow.Interfaces.IPlugin.Author
        'This is the author of the plug-in.  Licensees recieve a Serial Number that corresponds with
        'this Author name.  It can be a company name, individual, or organization name.
        Get
            Return "My Name"
        End Get
    End Property
 
    Public ReadOnly Property SerialNumber() As String Implements MapWindow.Interfaces.IPlugin.SerialNumber
        'This is the plug-in serial number.
        'This is no longer needed, but the property must remain for backward compatibility.
        Get
            Return ""
        End Get
    End Property
 
    Public ReadOnly Property Description() As String Implements MapWindow.Interfaces.IPlugin.Description
        'This is a description of the plug-in.  It appears in the plug-ins dialog box when a user selects
        'your plug-in.
        Get
            Return "This is an example plug-in."
        End Get
    End Property
 
    Public ReadOnly Property BuildDate() As String Implements MapWindow.Interfaces.IPlugin.BuildDate
        'This is the Build Date for the plug-in.  You can either return a string of a hard-coded date
        'such as "January 1, 2003" or you can use the .NET function below to dynamically obtain the build
        'date of the assembly.
        Get
            Return System.IO.File.GetLastWriteTime(Me.GetType().Assembly.Location)
        End Get
    End Property
 
    Public ReadOnly Property Version() As String Implements MapWindow.Interfaces.IPlugin.Version
        'This is the version number of the plug-in.  You can either return a hard-coded string
        'such as "1.0.0.1" or you can use the .NET function shown below to dynamically return
        'the version number from the assembly itself.
        Get
            Return System.Diagnostics.FileVersionInfo.GetVersionInfo(Me.GetType().Assembly.Location).FileVersion
        End Get
    End Property
 
    Public Sub Initialize(ByVal MapWin As MapWindow.Interfaces.IMapWin, ByVal ParentHandle As Integer) Implements MapWindow.Interfaces.IPlugin.Initialize
        'This event is fired when the user loads your plug-in either through the plug-in dialog
        'box, or by checkmarking it in the plug-ins menu.  This is where you would add buttons to the
        'tool bar or menu items to the menu.
        '
        'It is also standard to set a global reference to the IMapWin that is passed through here so that
        'you can access it elsewhere in your project to act on MapWindow.
        g_MapWin = MapWin
    End Sub
 
    Public Sub Terminate() Implements MapWindow.Interfaces.IPlugin.Terminate
        'This event is fired when the user unloads your plug-in either through the plug-in dialog
        'box, or by un-checkmarking it in the plug-ins menu.  This is where you would remove any
        'buttons from the tool bar tool bar or menu items from the menu that you may have added.
        'If you don't do this, then you will leave dangling menus and buttons that don't do anything.
    End Sub
 
    Public Sub ItemClicked(ByVal ItemName As String, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.ItemClicked
        'This event fires when a menu item or toolbar button is clicked.  So if you added a button or menu
        'on the Initialize event, then this is where you would handle it.
    End Sub
 
    Public Sub LayerRemoved(ByVal Handle As Integer) Implements MapWindow.Interfaces.IPlugin.LayerRemoved
        'This event fires when the user removes a layer from MapWindow.  This is useful to know if your
        'plug-in depends on a particular layer being present.
    End Sub
 
    Public Sub LayersAdded(ByVal Layers() As MapWindow.Interfaces.Layer) Implements MapWindow.Interfaces.IPlugin.LayersAdded
        'This event fires when the user adds a layer to MapWindow.  This is useful to know if your
        'plug-in depends on a particular layer being present. Also, if you keep an internal list of
        'available layers, for example you may be keeping a list of all "point" shapefiles, then you
        'would use this event to know when layers have been added or removed.
    End Sub
 
    Public Sub LayersCleared() Implements MapWindow.Interfaces.IPlugin.LayersCleared
        'This event fires when the user clears all of the layers from MapWindow.  As with LayersAdded
        'and LayersRemoved, this is useful to know if your plug-in depends on a particular layer being
        'present or if you are maintaining your own list of layers.
    End Sub
 
    Public Sub LayerSelected(ByVal Handle As Integer) Implements MapWindow.Interfaces.IPlugin.LayerSelected
        'This event fires when a user selects a layer in the legend.
    End Sub
 
    Public Sub LegendDoubleClick(ByVal Handle As Integer, ByVal Location As MapWindow.Interfaces.ClickLocation, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.LegendDoubleClick
        'This event fires when a user double-clicks a layer in the legend.
    End Sub
 
    Public Sub LegendMouseDown(ByVal Handle As Integer, ByVal Button As Integer, ByVal Location As MapWindow.Interfaces.ClickLocation, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.LegendMouseDown
        'This event fires when a user holds a mouse button down in the legend.
    End Sub
 
    Public Sub LegendMouseUp(ByVal Handle As Integer, ByVal Button As Integer, ByVal Location As MapWindow.Interfaces.ClickLocation, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.LegendMouseUp
        'This event fires when a user releases a mouse button in the legend.
    End Sub
 
    Public Sub MapDragFinished(ByVal Bounds As System.Drawing.Rectangle, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.MapDragFinished
        'If a user drags (ie draws a box) with the mouse on the map, this event fires at completion of the drag
        'and returns a system.drawing.rectangle that has the bounds of the box that was "drawn"
    End Sub
 
    Public Sub MapExtentsChanged() Implements MapWindow.Interfaces.IPlugin.MapExtentsChanged
        'This event fires any time there is a zoom or pan that changes the extents of the map.
    End Sub
 
    Public Sub MapMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Integer, ByVal y As Integer, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.MapMouseDown
        'This event fires when the user holds a mouse button down on the map. Note that x and y are returned
        'as screen coordinates (in pixels), not map coordinates.  So if you really need the map coordinates
        'then you need to use g_MapWin.View.PixelToProj()
    End Sub
 
    Public Sub MapMouseMove(ByVal ScreenX As Integer, ByVal ScreenY As Integer, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.MapMouseMove
        'This event fires when the user moves the mouse over the map. Note that x and y are returned
        'as screen coordinates (in pixels), not map coordinates.  So if you really need the map coordinates
        'then you need to use g_MapWin.View.PixelToProj()
    End Sub
 
    Public Sub MapMouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Integer, ByVal y As Integer, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.MapMouseUp
        'This event fires when the user releases a mouse button down on the map. Note that x and y are returned
        'as screen coordinates (in pixels), not map coordinates.  So if you really need the map coordinates
        'then you need to use g_MapWin.View.PixelToProj()
    End Sub
 
    Public Sub Message(ByVal msg As String, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.Message
        'Plug-ins can communicate with eachother using Messages.  If a message is sent then this event fires.
        'If you know the message is "for you" then you can set Handled=True and then it will not be sent to any
        'other plug-ins.
    End Sub
 
    Public Sub ProjectLoading(ByVal ProjectFile As String, ByVal SettingsString As String) Implements MapWindow.Interfaces.IPlugin.ProjectLoading
        'When the user opens a project in MapWindow, this event fires.  The ProjectFile is the file name of the
        'project that the user opened (including its path in case that is important for this this plug-in to know).
        'The SettingsString variable contains any string of data that is connected to this plug-in but is stored
        'on a project level. For example, a plug-in that shows streamflow data might allow the user to set a
        'separate database for each project (i.e. one database for the upper Missouri River Basin, a different
        'one for the Lower Colorado Basin.) In this case, the plug-in would store the database name in the
        'SettingsString of the project.
    End Sub
 
    Public Sub ProjectSaving(ByVal ProjectFile As String, ByRef SettingsString As String) Implements MapWindow.Interfaces.IPlugin.ProjectSaving
        'When the user saves a project in MapWindow, this event fires.  The ProjectFile is the file name of the
        'project that the user is saving (including its path in case that is important for this this plug-in to know).
        'The SettingsString variable contains any string of data that is connected to this plug-in but is stored
        'on a project level. For example, a plug-in that shows streamflow data might allow the user to set a
        'separate database for each project (i.e. one database for the upper Missouri River Basin, a different
        'one for the Lower Colorado Basin.) In this case, the plug-in would store the database name in the
        'SettingsString of the project.
    End Sub
 
    Public Sub ShapesSelected(ByVal Handle As Integer, ByVal SelectInfo As MapWindow.Interfaces.SelectInfo) Implements MapWindow.Interfaces.IPlugin.ShapesSelected
        'This event fires when the user selects one or more shapes using the select tool in MapWindow. Handle is the
        'Layer handle for the shapefile on which shapes were selected. SelectInfo holds information abou the
        'shapes that were selected.
    End Sub
 
End Class

Implements MapWindow.Interfaces.IPluginでInterfaceを定義します。
またPublic subとPublic Propertyは必ず中身がなくても実装されていなければなりません。
MyFirstPluginでは、PropertyのみCodeingしています。

MapWindowのPluginを開発する場合は、上記のcodeが基本となります。
また、必要なIPuginのInterfaceは

Property
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image09.jpg

Method
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image10.jpg

これらのPropertyとMethodに機能を付け加えていくことになります。

今回のまとめ

  1. MapWindowのpluginのtemplateを作成しました。
  2. PluginメニューにMyFirstPluginを作成しました。

次回は、このMyFirstPluginを拡張させたPluginの作成を行います。

MapWindowのPluginを作成することで、カスタマイズが簡単になります。また、MapWinGISでのCodingとほぼ同じように扱えるので、開発は比較的簡単にできます。

MapWindowのPluginの説明ではありませんが、プラグイン機能を持つアプリケーションを作成するでPluginの実装方法について非常にわかりやすく説明されています。

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

WP-SpamFree by Pole Position Marketing

Social Widgets powered by AB-WebLog.com.