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

📄 faq21.htm

📁 C++builder学习资料C++builder
💻 HTM
字号:


<HTML>

<HEAD>

   <TITLE>Display the same icons that Windows uses.</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

<TABLE  BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">



<TR>

<TD>







<H3>

Display the same icons that Windows uses.

</H3>

<P>

 This FAQ could also be called how to display the same icons that

Windows uses. The icons for drives, folders, documents, explorer, the network

neighborhood, and so on, are all located in a system ImageList. Most of the icons

reside in <TT>SHELL32.DLL</TT>, but others are appended to the system ImageList by the

operating system. To display the same icons that the system displays, all you

need to do is get the handle of the system ImageList.

</P>

<P>

<B>Step 1:</B> Add a #include statement for the file <TT>SHELLAPI.H</TT>

</P>

<pre>

    <font color="green">#include &lt;vcl\vcl.h></font>

    <font color="green">#pragma hdrstop</font>



    <font color="green">#include "MAINFORM.h"</font>

    <font color="green">#include &lt;win32\shellapi.h></font>

</pre>

<P>

<B>Step 2:</B> The API <TT>SHGetFileInfo</TT> function allows you to retrieve

the system ImageList. Add an Image control and an ImageList control to the main

form of your program. Don't add any icons to the ImageList. Add this code to

your form's constructor to bind the ImageList to the system ImageList.

</P>

<pre>

  <font color="navy">// Argument description:</font>

  <font color="navy">// 1st arg -> ""           : filename, not needed</font>

  <font color="navy">// 2nd arg -> 0            : file attributes flag, not needed because the last</font>

  <font color="navy">//                           arg does not contain SHGFI_USEFILEATTRIBUTES</font>

  <font color="navy">// 3rd arg -> &info        : this argument is always the address of a</font>

  <font color="navy">//                           SHFILEINFO structure.</font>

  <font color="navy">// 4th arg -> sizeof(info) : this arg is always sizeof(SHFILEINFO)</font>

  <font color="navy">// 5th arg -> flags        : (see notes below)</font>

  SHFILEINFO info<b>;</b>

  DWORD ImageHandle <b>=</b> SHGetFileInfo<b>(</b><font color="blue">""</font><b>,</b>

                                    <font color="blue">0</font><b>,</b>

                                    <b>&</b>info<b>,</b>

                                    <b>sizeof</b><b>(</b>info<b>)</b><b>,</b>

                                    SHGFI_ICON <b>|</b>

                                    SHGFI_SHELLICONSIZE <b>|</b>

                                    SHGFI_SYSICONINDEX<b>)</b><b>;</b>



  <font color="navy">// if the function succeeds, assign the returned handle to the TImageList</font>

  <font color="navy">// member. Note that the TImageList control appears to adjust its height</font>

  <font color="navy">// and width when you assign its handle. Also note that ShareImages is true to</font>

  <font color="navy">// prevent TImageList from attempting to deleting the underlying system</font>

  <font color="navy">// imagelist (although the shell blocks this attempt).</font>

  <b>if</b> <b>(</b>ImageHandle <b>!=</b> <font color="blue">0</font><b>)</b>

  <b>{</b>

    ImageList1<b>-></b>Handle <b>=</b> ImageHandle<b>;</b>

    ImageList1<b>-></b>ShareImages <b>=</b> <b>true</b><b>;</b>

  <b>}</b>



  <font color="navy">// If everything has gone according to plan,</font>

  <font color="navy">// display the first icon in an Image control</font>

  <b>if</b><b>(</b>ImageList1<b>-></b>Count <b>></b> <font color="blue">0</font><b>)</b>

    ImageList1<b>-></b>GetIcon<b>(</b><font color="blue">0</font><b>,</b>Image1<b>-></b>Picture<b>-></b>Icon<b>)</b><b>;</b>  <font color="navy">// 0 is the icon's index</font>

</pre>

<P>

<B>Notes:</B> The return value from SHGetFileInfo takes on a different meaning

based on the last argument to the function. If the last arg contains either

the SHGFI_ICON or the SHGFI_SYSICONINDEX flags, then the return value is the

handle to the system imagelist.

</P>

<P>

Here is a description of th flags for the last argument to SHGetFileInfo:

</P>

<PRE>

      SHGFI_ICON :         specifies that the result will be the handle to the

                           system imagelist (somewhat redundant). Also affects

                           how the info structure will be filled in.



      SHGFI_SHELLICONSIZE: tells the function to use the shell icon size instead

                           of the system icon size. Recall that there are four

                           classes of icons in win95 (system large, system

                           small, shell large and shell small). This parameter

                           isn't "necessary" but it makes sense to use the shell

                           sizes since the imagelist will represent shell items.



      SHGFI_SMALLICON:     tells the function to return the handle of the small

                           icon imagelist. Their are two system image lists; one

                           for small icons and one for large icons. The two are

                           not quite identical. The large icons are sometimes

                           fancier. The large icons for the floppy drives

                           contain little pictures of disks, but the small icons

                           don't. To my knowledge, both the small and large

                           imagelists contain the same number of icons. Omit

                           this parameter to use large icons instead of small

                           icons.



      SHGFI_SYSICONINDEX:  specifies that the return value is the handle of the

                           system image list (redundant with SHGFI_ICON). Of

                           greater importance, it also specifies that info.iIcon

                           will contain the proper image list index for the file

                           name. This is used later on for the drives.

</PRE>





</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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