阅读:2524回复:8
关于添加layer的代码问题!
<P>总统先生和各位高手:我有下面的类代码(clsaddlayer):</P>
<P>'<BR>Option Explicit</P> <P>Dim m_pApp As IApplication</P> <P>Implements ICommand<BR> <BR>Private Property Get ICommand_Enabled() As Boolean<BR> ICommand_Enabled = True<BR>End Property<BR> <BR>Private Property Get ICommand_Checked() As Boolean<BR> ' TOD Add your implementation here<BR>End Property<BR> <BR>Private Property Get ICommand_Name() As String<BR> ICommand_Name = "Developer Samples_Add Custom Layer"<BR>End Property<BR> <BR>Private Property Get ICommand_Caption() As String<BR> ICommand_Caption = "Add Custom Layer"<BR>End Property<BR> <BR>Private Property Get ICommand_Tooltip() As String<BR> ICommand_Tooltip = "Add Custom Layer To Map"<BR>End Property<BR> <BR>Private Property Get ICommand_Message() As String<BR> ICommand_Message = "Add a Custom Layer to the Map"<BR>End Property<BR> <BR>Private Property Get ICommand_HelpFile() As String<BR> <BR> ' TOD Add your implementation here<BR> <BR>End Property<BR> <BR>Private Property Get ICommand_HelpContextID() As Long<BR> ' TOD Add your implementation here<BR>End Property<BR> <BR>Private Property Get ICommand_Bitmap() As esriSystem.OLE_HANDLE<BR> ' TOD Add your implementation here<BR>End Property<BR> <BR>Private Property Get ICommand_Category() As String<BR> ICommand_Category = "Developer Samples"<BR>End Property<BR> <BR>Private Sub ICommand_OnCreate(ByVal hook As Object)<BR> Set m_pApp = hook<BR>End Sub<BR> <BR>Private Sub ICommand_OnClick()<BR> Dim pCustomLayer As CUSTOMLAYERSLib.ICustomLayer<BR> Set pCustomLayer = New CUSTOMLAYERSLib.CustomLayer<BR> <BR> Dim pMxDoc As IMxDocument<BR> Set pMxDoc = m_pApp.Document‘运行时有问题<BR> <BR> Dim pLayer As ILayer<BR> Set pLayer = pCustomLayer<BR> pLayer.Name = "Test Custom Layer"<BR> <BR> pMxDoc.FocusMap.addlayer pCustomLayer<BR>End Sub</P> <P>Public Sub addlayer()<BR> ICommand_OnClick<BR>End Sub</P> <P><BR>然后定义对象</P> <P>public iii as clsaddlayer</P> <P>iii.addlayer</P> <P>但运行时,出现Set pMxDoc = m_pApp.Document没有定义,什么with之类的错误!</P> <P>请总统先生赐教!谢谢!</P> |
|
1楼#
发布于:2005-09-10 13:06
<P>上面的程序是用于VBA的</P>
|
|
|
2楼#
发布于:2005-09-11 10:46
<P>m_pApp变量应该是接口IMxApplication的一个对象</P>
<P>如果是了,在哪里都可以用到!</P> |
|
3楼#
发布于:2005-09-12 09:39
<P>kisssy,不对吧,上面的程序怎么是用于VBA? 是用VB开发exe的吧</P>
|
|
4楼#
发布于:2005-09-12 14:54
<P>谢谢大家的知道,看来我搞错了。</P>
<P>怎样修改可以变成AE的代码呢?</P> |
|
5楼#
发布于:2005-09-12 16:58
<P 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align=left><FONT face="Times New Roman">Option Explicit <p></p></FONT></P>
<P>'Dim m_pApp As IApplication</P> <P>Private m_pHookHelper <CODE>As</CODE> IHookHelper</P> <P>Implements ICommand</P> <P>Private Sub Class_Initialize()<BR><BR> <CODE>'Load resources</CODE><BR><BR> <CODE>Set</CODE> m_pHookHelper = <CODE>New</CODE> HookHelper<BR> <BR><CODE>End Sub</CODE><BR><BR><CODE>Private Sub</CODE> Class_Terminate()<BR><BR> <CODE>'Clear variables</CODE><BR> <CODE>Set</CODE> m_pHookHelper = <CODE>Nothing</CODE><BR><BR><CODE> </CODE><CODE> </CODE><BR><CODE>End Sub</CODE><BR><BR><BR>Private Property Get ICommand_Enabled() As Boolean<BR> ICommand_Enabled = True<BR>End Property<BR><BR>Private Property Get ICommand_Checked() As Boolean<BR> ' TOD Add your implementation here<BR>End Property<BR><BR>Private Property Get ICommand_Name() As String<BR> ICommand_Name = "Developer Samples_Add Custom Layer"<BR>End Property<BR><BR>Private Property Get ICommand_Caption() As String<BR> ICommand_Caption = "Add Custom Layer"<BR>End Property<BR><BR>Private Property Get ICommand_Tooltip() As String<BR> ICommand_Tooltip = "Add Custom Layer To Map"<BR>End Property<BR><BR>Private Property Get ICommand_Message() As String<BR> ICommand_Message = "Add a Custom Layer to the Map"<BR>End Property<BR><BR>Private Property Get ICommand_HelpFile() As String<BR> <BR> ' TOD Add your implementation here<BR> <BR>End Property<BR><BR>Private Property Get ICommand_HelpContextID() As Long<BR> ' TOD Add your implementation here<BR>End Property<BR><BR>Private Property Get ICommand_Bitmap() As esriSystem.OLE_HANDLE<BR> ' TOD Add your implementation here<BR>End Property<BR><BR>Private Property Get ICommand_Category() As String<BR> ICommand_Category = "Developer Samples"<BR>End Property<BR><BR>Private Sub ICommand_OnCreate(ByVal hook As Object)<BR> Set m_pHookHelper.hook=hook<BR>End Sub<BR><BR>Private Sub ICommand_OnClick()<BR>' Dim pCustomLayer As CUSTOMLAYERSLib.ICustomLayer<BR>' Set pCustomLayer = New CUSTOMLAYERSLib.CustomLayer<BR> <BR>' Dim pMxDoc As IMxDocument<BR>' Set pMxDoc = m_pApp.Document‘运行时有问题<BR> <BR>' Dim pLayer As ILayer<BR>' Set pLayer = pCustomLayer<BR>' pLayer.Name = "Test Custom Layer"<BR> <BR>' pMxDoc.FocusMap.addlayer pCustomLayer</P> <P>'''''首先假设你在你的主窗体frmMain上有一个CommonDialog控件叫cdg</P> <P>'''''下面以对话框添加Shp文件为例</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt">cdg.Filter=”Add ShapeFile(*.Shp)|*.Shp”</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt">cdg.ShowOpen</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt">if cdg.FileName=”” then Exit sub</P> <P>'''''解析cdg.FileName字符串,找出SHP的FeatureClass Path和Name</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt">dim strPath as string 'FeatureClass Path</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt">dim strTempName as string 'FeatureClass Name+”.shp”</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt">dim strName as string ' FeatureClass Name</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt">Dim i As Integer</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt"> i = InStr(cdg.FileName, "\")</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt"> While i > 0</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt"> strPath = Left(cdg.FileName, i - 1)</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt"> strTempName = Mid(cdg.FileName, i + 1)</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt"> i = InStr(i + 1, cdg.FileName, "\")</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt"> Wend</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt">Dim j As Integer</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt"> j = InStr(strTempName, ".")</P> <P 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt"> strName = Left(strTempName, j - 1)</P> <P>''''Open WorkSpace<p></p></P> <P> Dim myFWKS As IFeatureWorkspace<p></p></P> <P> Dim myWKSF As IWorkspaceFactory<p></p></P> <P> Set myWKSF = New ShapefileWorkspaceFactory<p></p></P> <P> Set myFWKS = myWKSF.OpenFromFile(strPath, 0)<p></p></P> <P> If Not myFWKS Is Nothing Then<p></p></P> <P> ''''Open<p></p></P> <P> Dim myFC As IFeatureClass<p></p></P> <P> Set myFC = myFWKS.OpenFeatureClass(strName)<p></p></P> <P> Dim myDS As IDataset<p></p></P> <P> Set myDS = myFC<p></p></P> <P> Dim myFLayer As IFeatureLayer<p></p></P> <P> Set myFLayer = New FeatureLayer<p></p></P> <P> Set myFLayer.FeatureClass = myFC<p></p></P> <P> myFLayer.Name = myDS.Name<p></p></P> <P 24pt">End If</P> <P>''''Add Layer</P><PRE><FONT size=2><CODE>Dim</CODE><FONT face=黑体> pmap </FONT><CODE>As</CODE><FONT face=黑体> Imap<p></p></FONT></FONT></PRE><PRE><FONT size=2><FONT face=黑体> </FONT><CODE>Set</CODE><FONT face=黑体> pmap = m_pHookHelper.FocusMap<p></p></FONT></FONT></PRE><PRE><FONT face=黑体><FONT size=2> <p></p></FONT></FONT></PRE><PRE><FONT face=黑体><FONT size=2> pmap.Addlayer myFlayer<p></p></FONT></FONT></PRE><PRE><FONT face=黑体><FONT size=2> <p></p></FONT></FONT></PRE><PRE><FONT face=黑体 size=2>'''Refresh</FONT></PRE><PRE><FONT size=2><CODE>Dim</CODE><FONT face=黑体> pActiveView </FONT><CODE>As</CODE><FONT face=黑体> esriCarto.IActiveView<p></p></FONT></FONT></PRE><PRE><FONT size=2><FONT face=黑体> </FONT><CODE>Set</CODE><FONT face=黑体> pActiveView = m_pHookHelper.FocusMap<p></p></FONT></FONT></PRE><PRE><FONT size=2><FONT face=黑体> </FONT><CODE>'Set the extent to the full extent</CODE><p></p></FONT></PRE><PRE><FONT face=黑体><FONT size=2> pActiveView.Extent = pActiveView.FullExtent<p></p></FONT></FONT></PRE><PRE><FONT size=2><FONT face=黑体> </FONT><CODE>'Refresh the active view</CODE><p></p></FONT></PRE><PRE><FONT size=2><FONT face=黑体> pActiveView.Refresh<p></p></FONT></FONT></PRE><PRE><FONT size=2><FONT face=黑体> <p></p></FONT></FONT></PRE><BR>End Sub |
|
|
6楼#
发布于:2005-09-13 13:13
<P>谢谢你,我会尽快试试看,然后反映情况,你是个好人!</P>
|
|
7楼#
发布于:2005-09-14 00:41
好人!<img src="images/post/smile/dvbbs/em08.gif" /><img src="images/post/smile/dvbbs/em08.gif" />
|
|
|
8楼#
发布于:2005-10-13 08:46
我也受益了,不错!
|
|