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

📄 faq22.htm

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


<HTML>

<HEAD>

   <TITLE>Get the icon for a file or folder.</TITLE>

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

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

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



<TR>

<TD>

<H3>

Get the icon for a file or folder.

</H3>

<P>

 Displaying the icon for a file or folder is a simple, two step

process. First, you obtain a handle for the system ImageList. Next, you pass a

file or directory name to the API <TT>SHGetFileInfo</TT> function and ask it to

return the corresponding index number for an icon in 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;win32\shellapi.h></font>

</pre>

<P>

<B>Step 2:</B> Add an ImageList control to the main form of your program. Don't

add any icons to the ImageList. Edit the constructor of the main form and add

code to bind the ImageList control to the system ImageList. See the FAQ on how to

<A TARGET=_top HREF="faq21.htm">display the same icons used by Windows</A>

for more documentation.

</P>

<pre>

  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>



  <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>

</pre>

<P>

<B>Step 3:</B> Now add code that will return an icon index for the file,

directory, or drive that you care about. Here are three examples:

</P>

<pre>

  <font color="navy">// This code fetches the icon for the C:\ drive</font>

  SHFILEINFO info<b>;</b>

  DWORD result <b>=</b> SHGetFileInfo<b>(</b><font color="blue">"C:\\"</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">// Check for failure. If return value was OK, the icon index</font>

  <font color="navy">// is return in the iIcon member of the SHFILEINFO structure.</font>

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

    ImageList1<b>-></b>GetIcon<b>(</b>info<b>.</b>iIcon<b>,</b>Image1<b>-></b>Picture<b>-></b>Icon<b>)</b><b>;</b>







  <font color="navy">// This code fetches the icon for the C:\windows directory</font>

  SHFILEINFO info<b>;</b>

  DWORD result <b>=</b> SHGetFileInfo<b>(</b><font color="blue">"C:\\WINDOWS"</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>



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

    ImageList1<b>-></b>GetIcon<b>(</b>info<b>.</b>iIcon<b>,</b>Image1<b>-></b>Picture<b>-></b>Icon<b>)</b><b>;</b>







  <font color="navy">// This code fetches the icon for a text file</font>

  SHFILEINFO info<b>;</b>

  DWORD result <b>=</b> SHGetFileInfo<b>(</b><font color="blue">"C:\\CBUILDER\DEPLOY.TXT"</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>



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

    ImageList1<b>-></b>GetIcon<b>(</b>info<b>.</b>iIcon<b>,</b>Image1<b>-></b>Picture<b>-></b>Icon<b>)</b><b>;</b>



</pre>



</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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