⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csdn_文档中心_怎样用vc在容器端取得activex控件的属性.htm

📁 csdn10年中间经典帖子
💻 HTM
📖 第 1 页 / 共 2 页
字号:
          <TD align=middle width=500></TD></TR>
        <TR>
          <TD align=middle bgColor=#003399 height=10><FONT 
            color=#ffffff>标题</FONT></TD>
          <TD><B>&nbsp;&nbsp;&nbsp;&nbsp;怎样用VC在容器端取得ActiveX控件的属性</B>&nbsp;&nbsp;&nbsp;&nbsp;flower(原作) 
          </TD></TR>
        <TR>
          <TD align=middle height=5></TD>
          <TD align=middle width=500></TD></TR>
        <TR>
          <TD align=middle bgColor=#003399><FONT color=#ffffff>关键字</FONT></TD>
          <TD width=500>&nbsp;&nbsp;&nbsp;&nbsp;怎样用VC在容器端取得ActiveX控件的属性</TD></TR>
        <TR>
          <TD align=middle height=5></TD>
          <TD align=middle width=500></TD></TR></TBODY></TABLE><!--文章说明信息结束//-->
      <TABLE border=0 width=600>
        <TBODY>
        <TR>
          <TD 
            align=left><BR>怎样获得ActiveX控件的各项属性值<BR><BR>我们需要在容器中获得ActiveX控件的各项属性值,这需要利用一些COM库提供的接口,这篇文章主要是对这一过程作一介绍,从而使大家对这些接口有所了解并学会使用.<BR>首先我们需要在容器方的COleClientItem类中添加一个函数,如InitControlInfo().<BR>需要注意的事,这个函数要在创建了控件(即CoCreateInstance())之后,在激活控件(即QuickActive()或SetClientSite())之前调用.<BR>在这个函数中,我们将使用以下几个主要的接口:ITypeInfoPtr, 
            IProvideClassInfoPtr请注意:这两个接口是智能指针,所以你不必去管它们的生存周期.<BR>首先,我们要使用m_lpObject成员变量,这个变量是COleClientItem内部实现的.它其实是一个指向IOleObject的指针,在创建控件的时候你需要给它赋值以便后来的使用. 
            现在我们可以直接使用它.<BR>(一):&nbsp; <BR>&nbsp; m_lpObject-&gt;QueryInterface( 
            IID_IProvideClassInfo,(void**)&amp;pPCI );//&nbsp; 
            由此我们获得了IProvideClassInfoPtr pPCI<BR>&nbsp; pPCI-&gt;GetClassInfo( 
            &amp;pClassInfo );//&nbsp; 获得ITypeInfoPtr pClassInfo<BR>&nbsp; 
            pClassInfo-&gt;GetTypeAttr( &amp;pTypeAttr ) // TYPEATTR* pTypeAttr 
            TYPEATTR使用来描述类型属性的一个结构,它包含很多值,具体可参阅MSDN.<BR>&nbsp; <BR>&nbsp; 
            #define IMPLTYPE_MASK 
            (IMPLTYPEFLAG_FDEFAULT&brvbar;IMPLTYPEFLAG_FSOURCE&brvbar;\IMPLTYPEFLAG_FRESTRICTED)<BR>&nbsp; 
            #define IMPLTYPE_DEFAULTSOURCE 
            (IMPLTYPEFLAG_FDEFAULT&brvbar;IMPLTYPEFLAG_FSOURCE)<BR>&nbsp; #define 
            IMPLTYPE_DEFAULTINTERFACE (IMPLTYPEFLAG_FDEFAULT)<BR><BR>&nbsp; BOOL 
            tFoundDefaultSource = FALSE;<BR>&nbsp; BOOL tFoundDefaultInterface = 
            FALSE;<BR>&nbsp; //&nbsp; 
            这个循环用来找寻声明的资源或接口,pTypeAttr-&gt;cImplTypes是声明的类型的数量.<BR>&nbsp; for( 
            iType = 0; (iType &lt; pTypeAttr-&gt;cImplTypes) &amp;&amp; 
            !(tFoundDefaultSource &amp;&amp; tFoundDefaultInterface); iType++ 
            )<BR>&nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; hResult = 
            pClassInfo-&gt;GetImplTypeFlags( iType, &amp;iFlags );//&nbsp; 
            这里取得的是用序号指定的类型的标志位<BR>&nbsp; &nbsp; &nbsp; &nbsp; if( SUCCEEDED( 
            hResult ) )<BR>&nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; if( (iFlags&amp;IMPLTYPE_MASK) == 
            IMPLTYPE_DEFAULTSOURCE ) //&nbsp; 是否为资源<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ASSERT( !tFoundDefaultSource 
            );<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; tFoundDefaultSource = TRUE;<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hResult = 
            pClassInfo-&gt;GetRefTypeOfImplType( iType, &amp;hRefType );//&nbsp; 
            先取得声明类型的句柄<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; if( FAILED( hResult ) ){return( hResult 
            );}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; hResult = pClassInfo-&gt;GetRefTypeInfo( hRefType, 
            &amp;pTypeInfo );//&nbsp; 利用刚才取得句柄来取得TypeInfo 注:这两步必须这样使用.<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( 
            FAILED( hResult ) {return( hResult );}<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TRACE( "Events:\n" 
            );<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; hResult = m_infoEvents.Init( pTypeInfo ); //&nbsp; 
            利用取得pTypeInfo来初始化控件的事件属性<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; 
            注:m_infoEvents是一个自定义的类CInterfaceInfo,稍候会讲解该类的Init( ITypeInfo* 
            pTypeInfo )函数&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; if( FAILED( hResult ){return( hResult );}<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            pTypeInfo.Release();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; }<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; else if( (iFlags&amp;IMPLTYPE_MASK) == 
            IMPLTYPE_DEFAULTINTERFACE )//&nbsp; 是否为接口<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ASSERT( 
            !tFoundDefaultInterface );<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tFoundDefaultInterface = 
            TRUE;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; hResult = pClassInfo-&gt;GetRefTypeOfImplType( 
            iType, &amp;hRefType );<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( FAILED( hResult ) ){return( 
            hResult );}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; hResult = pClassInfo-&gt;GetRefTypeInfo( 
            hRefType, &amp;pTypeInfo );<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( FAILED( hResult ) 
            ){return( hResult );}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TRACE( "Methods\n" );<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; hResult = m_infoMethods.Init( pTypeInfo );//&nbsp; 
            利用取得pTypeInfo来初始化控件的方法属性<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( FAILED( hResult ) 
            ){return( hResult );}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pTypeInfo.Release();<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; &nbsp; 
            }<BR>&nbsp; } 
            <BR>(二):这是一个自定义的类CInterfaceInfo,用来保存控件的属性值的.在这里,我们主要在它的Init( 
            ITypeInfo* pTypeInfo )中来取得控件的属性.<BR>&nbsp; &nbsp; 
            pTypeInfo-&gt;GetFuncDesc( iMethod, &amp;pFuncDesc ) //&nbsp; 
            这个函数用来取得指定序号的函数的描述<BR>&nbsp; &nbsp; pTypeInfo-&gt;GetVarDesc( iVar, 
            &amp;pVarDesc ) //&nbsp; 
            这个函数用来取得指定序号的变量的描述<BR>注:关于FUNCDESC和VARDESC结构可以参阅MSDN<BR>&nbsp; 
            &nbsp; 这里我们主要来关注一下怎样取属性值,关于函数其实是类似的.<BR>&nbsp; DISPID 
            m_dispid;<BR>&nbsp; BSTR bstrName;<BR>&nbsp; int nNames;<BR>&nbsp; 
            m_dispid = pVarDesc-&gt;memid; //&nbsp; 取得属性的DISPID&nbsp; &nbsp; 
            &nbsp; <BR>&nbsp; pTypeInfo-&gt;GetNames( m_dispid, &amp;bstrName, 
            1, &amp;nNames ); //&nbsp; 利用DISPID取得属性的名字<BR>&nbsp; CString 
            m_strName = bstrName; <BR>&nbsp; SysFreeString( bstrName );&nbsp; 
            //&nbsp; 释放资源<BR>&nbsp; 对于PROPERTYPUT我们可能还需要得到属性的某些参数<BR>&nbsp; 
            pVarDesc-&gt;elemdescVar则是属性的参数描述信息<BR>注:关于ELEMDESC可以参阅MSDN 
            <BR>在这个类中我们可以为控件分配一个数组列表,用来保存从控件中取得的所有属性.这样以后我们就可以拿来使用了.<BR>我在这里大致描述了一下在容器中取得控件属性的方法,有什么不懂得可以e-mail我.<BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0 
width=770>
  <TBODY>
  <TR bgColor=#006699>
    <TD align=middle bgColor=#006699 id=white><FONT 
    color=#ffffff>对该文的评论</FONT></TD>
    <TD align=middle>
      <SCRIPT 
src="CSDN_文档中心_怎样用VC在容器端取得ActiveX控件的属性.files/readnum.htm"></SCRIPT>
    </TD></TR></TBODY></TABLE>
<TABLE align=center bgColor=#666666 border=0 cellPadding=2 cellSpacing=1 
width=770>
  <TBODY>
  <TR>
    <TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #cccccc"><IMG height=16 
      hspace=1 src="CSDN_文档中心_怎样用VC在容器端取得ActiveX控件的属性.files/ico_pencil.gif" 
      width=16> </SPAN>&nbsp;&nbsp;&nbsp;&nbsp; xianming7461746 <I>(2001-6-29 
      10:59:11)</I> </TD></TR>
  <TR>
    <TD bgColor=#ffffff colSpan=3 width=532><BR>如果在TYPEATTR中cVar为0时又怎么做呢? 
    <BR></TD></TR></TBODY></TABLE>
<TABLE align=center bgColor=#666666 border=0 cellPadding=2 cellSpacing=1 
width=770>
  <TBODY>
  <TR>
    <TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #cccccc"><IMG height=16 
      hspace=1 src="CSDN_文档中心_怎样用VC在容器端取得ActiveX控件的属性.files/ico_pencil.gif" 
      width=16> </SPAN>&nbsp;&nbsp;&nbsp;&nbsp; feijunjun <I>(2001-3-25 
      19:09:47)</I> </TD></TR>
  <TR>
    <TD bgColor=#ffffff colSpan=3 width=532><BR>要的就是这个!谢谢这位兄台! ^o^ 
  <BR></TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1 
width=770>
  <TBODY>
  <TR>
    <TH bgColor=#006699 id=white><FONT 
color=#ffffff>我要评论</FONT></TH></TR></TBODY></TABLE></DIV>
<DIV align=center>
<TABLE border=0 width=770>
  <TBODY>
  <TR>
    <TD>你没有登陆,无法发表评论。 请先<A 
      href="http://www.csdn.net/member/login.asp?from=/Develop/read_article.asp?id=911">登陆</A> 
      <A 
href="http://www.csdn.net/expert/zc.asp">我要注册</A><BR></TD></TR></TBODY></TABLE></DIV><BR>
<HR noShade SIZE=1 width=770>

<TABLE border=0 cellPadding=0 cellSpacing=0 width=500>
  <TBODY>
  <TR align=middle>
    <TD height=10 vAlign=bottom><A 
      href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A 
      href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A 
      href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A 
      href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A 
      href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A 
      href="http://www.csdn.net/english">English</A> </TD>
    <TD align=middle rowSpan=3><A 
      href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG 
      border=0 height=48 
      src="CSDN_文档中心_怎样用VC在容器端取得ActiveX控件的属性.files/biaoshi.gif" 
  width=40></A></TD></TR>
  <TR align=middle>
    <TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</TD></TR>
  <TR align=middle>
    <TD vAlign=top><FONT face=Verdana>Copyright &copy; CSDN.net, Inc. All rights 
      reserved</FONT></TD></TR>
  <TR>
    <TD height=15></TD>
    <TD></TD></TR></TBODY></TABLE></DIV>
<DIV></DIV><!--内容结束//--><!--结束//--></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -