FAQ
ジオメトリを一時的に描画する方法

ナレッジ番号:5267 | 登録日:2023/07/26 | 更新日:2023/12/28

概要

IScreenDisplay::Draw メソッドを使用してジオメトリを一時的に描画することができます。一時的な描画を用いることでレイヤーやグラフィック エレメントなどの永続的データを用いずにジオメトリの描画が実現できます。

一時的な描画はマップが再描画されると消えてしまいますが、再描画のイベントをハンドリングすることによって一時的な描画を継続的に実行することができます。

サンプル コード

一時的なジオメトリの描画

ジオメトリを一時的に描画するサンプルです。事前にシンボル オブジェクトとジオメトリ オブジェクトを作成してください。

//ArcGIS for Desktop アドインを使用する場合
//IMxApplication pMxApplication = (IMxApplication)ArcMap.Application;
//IDisplay pDisplay = pMxApplication.Display;
//IActiveView pActiveView = ArcMap.Document.ActiveView;

//MapControl を使用する場合
IDisplay pDisplay  = axMapControl1.ActiveView.ScreenDisplay;
IActiveView pActiveView = axMapControl1.ActiveView;

//SimpleFillSymbol オブジェクト(黒塗りつぶし)の作成
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
ISymbol pSymbol= (ISymbol)pSimpleFillSymbol;
pSimpleFillSymbol.Color = new RgbColorClass();
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;

//Polygon オブジェクトの作成
IPolygon pPolygon = new PolygonClass();
ISegmentCollection pSegmentCollection = (ISegmentCollection)pPolygon;
pSegmentCollection.SetRectangle(pActiveView.Extent);

//ディスプレイの準備
pDisplay.StartDrawing(pDisplay.hDC, (int)esriScreenCache.esriNoScreenCache); 
//描画するジオメトリ タイプに応じたシンボル
pDisplay.SetSymbol(pSymbol); 
//Polygon を描画する場合
pDisplay.DrawPolygon(pPolygon);     
//ビューへ描画     
pDisplay.FinishDrawing();                     
'ArcGIS for Desktop アドインを使用する場合
'Dim pMxApplication As IMxApplication = CType(My.ArcMap.Application, IMxApplication)
'Dim pDisplay As IDisplay = pMxApplication.Display
'Dim pActiveView As IActiveView = ArcMap.Document.ActiveView

'MapControl を使用する場合
Dim pDisplay As IDisplay = axMapControl1.ActiveView.ScreenDisplay
Dim pActiveView As IActiveView = axMapControl1.ActiveView

'SimpleFillSymbol オブジェクト(黒塗りつぶし)の作成
Dim pSimpleFillSymbol As ISimpleFillSymbol = New SimpleFillSymbolClass()
Dim pSymbol As ISymbol = CType(pSimpleFillSymbol, ISymbol)
pSimpleFillSymbol.Color = New RgbColorClass()
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid

'Polygon オブジェクトの作成
Dim pPolygon As IPolygon = New PolygonClass()
Dim pSegmentCollection As ISegmentCollection = CType(pPolygon, ISegmentCollection)
pSegmentCollection.SetRectangle(pActiveView.Extent)

With pDisplay
    'ディスプレイの準備
    .StartDrawing(pDisplay.hDC, CType(esriScreenCache.esriNoScreenCache, Short)) 
  '描画するジオメトリ タイプに応じたシンボル
    .SetSymbol(pSymbol)
    'Polygon を描画する場合                    
    .DrawPolygon(pPolygon) 
    'ビューへ描画            
    .FinishDrawing()                      
End With

一時的な描画を用いた継続的な描画

マップを再描画しても継続的に一時的な描画を行うサンプルです。このサンプルはアクティブなデータ フレームに存在する最上位のレイヤー範囲をシンプル ライン シンボルで描画します。
※イベント ハンドラーについての詳細は「イベントのハンドリング方法」をご参照ください。

//一時的な描画の有効化・無効化の制御
private IActiveViewEvents_Event m_pActiveViewEvents;
private void Button1_Click(object sender, EventArgs e)
{
    //ArcGIS for Desktop アドインを使用する場合
    //IActiveView pActiveView = ArcMap.Document.ActiveView;
    //IViewManager pViewManager = (IViewManager)ArcMap.Document.FocusMap;

    //MapControl を使用する場合
    IActiveView pActiveView = axMapControl1.ActiveView;
    IViewManager pViewManager = (IViewManager)axMapControl1.Map;

    m_pActiveViewEvents = (IActiveViewEvents_Event)axMapControl1.ActiveView;

    if (Button1.Checked == false)
    {
        //イベント ハンドラーの登録
        m_pActiveViewEvents.AfterItemDraw += new IActiveViewEvents_AfterItemDrawEventHandler(myAfterItemDraw);

        pViewManager.VerboseEvents = true; //AfterItemDraw イベントの有効化
        pActiveView.Refresh();

        Button1.Checked = !Button1.Checked;
    }
    else
    {
        //イベント ハンドラーの登録解除
        m_pActiveViewEvents.AfterItemDraw -= new IActiveViewEvents_AfterItemDrawEventHandler(myAfterItemDraw);

        pViewManager.VerboseEvents = false; //AfterItemDraw イベントの無効化
        pActiveView.Refresh();

        Button1.Checked = !Button1.Checked;
    }
}

//AfterItemDraw イベント
private void myAfterItemDraw(short Index, IDisplay Display, esriDrawPhase phase)
{
    //Polygon オブジェクトの作成
    //IGeoDataset pGeoDataset = (IGeoDataset)ArcMap.Document.FocusMap.get_Layer(0);
    IGeoDataset pGeoDataset = (IGeoDataset)axMapControl1.Map.get_Layer(0);
    IPolyline pPolyline = new PolylineClass();
    ISegmentCollection pSegmentCollection = (ISegmentCollection)pPolyline;
    pSegmentCollection.SetRectangle(pGeoDataset.Extent);


    //SimpleLineSymbol オブジェクト(黒塗りつぶし)の作成
    ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbol();
    ISymbol pSymbol = (ISymbol)pSimpleLineSymbol;
    pSimpleLineSymbol.Color = new RgbColorClass();
    pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
    pSimpleLineSymbol.Width = 2;


    if (phase != esriDrawPhase.esriDPGeography) { return; }

    //IScreenDisplay pScreenDisplay = ArcMap.Document.ActiveView.ScreenDisplay;
    IScreenDisplay pScreenDisplay = axMapControl1.ActiveView.ScreenDisplay;
    
    pScreenDisplay.StartDrawing(pScreenDisplay.hDC, 0);
    pScreenDisplay.SetSymbol((ISymbol)pSimpleLineSymbol);
    pScreenDisplay.DrawPolyline(pPolyline);
    pScreenDisplay.FinishDrawing();
}
'一時的な描画の有効化・無効化の制御
'Private m_pActiveViewEvents As IActiveViewEvents_Event

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handletton1.Click
    'ArcGIS for Desktop アドインを使用する場合
    'Dim pActiveView As IActiveView  = My.ArcMap.Document.ActiveView
    'Dim pViewManager As IViewManager = CType(ArcMap.Document.FocusMap, IViewManager)

    'MapControl を使用する場合
    Dim pActiveView As IActiveView = axMapControl1.ActiveView
    Dim pViewManager As IViewManager = CType(axMapControl1.Map, IViewManager)

    m_pActiveViewEvents = CType(axMapControl1.ActiveView, IActiveViewEvents_Event)

    If Button1.Checked = False Then
        'イベント ハンドラーの登録
        AddHandler m_pActiveViewEvents.AfterItemDraw, AddressOf myAfterItemDraw

        pViewManager.VerboseEvents = True   'イベントの有効化
        pActiveView.Refresh()

        Button1.Checked = Not Button1.Checked
    Else
        'イベント ハンドラーの登録解除
        RemoveHandler m_pActiveViewEvents.AfterItemDraw, AddressOf myAfterItemDraw

        pViewManager.VerboseEvents = False  'AfterItemDraw イベントの無効化
        pActiveView.Refresh()

        Button1.Checked = Not Button1.Checked
    End If

End Sub

'AfterItemDraw イベント
Private Sub myAfterItemDraw(Index As Short, Display As IDisplay, phase As esriDrawPhase)
    'Polygon オブジェクトの作成
    'Dim pGeoDataset As IGeoDataset = CType(ArcMap.Document.FocusMap.Layer(0), IGeoDataset)
    Dim pGeoDataset As IGeoDataset = CType(axMapControl1.Map.Layer(0), IGeoDataset)

    Dim pPolyline As IPolyline = New PolylineClass()
    Dim pSegmentCollection As ISegmentCollection = CType(pPolyline, ISegmentCollection)
    pSegmentCollection.SetRectangle(pGeoDataset.Extent)


    'SimpleLineSymbol オブジェクト(黒塗りつぶし)の作成
    Dim pSimpleLineSymbol As ISimpleLineSymbol = New SimpleLineSymbol()
    Dim pSymbol As ISymbol = CType(pSimpleLineSymbol, ISymbol)
    pSimpleLineSymbol.Color = New RgbColorClass()
    pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid
    pSimpleLineSymbol.Width = 2


    If phase = Not esriDrawPhase.esriDPGeography Then
        Return
    End If

    'Dim pScreenDisplay As IScreenDisplay = ArcMap.Document.ActiveView.ScreenDis
    Dim pScreenDisplay As IScreenDisplay = axMapControl1.ActiveView.ScreenDisplay

    pScreenDisplay.StartDrawing(pScreenDisplay.hDC, 0)
    pScreenDisplay.SetSymbol(CType(pSimpleLineSymbol, ISymbol))
    pScreenDisplay.DrawPolyline(pPolyline)
    pScreenDisplay.FinishDrawing()
End Sub

メタデータ

種類

製品