Thursday, June 5, 2008

IScreenDisplay Example with Description

Looking at ScreenDisplay
Both the PageLayout and the Map have an associated ScreenDisplay
object that is used for all graphic rendering. ScreenDisplay maintains a
DisplayTransformation. You can access the properties of DisplayTransformation
such as VisibleBounds and use the methods to convert coordinates
and distances between map and device units. This is illustrated
by the diagram in Figure 4.
Figure 5
Private Sub Hello()
Dim mxDoc As IMxDocument
Set mxDoc = Application.Document
Dim activeView As IActiveView
Set activeView = mxDoc.activeView
‘ set up a text symbol to draw with
Dim sym As ITextSymbol
Set sym = New TextSymbol
‘ change the font size to 18
Dim fnt As IFontDisp
Set fnt = sym.Font
fnt.Size = 18
sym.Font = fnt
‘ draw into the active view’s display
With activeView.ScreenDisplay
.StartDrawing .hDC, esriNoScreenCache
.SetSymbol sym
Dim bnds As IArea
Set bnds =
.DisplayTransformation.VisibleBounds
.DrawText bnds.Centroid, “Hello”
.FinishDrawing
End With
End Sub
Events such as adding a new layer to the map will cause the ScreenDisplay
to trigger a redraw. The drawing process occurs in three phases.
The drawing order is the geography, the selection, and the annotation.
A drawing cache is an off-screen bitmap of the rendered map that is
stamped onto the screen whenever the window is painted. ScreenDisplay
manages several drawing caches. When you initiate drawing to a
ScreenDisplay you must specify the destination cache. If the constant
esriNoScreenCache is specifi ed, the graphics will draw directly on the
ScreenDisplay’s window and will not be cached. If another window
obscures the display, the graphics will be lost. The following script
draws the text “Hello” in the center of the ScreenDisplay using the esri-
NoScreenCache constant.
In the example in Figure 5, note that the drawing calls SetSymbol and
DrawText are bracketed within the StartDrawing and FinishDrawing
methods.
Working with Layers
Maps maintain a collection of layers. A layer is any object that implements
the ILayer interface. Examples of layer objects include Feature-
Layer, GroupLayer, GraphicsLayer, AnnotationLayer, CadLayer, Tin-
Layer, and RasterLayer. The Layer(index) and LayerCount properties
of the IMap interface can be used to navigate the layer collection. The
function illustrated in Figure 6 fi nds and returns the layer with the specifi
ed name.
Figure 6
Function FindLayer(map As IMap, name As String)
As ILayer
Dim i As Integer
For i = 0 To map.LayerCount - 1
If map.Layer(i).name = name Then
Set FindLayer = map.Layer(i)
Exit Function
End If
Next
End Function
Figure 7
Sub AddLayer()
‘ use a workspaceFactory to open the workspace
Dim wksFact As IWorkspaceFactory
Set wksFact = New Shapefi leWorkspaceFactory
Dim wks As IFeatureWorkspace
Set wks = wksFact.OpenFromFile(“c:\Data\shp”, 0)
‘ open the featureClass
Dim fc As IFeatureClass
Set fc = wks.OpenFeatureClass(“BigCypress”)
‘ create a featureLayer
Dim lyr As IFeatureLayer
Set lyr = New FeatureLayer
Set lyr.FeatureClass = fc
‘ name the layer with the featureClass name
Dim ds As IDataset
Set ds = fc
lyr.Name = ds.Name
‘ add the layer to the map
Dim mxDoc As IMxDocument
Set mxDoc = Application.Document
Dim map As IMap
Set map = mxDoc.FocusMap
map.AddLayer lyr
End Sub
A FeatureLayer manages a connection to a vector data source such as
a coverage, shapefi le, or Spatial Database Engine (SDE) layer through
a FeatureClass object. The script in Figure 7 obtains a FeatureClass
object for a shapefi le by opening the Workspace that contains the shapefi
le using Shapefi leWorkspaceFactory. It then creates a FeatureLayer,
attaches the FeatureClass to the layer, and adds it to the FocusMap.

No comments: