Bitmaps

TBitmap contains two functions called LoadFromResourceID and LoadFromResourceName that allow you to load bitmaps from the program's resources. Use LoadFromResourceID when the ID of your bitmap resource is an integer (the RC file example used numeric ID's). Use LoadFromResourceName when the bitmap's ID is a string. If you used the Image Editor to create a .RES file, you will need LoadFromResourceName because the Image Editor saves the resources using string IDs. Here are some code examples.

// Loading a bitmap that uses numeric IDs. Graphics::TBitmap *bmp = new Graphics::TBitmap; bmp->LoadFromResourceID((int)HInstance,IDB_SPLASH); // Loading a bitmap that uses string IDs, like the // ones that the Image Editor creates. Graphics::TBitmap *bmp = new Graphics::TBitmap; bmp->LoadFromResourceName((int)HInstance,"Bitmap1"); // Loading a bitmap into a TImage control // using numeric IDs. Image1->Picture->Bitmap->LoadFromResourceID((int)HInstance,IDB_SPLASH);

Note: You can also use API functions to load the bitmaps. Use the API LoadBitmap function and assign the result to the Handle property of TBitmap.

Icons

TIcon is the VCL class for working with icons. Unlike TBitmap, TIcon does not contain member functions to help you load the icon. You must use the API functions LoadIcon or LoadImage to load the icon into a TIcon object. LoadIcon is simpler to use, but it can only load 32 x 32 icons. If you use LoadIcon to load an icon of any other size, the API will enlarge or shrink the icon image to 32 x 32. LoadImage allows you to load icons of any size, but it takes more arguments. Here are some icon examples.

// Load a 32 x 32 icon that uses an integer ID TIcon *icon = new Graphics::TIcon; icon->Handle=LoadIcon(HInstance,MAKEINTRESOURCE(ID_FOLDER_ICON)); // Load a 32 x 32 icon that uses a string ID TIcon *icon = new Graphics::TIcon; icon->Handle=LoadIcon(HInstance,"IconFolder"); // load a system icon TIcon *icon = new Graphics::TIcon; icon->Handle=LoadIcon(HInstance,IDI_EXCLAMATION); // Load a 16 x 16 icon that uses an integer ID TIcon *icon = new Graphics::TIcon; icon->Handle=LoadImage(HInstance, MAKEINTRESOURCE(ID_SMALL_FOLDER), IMAGE_ICON, 16,16, LR_LOADREALSIZE); // Load a 16 x 16 icon that uses a string ID TIcon *icon = new Graphics::TIcon; icon->Handle=LoadImage(HInstance, "SmallFolder"), IMAGE_ICON, 16,16, LR_LOADREALSIZE); //Load an icon directly into a TImage control Image1->Picture->Icon->Handle = LoadIcon(HInstance,IDI_EXCLAMATION);
Q: Change the mouse cursor. Answer: Most VCL controls have a Cursor property that allows you to change the mouse cursor for that control's air space. This works OK when you need to change the cursor for one control, but it falls short when you need to set the cursor for the entire screen. For example, if you set the cursor property of a form, the cursor will change back when it passes over a control in the form. To change the cursor for the entire screen, use the Cursor property of the global screen property. The list below illustrates the stock values that you can assign to Screen->Cursor.
  • crDefault
  • crNone
  • crArrow
  • crCross
  • crIBeam
  • crSize
  • crSizeNESW
  • crSizeNS
  • crSizeNWSE
  • crSizeWE
  • crUpArrow
  • crHourGlass
  • crDrag
  • crNoDrop
  • crHSplit
  • crVSplit
  • crMultiDrag
  • crSQLWait
  • crNo
  • crAppStart
  • crHelp
The steps below describe how to assign a custom cursor, instead of using the stock cursors. Step 1: Use the image editor to create a RES file that contains your custom cursor. Add the RES file to the project. Step 2: The available screen cursors, include the stock cursors listed above, are all contained in the Cursors array property of TScreen. You must load your cursor and add it to this array before you can use it. Declare a constant that will serve as your cursor identifer and array index. Then load in the cursor using the declared index value. const TCursor crMyCustomCursor=1; // ID for custom cursor __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner){ // load the cursor into the array. // "CURSOR1" is the string identifer set using the Image Editor Screen->Cursors[crMyCustomCursor]=LoadCursor((void*) HInstance,"CURSOR1");} Step 3: Assign the new cursor the to Screen->Cursor property whenever you want it to be the mouse cursor. Screen->Cursor = crMyCustomCursor;
 

Q: Load and play wave sound from a program's resources

Answer:

The FAQ titled "adding icons, cursors, and bitmaps to a BCB project" contained an example of how to use RC files in a BCB project. If you look closely, you will see that the RC file adds a WAVE resource to the project. The WAVE resource gets bound to the executable, just like a bitmap or an icon. This FAQ demonstrates how you can play the wave resource at runtime.

The API provides a function called PlaySound that allows you to play a wave soundbite. PlaySound does not provide any visual feedback that the audio is playing, it simply plays the sound, and that's it. This makes PlaySound a good choice for playing short wave resources that you associate with an action in your program. The code example below uses PlaySound to play a typewriter sound whenever the user types in a TMemo control (The WAV file just happens to be one that I just happened to have laying around on my PC. If you don't have TYPE.WAV, use the file TICK.WAV that comes with BCB's football example)

Step 1:

Add the wave resource to your project. The FAQ on resources covers this in detail. The code below summarizes the resource statements that you should use to compile this example. Create an RC and an RH file that contain these statements, and add the RC file to your BCB project.

// insert this line in an RC file IDW_TYPE WAVE "type.wav" // insert this line in an RH file #define IDW_TYPE 1000

Step 2:

Use the PlaySound API function to play the wave resource (to use the code below, place a TMemo on a form, and create an OnKeyPress handler).

#include <mmsystem.h>#include "res.rh" void __fastcall TForm1::Memo1KeyPress(TObject *Sender, char &Key){ PlaySound(MAKEINTRESOURCE(IDW_TYPE), HInstance, SND_RESOURCE | SND_ASYNC );}

Note: SND_RESOURCE tells Windows that the first parameter to PlaySound is the resource name of a wave resource that is bound to the executable. Change this parameter to SND_FILENAME if you need to load a wave file. When loading files, the first argument to PlaySound is a char * the contains the filename. You can also play system sounds by using the SND_ALIAS. Here are some examples of SND_FILENAME and SND_ALIAS.

PlaySound("type.wav", NULL, SND_FILENAME | SND_ASYNC |SND_NOSTOP);PlaySound("SystemStart",NULL, SND_ALIAS | SND_ASYNC |SND_NOSTOP);

For a list of available SND_ALIAS associated sounds, look in the registry under HKEY_CURRENT_USER\AppEvents\Schemes

Note: The SND_ASYNC flag causes PlaySound to return before the sound has finished playing. Specify the SND_SYNC if you prefer that the wave sound finish before the returns.

Note: The SND_NOSTOP flag prevents the sound from being interrupted from another sound request. To see the flag in action, test the typewrite sound with and without the flag.

Note: There are other flags that you can pass to PlaySound. For more details, consult the Microsoft Multimedia help file that comes with C++Builder.

Наши рекомендации