將位圖、圖示、游標、聲音等資源編譯進 EXE,可免去外部檔案部署的麻煩。C++Builder 使用標準 .rc 文字定義檔配合 BRCC32 編譯器實現。
資源名 類型 檔案路徑,例如:
DemHint WAVE D:\Res\DemHint.wav
PrintType BITMAP D:\Res\PrintType.bmp
ZoomIn CURSOR D:\Res\ZoomIn.cur
BRCC32 zpg.rc 產生 zpg.res。.cpp 加入 USERES("Zpg.res");,或單元檔頂部加 #pragma resource "zpg.res"。#pragma package(smart_init)。| 資源類型 | 調用方式 |
|---|---|
| 位圖 | Graphics::TBitmap *bmp = new Graphics::TBitmap; bmp->LoadFromResourceName((unsigned)HInstance, "PrintType"); |
| 聲音 | PlaySound("DemHint", HInstance, SND_RESOURCE); |
| 游標 | Screen->Cursors[crZoomIn] = LoadCursor(HInstance, "ZoomIn"); PaintBox->Cursor = (TCursor)crZoomIn; Application->ProcessMessages(); |
關鍵提醒: 自訂游標常數建議使用正數(系統預定義為負數),避免衝突。資源編譯進 EXE 後,發佈時無需攜帶 .res 或原始素材檔。