faq52.htm

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

HTM
139
字号


<HTML>

<HEAD>

   <TITLE>Add icon, cursor, bitmap, and sound resources to a BCB project</TITLE>

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

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

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



<TR>

<TD>

<H3>

Add icon, cursor, bitmap, and sound resources to a BCB project

</H3>

<P>

In this situation, there is more than one way to skin a cat. You can either use

the C++Builder Image Editor, or you can use good old fashioned <TT>RC</TT> files (or you

can use a mixture of the two).

</P>

<B>Using the Image Editor</B> (limited to bitmaps, icons, and cursors)

<P>

The Image Editor allows you to create bitmap, icon, and cursor resources. You can

save tham as <TT>BMP</TT>, <TT>ICO</TT>, or <TT>CUR</TT> files respectively. The

Image Editor also allows you to create groups of resources and save them directly

in one <TT>RES</TT> file. The <TT>RES</TT> file is more useful because you can't

add <TT>BMP</TT>, <TT>ICO</TT>, or <TT>CUR</TT> files directly to the project,

but you can add <TT>RES</TT> files.

</P>

<P>

To start, launch the Image Editor. Select the File | New | Resource File (res) menu

option. Save the new resource file in your project directory. Use the Image Editor

to design the cursors, icons, or bitmaps that you need to use in your program. Keep

these resources inside the <TT>RES</TT> file. For example, if you need a new bitmap, select

the Resource | New | Bitmap menu option. This creates a new bitmap right inside the

<TT>RES</TT> file, instead of creating a <TT>BMP</TT> file on your hard drive.

</P>

<P>

When you are done creating and editing your resources, make sure that each one has a

logical name. Then save the <TT>RES</TT> file and go back to the BCB IDE.

</P>

<P>

You can add the <TT>RES</TT> file to your project using one of two different methods. The

first way is to select the Project | Add To Project menu option. This menu opton brings up a

file open dialog box. Locate and select the <TT>RES</TT> file. This inserts the <TT>RES</TT>

file into the Project Manager (you can also add files to the project using the big plus

 button in the Project Manager).

</P>

<P>

The other way to add a <TT>RES</TT> file to the project is to use the <TT>#pragma</TT>

directive. Here is an example:

</P>

<pre>

<font color="green">#pragma resource "PICTURES.res"</font>

</pre>

<B>Using an RC file</B>

<P>

On the surface, the Image Editor might seem easier than using an <TT>RC</TT> resource file,

but if you have to maintain a lot of bitmaps, and if you have to modify them frequently, you

might agree that <TT>RC</TT> files are actually more convenient. <TT>RC</TT> files become a

necessity when you need to add <TT>WAV</TT> files to your project. To use an <TT>RC</TT>

file, follow these steps:

</P>

<P>

<B>Step 1: </B> Use a program to create the cursor, icon, and bitmap files that you need.

There are lots of bitmap picture editors out there, and most are a lot more powerful than

the picture editor in BCB (even Borland agrees, BCB was written with itself, but Borland

has stated that the BCB splash screen was designed with PhotoPaint or PhotoShop). Icons

and Cursors are more difficult because there are fewer editors available. You can use

the Image Editor and save each resource using <TT>.CUR</TT> and <TT>.ICO</TT> extensions

instead putting all of the resources into one<TT>.RES</TT> file. You could also create

the resources with BC++ 5, the Resource Workshop from BC++ 4.5, or the resource editor from

any other compiler, such as MS Dev Studio. However you do it, group all of your <TT>.ICO</TT>, <TT>.BMP</TT>, and

<TT>.CUR</TT> files into your project directory.

</P>

<P>

<B>Step 2: </B> Create the <TT>RC</TT> file. To create the <TT>RC</TT> file, open a new

file using the editor of choice. I prefer to use the Text object in the BCB Object

Repository. Save the new file in your project directory and call it <TT>RESOURCE.RC</TT>. Add

statements to the <TT>RC</TT> file for each resource that you need to add. Follow the

format shown below:

</p>

<PRE>

#include "resource.rh"



ID_SPLASH     BITMAP   "splash.bmp"

ID_ABOUT      BITMAP   "about.bmp"

ID_HAND       CURSOR   "hand.cur"

ID_FOLDER     ICON     "folder.ico"

ID_TYPE       WAVE     "type.wav"

ID_SEARCHAVI  AVI      "search.avi"

</PRE>

<P>

The first column contains the resource IDs. These will be defined in <TT>RESOURCES.RH</TT>

in the next step. The second column describes the type of resource. The last column

is the filename of the resource.

<P>

<B>Step 3: </B> Create the header file for the <TT>RC</TT> file. Once again, create a new

text file using the editor of your choice. Save the file as <TT>RESOURCE.RH</TT> and enter

text that follows this pattern:

</p>

<pre>

<font color="green">#ifndef RESOURCE_RH</font>

<font color="green">#define RESOURCE_RH</font>



<font color="green">#define ID_SPLASH     1000</font>

<font color="green">#define ID_ABOUT      1001</font>

<font color="green">#define ID_HAND       1002</font>

<font color="green">#define ID_FOLDER     1003</font>

<font color="green">#define ID_TYPE       1004</font>

<font color="green">#define ID_SEARCHAVI  1005</font>

<font color="green">#endif</font>

</pre>

<P>

<B>Step 3: </B>Add the <TT>RC</TT> file to your project. When you need to load a resource,

<TT>#include</TT> the resource header file and load the resource based on its ID.

</P>

<P>

<B>Note:</B> The Image Editor is less than perfect when it comes to working with complex

bitmaps that contain custom 256 color palettes. This is another good reason to hand craft

an RC file.

</P>

<P>

<B>Note:</B> This FAQ only shows you how to bind resources to your program. I have covered

how to load and use the resources in the FAQs listed below.

<UL>

<LI><A TARGET=_top HREF="faq24.htm">Change the mouse cursor</A>

<LI><A TARGET=_top HREF="faq53.htm"> Load bitmaps and icons from the programs's resources</A>

<LI><A TARGET=_top HREF="faq54.htm"> Play wave audio from a program's resource using PlaySound</A>

</UL>            



</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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