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

📄 ch20.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<pre><font color="#008000">WIDTH=&quot;200&quot;</font></pre>

<pre><font color="#008000">HEIGHT=&quot;200&quot;&gt;</font></pre>

<pre><font color="#008000">&lt;PARAM NAME=&quot;ForeColor&quot; VALUE=&quot;0&quot;&gt;</font></pre>

<pre><font color="#008000">&lt;PARAM NAME=&quot;BackColor&quot; VALUE=&quot;16777215&quot;&gt;</font></pre>

<pre><font color="#008000">&lt;PARAM NAME=&quot;Image&quot; VALUE=&quot;beans.bmp&quot;&gt;</font></pre>

<pre><font color="#008000">&lt;EMBED LIVECONNECT NAME=&quot;Dieroll1&quot;</font></pre>

<pre><font color="#008000">WIDTH=&quot;200&quot;</font></pre>

<pre><font color="#008000">HEIGHT=&quot;200&quot;</font></pre>

<pre><font color="#008000">CLASSID=&quot;CLSID:46646B43-EA16-11CF-870C-00201801DDD6&quot;</font></pre>

<pre><font color="#008000">TYPE=&quot;application/oleobject&quot;</font></pre>

<pre><font color="#008000">CODEBASE=&quot;dieroll.cab#Version=1,0,0,1&quot;</font></pre>

<pre><font color="#008000">PARAM_ForeColor=&quot;0&quot;</font></pre>

<pre><font color="#008000">PARAM_BackColor=&quot;16777215&quot;&gt;</font></pre>

<pre><font color="#008000">&lt;/OBJECT&gt;</font></pre>

<pre><font color="#008000">&lt;BR&gt;</font></pre>

<pre><font color="#008000">Here is some text after the die</font></pre>

<pre><font color="#008000">&lt;/BODY&gt;</font></pre>

<pre><font color="#008000">&lt;/HTML&gt;</font></pre>

<P>It is the <font color="#008000">&lt;EMBED&gt;</font> tag that brings up the plug-in. Because it&#146;s inside the <font color="#008000">&lt;OBJECT&gt;...&lt;/OBJECT&gt;</font> tag, Microsoft Internet Explorer and other browsers that know the <font 
color="#008000">OBJECT</font> tag will ignore the <font color="#008000">EMBED</font>. This means that this HTML source will display the control equally well in Netscape Navigator and in Explorer. You will probably want to include a link on your page to the 
NCompass page to help your readers get and learn about the plug-in.</P>

<P>Microsoft is committed to establishing ActiveX controls as a cross-platform, multi-browser solution that will, in the words of its slogan, &quot;Activate the Internet.&quot; The ActiveX control specification is no longer a proprietary document, but has 
been released to a committee that will maintain the standard. So don't pay any attention to people who suggest you should only build these controls if your readers use Internet Explorer!</P>

<H3><A ID="I5" NAME="I5"><B>Registering as Safe for Scripting and Initializing</B></A></H3>

<P>For any of your readers who operate with a Medium Safety level, the control should be registered as safe for scripting and initializing. This assures anyone who wants to view a page containing the control that no matter what functions are called from a 
script, or what parameters are initialized through the <font color="#008000">PARAM</font> attribute, nothing unsafe will happen. For an example of a control that is not safe, think of a control that deletes a file on your machine when it executes. The 
default file is one you won't miss or that probably won't exist. A page that put this control in a script, or that initialized the file name with <font color="#008000">PARAM</font> attributes, might order the control to delete a very important file or 
files, based on guesses about where most people keep documents. It would be simple to delete C:\MSOFFICE\WINWORD\WINWORD.EXE, for example, and that would be annoying for Word users. Figure 20.4 shows the error message displayed in Explorer when you are 
using the Medium safety level and load a page featuring a control that is not registered as script-safe or init-safe. The NCompassLabs plug-in, ControlActive, also refuses to load controls that are not registered as script-safe and init-safe.</P>

<A HREF="Vfigs04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch20/Vfigs04.gif"><b>Fig. 20.4</b></A>

<P><I>Explorer alerts you to controls that might run amok.</I></P>

<P>First, you need to add three functions to DierollCtl.cpp. (These come unchanged from the ActiveX SDK.) These functions are called by code presented later in this section. Don't forget to add declarations of the these functions to the header file too. 
The code is in Listing 20.3.</P>

<P><I>Listing 20.3&#151;DierollCtl.cpp&#151;new functions to mark the control as </I><I>safe</I></P>

<pre><font color="#008000"> ////////////////////////////////////////////////////////////////</font></pre>

<pre><font color="#008000"> // Copied from the ActiveX SDK</font></pre>

<pre><font color="#008000"> // This code is used to register and unregister a</font></pre>

<pre><font color="#008000"> // control as safe for initialization and safe for scripting</font></pre>

<pre><font color="#008000"> HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription)</font></pre>

<pre><font color="#008000"> {</font></pre>

<pre><font color="#008000">      ICatRegister* pcr = NULL ;</font></pre>

<pre><font color="#008000">      HRESULT hr = S_OK ;</font></pre>

<pre><font color="#008000">      hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,</font></pre>

<pre><font color="#008000">             NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&amp;pcr);</font></pre>

<pre><font color="#008000">      if (FAILED(hr))</font></pre>

<pre><font color="#008000">             return hr;</font></pre>

<pre><font color="#008000">      // Make sure the HKCR\Component Categories\{..catid...}</font></pre>

<pre><font color="#008000">      // key is registered</font></pre>

<pre><font color="#008000">      CATEGORYINFO catinfo;</font></pre>

<pre><font color="#008000">      catinfo.catid = catid;</font></pre>

<pre><font color="#008000">      catinfo.lcid = 0x0409 ; // english</font></pre>

<pre><font color="#008000">      // Make sure the provided description is not too long.</font></pre>

<pre><font color="#008000">      // Only copy the first 127 characters if it is</font></pre>

<pre><font color="#008000">      int len = wcslen(catDescription);</font></pre>

<pre><font color="#008000">      if (len&gt;127)</font></pre>

<pre><font color="#008000">            len = 127;</font></pre>

<pre><font color="#008000">      wcsncpy(catinfo.szDescription, catDescription, len);</font></pre>

<pre><font color="#008000">      // Make sure the description is null terminated</font></pre>

<pre><font color="#008000">      catinfo.szDescription[len] = '\0';</font></pre>

<pre><font color="#008000">      hr = pcr-&gt;RegisterCategories(1, &amp;catinfo);</font></pre>

<pre><font color="#008000">      pcr-&gt;Release();</font></pre>

<pre><font color="#008000">      return hr;</font></pre>

<pre><font color="#008000">}</font></pre>

<pre><font color="#008000">HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    // Register your component categories information.</font></pre>

<pre><font color="#008000">    ICatRegister* pcr = NULL ;</font></pre>

<pre><font color="#008000">    HRESULT hr = S_OK ;</font></pre>

<pre><font color="#008000">    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,</font></pre>

<pre><font color="#008000">            NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&amp;pcr);</font></pre>

<pre><font color="#008000">    if (SUCCEEDED(hr))</font></pre>

<pre><font color="#008000">    {</font></pre>

<pre><font color="#008000">           // Register this category as being &quot;implemented&quot; by</font></pre>

<pre><font color="#008000">           // the class.</font></pre>

<pre><font color="#008000">           CATID rgcatid[1] ;</font></pre>

<pre><font color="#008000">           rgcatid[0] = catid;</font></pre>

<pre><font color="#008000">           hr = pcr-&gt;RegisterClassImplCategories(clsid, 1, rgcatid);</font></pre>

<pre><font color="#008000">    }</font></pre>

<pre><font color="#008000">    if (pcr != NULL)</font></pre>

<pre><font color="#008000">          pcr-&gt;Release();</font></pre>

<pre><font color="#008000">                     </font></pre>

<pre><font color="#008000">    return hr;</font></pre>

<pre><font color="#008000">}</font></pre>

<pre><font color="#008000">HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    ICatRegister* pcr = NULL ;</font></pre>

<pre><font color="#008000">    HRESULT hr = S_OK ;</font></pre>

<pre><font color="#008000">    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,</font></pre>

<pre><font color="#008000">             NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&amp;pcr);</font></pre>

<pre><font color="#008000">    if (SUCCEEDED(hr))</font></pre>

<pre><font color="#008000">    {</font></pre>

<pre><font color="#008000">        // Unregister this category as being &quot;implemented&quot; by</font></pre>

<pre><font color="#008000">        // the class.</font></pre>

<pre><font color="#008000">        CATID rgcatid[1] ;</font></pre>

<pre><font color="#008000">        rgcatid[0] = catid;</font></pre>

<pre><font color="#008000">        hr = pcr-&gt;UnRegisterClassImplCategories(clsid, 1, rgcatid);</font></pre>

<pre><font color="#008000">    }</font></pre>

<pre><font color="#008000">    if (pcr != NULL)</font></pre>

<pre><font color="#008000">    pcr-&gt;Release();</font></pre>

<pre><font color="#008000">                     </font></pre>

<pre><font color="#008000">    return hr;</font></pre>

<pre><font color="#008000">}</font></pre>

<P>Second, add two <font color="#008000">#include</font> statements at the top of DierollCtl.cpp:</P>

<pre><font color="#008000">#include &quot;comcat.h&quot;</font></pre>

<pre><font color="#008000">#include &quot;objsafe.h&quot;</font></pre>

<P>Finally, modify <font color="#008000">UpdateRegistry()</font> in DierollCtl.cpp to call these new functions. The new code calls <font color="#008000">CreateComponentCategory()</font> to create a category called &quot;CATID_SafeForScripting&quot;, then 
adds this control to that category. Then it creates a category called &quot;CATID_SafeForInitializing&quot; and adds the control to that category as well. Listing 20.4 shows the new version of <font color="#008000">UpdateRegistry()</font>.</P>

⌨️ 快捷键说明

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