sirc_lizheng
伴读书童
伴读书童
  • 注册日期2004-07-09
  • 发帖数148
  • QQ
  • 铜币495枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:2263回复:8

关于添加layer的代码问题!

楼主#
更多 发布于:2005-09-10 10:55
<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>
喜欢0 评分0
kisssy
卧底
卧底
  • 注册日期2004-04-18
  • 发帖数235
  • QQ
  • 铜币614枚
  • 威望2点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2005-09-10 13:06
<P>上面的程序是用于VBA的</P>
个人专栏: https://zhuanlan.zhihu.com/c_165676639
举报 回复(0) 喜欢(0)     评分
cftao2008
路人甲
路人甲
  • 注册日期2005-03-09
  • 发帖数141
  • QQ
  • 铜币568枚
  • 威望0点
  • 贡献值0点
  • 银元0个
2楼#
发布于:2005-09-11 10:46
<P>m_pApp变量应该是接口IMxApplication的一个对象</P>
<P>如果是了,在哪里都可以用到!</P>
举报 回复(0) 喜欢(0)     评分
lzygis
路人甲
路人甲
  • 注册日期2005-08-29
  • 发帖数6
  • QQ
  • 铜币138枚
  • 威望0点
  • 贡献值0点
  • 银元0个
3楼#
发布于:2005-09-12 09:39
<P>kisssy,不对吧,上面的程序怎么是用于VBA? 是用VB开发exe的吧</P>
举报 回复(0) 喜欢(0)     评分
sirc_lizheng
伴读书童
伴读书童
  • 注册日期2004-07-09
  • 发帖数148
  • QQ
  • 铜币495枚
  • 威望0点
  • 贡献值0点
  • 银元0个
4楼#
发布于:2005-09-12 14:54
<P>谢谢大家的知道,看来我搞错了。</P>
<P>怎样修改可以变成AE的代码呢?</P>
举报 回复(0) 喜欢(0)     评分
kisssy
卧底
卧底
  • 注册日期2004-04-18
  • 发帖数235
  • QQ
  • 铜币614枚
  • 威望2点
  • 贡献值0点
  • 银元0个
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
个人专栏: https://zhuanlan.zhihu.com/c_165676639
举报 回复(0) 喜欢(0)     评分
sirc_lizheng
伴读书童
伴读书童
  • 注册日期2004-07-09
  • 发帖数148
  • QQ
  • 铜币495枚
  • 威望0点
  • 贡献值0点
  • 银元0个
6楼#
发布于:2005-09-13 13:13
<P>谢谢你,我会尽快试试看,然后反映情况,你是个好人!</P>
举报 回复(0) 喜欢(0)     评分
license
路人甲
路人甲
  • 注册日期2003-08-20
  • 发帖数235
  • QQ33281522
  • 铜币366枚
  • 威望0点
  • 贡献值0点
  • 银元0个
7楼#
发布于:2005-09-14 00:41
好人!<img src="images/post/smile/dvbbs/em08.gif" /><img src="images/post/smile/dvbbs/em08.gif" />
Gis的小石块 QICQ:33281522 EMAIL:license@vip.sina.com GIS的麦田守望者,希望和大家交流。 〓〓〓〓〓〓〓〓〓 〓 GISEMPIRE 〓 〓 灌水★波菜 〓 〓 专 用 章 〓 〓〓〓〓〓〓〓〓〓
举报 回复(0) 喜欢(0)     评分
echodjx
路人甲
路人甲
  • 注册日期2004-06-06
  • 发帖数37
  • QQ
  • 铜币29枚
  • 威望0点
  • 贡献值0点
  • 银元0个
8楼#
发布于:2005-10-13 08:46
我也受益了,不错!
举报 回复(0) 喜欢(0)     评分
游客

返回顶部