faq53.htm

来自「C++builder学习资料C++builder」· HTM 代码 · 共 101 行

HTM
101
字号


<HTML>

<HEAD>

   <TITLE>Load bitmaps and icons from the programs's resources</TITLE>

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

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

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



<TR>

<TD>

<H3>

Load bitmaps and icons from the programs's resources

</H3>

<P>

This FAQ assumes that you have already bound your bitmaps or icons to your executable.

If you need help adding bitmaps or icons to a project, see the FAQ on

<A TARGET=_top HREF="faq52.htm">adding icons, cursors, and bitmaps to a BCB project</A>.

</P>

<B>Bitmaps</B>

<P>

<TT>TBitmap</TT> contains two functions called <TT>LoadFromResourceID</TT> and

<TT>LoadFromResourceName</TT> that allow you to load bitmaps from the program's resources.

Use <TT>LoadFromResourceID</TT> when the ID of your bitmap resource is an integer

(the <A TARGET=_top HREF="faq52.htm">RC file example</A> used numeric ID's). Use

<TT>LoadFromResourceName</TT> when the bitmap's ID is a string. If you used

the Image Editor to create a <TT>.RES</TT> file, you will need <TT>LoadFromResourceName</TT>

because the Image Editor saves the resources using string IDs. Here are some code examples.

</P>

<pre>

  <font color="navy">// Loading a bitmap that uses numeric IDs.</font>

  Graphics<b>:</b><b>:</b>TBitmap <b>*</b>bmp <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TBitmap<b>;</b>

  bmp<b>-></b>LoadFromResourceID<b>(</b><b>(</b><b>int</b><b>)</b>HInstance<b>,</b>IDB_SPLASH<b>)</b><b>;</b>



  <font color="navy">// Loading a bitmap that uses string IDs, like the</font>

  <font color="navy">// ones that the Image Editor creates.</font>

  Graphics<b>:</b><b>:</b>TBitmap <b>*</b>bmp <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TBitmap<b>;</b>

  bmp<b>-></b>LoadFromResourceName<b>(</b><b>(</b><b>int</b><b>)</b>HInstance<b>,</b><font color="blue">"Bitmap1"</font><b>)</b><b>;</b>



  <font color="navy">// Loading a bitmap into a TImage control</font>

  <font color="navy">// using numeric IDs.</font>

  Image1<b>-></b>Picture<b>-></b>Bitmap<b>-></b>LoadFromResourceID<b>(</b><b>(</b><b>int</b><b>)</b>HInstance<b>,</b>IDB_SPLASH<b>)</b><b>;</b>

</pre>

Note: You can also use API functions to load the bitmaps. Use the API <TT>LoadBitmap</TT>

function and assign the result to the <TT>Handle</TT> property of <TT>TBitmap.</TT>

<BR>

<P>

<B>Icons</B>

</P>

<P>

<TT>TIcon</TT> is the VCL class for working with icons. Unlike <TT>TBitmap</TT>,

<TT>TIcon</TT> does not contain member functions to help you load the icon. You must use the

API functions <TT>LoadIcon</TT> or <TT>LoadImage</TT> to load the icon into a <TT>TIcon</TT>

object. <TT>LoadIcon</TT> is simpler to use, but it can only load 32 x 32 icons. If you use

<TT>LoadIcon</TT> to load an icon of any other size, the API will enlarge or shrink the icon

image to 32 x 32. <TT>LoadImage</TT> allows you to load icons of any size, but it takes more

arguments. Here are some icon examples.

</P>

<pre>

  <font color="navy">// Load a 32 x 32 icon that uses an integer ID</font>

  TIcon <b>*</b>icon <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TIcon<b>;</b>

  icon<b>-></b>Handle<b>=</b>LoadIcon<b>(</b>HInstance<b>,</b>MAKEINTRESOURCE<b>(</b>ID_FOLDER_ICON<b>)</b><b>)</b><b>;</b>



  <font color="navy">// Load a 32 x 32 icon that uses a string ID</font>

  TIcon <b>*</b>icon <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TIcon<b>;</b>

  icon<b>-></b>Handle<b>=</b>LoadIcon<b>(</b>HInstance<b>,</b><font color="blue">"IconFolder"</font><b>)</b><b>;</b>



  <font color="navy">// load a system icon</font>

  TIcon <b>*</b>icon <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TIcon<b>;</b>

  icon<b>-></b>Handle<b>=</b>LoadIcon<b>(</b>HInstance<b>,</b>IDI_EXCLAMATION<b>)</b><b>;</b>



  <font color="navy">// Load a 16 x 16 icon that uses an integer ID</font>

  TIcon <b>*</b>icon <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TIcon<b>;</b>

  icon<b>-></b>Handle<b>=</b>LoadImage<b>(</b>HInstance<b>,</b>

                         MAKEINTRESOURCE<b>(</b>ID_SMALL_FOLDER<b>)</b><b>,</b>

                         IMAGE_ICON<b>,</b>

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

                         LR_LOADREALSIZE<b>)</b><b>;</b>



  <font color="navy">// Load a 16 x 16 icon that uses a string ID</font>

  TIcon <b>*</b>icon <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TIcon<b>;</b>

  icon<b>-></b>Handle<b>=</b>LoadImage<b>(</b>HInstance<b>,</b>

                         <font color="blue">"SmallFolder"</font><b>)</b><b>,</b>

                         IMAGE_ICON<b>,</b>

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

                         LR_LOADREALSIZE<b>)</b><b>;</b>



  <font color="navy">//Load an icon directly into a TImage control</font>

  Image1<b>-></b>Picture<b>-></b>Icon<b>-></b>Handle <b>=</b> LoadIcon<b>(</b>HInstance<b>,</b>IDI_EXCLAMATION<b>)</b><b>;</b>

</pre>





</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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