資源檔(.RES)的應用
來源:cww

資源檔有什麼用處呢?最重要的有兩個地方
1.國際發行:我們將Application中所有的文字從Resource用讀取,那麼,只要更動
            Resource檔的內容,就可以用不同語言的方式來顯示。
2.管理資源:例如說,我們的AP中用了數百張的圖片或聲音,不用Resource檔的話,
            在發行出去的AP中,就必需有數百個圖形、聲音檔,那似乎不太好,使
            用Resource檔,便可以將這些圖形、聲音放進一個.Res檔。

使用RC.exe來Compiler我們所定義的Resource檔, RC.EXE在vb5.0光碟中的\TOOLS\RESOURCE

Resources 可分成兩大部份:

- String resources (text strings such as “Hello World”).
- Binary resources (icons, bitmaps, cursors, sounds, video等)

String Resources

語法:

STRINGTABLE [load-option] [mem-option]
BEGIN
stringID string
.
    .
    .
END

參數說明 :
- load-option 可以是以下兩種

Option Description
—— ———–
PRELOAD Resource is loaded immediately.
LOADONCALL (Default) Resource is loaded when called.
Option Description
—— ———–
FIXED Resource remains at a fixed memory location.
MOVEABLE Resource can be moved if necessary in order to compact memory.
DISCARDABLE Resource can be discarded if no longer needed.
Option Description
—— ———–
BITMAP Defines a bitmap (.BMP)
CURSOR Defines a cursor (.CUR)
ICON Defines an icon (.ICO)
SOUND Defines a wave file (.WAV)
VIDEO Defines a video file (.AVI)
Option Description
—— ———–
PRELOAD Resource is loaded immediately.
LOADONCALL (Default) Resource is loaded when called.
Option Description
—— ———–
FIXED Resource remains at a fixed memory location.
MOVEABLE Resource can be moved if necessary in order to compact memory.
DISCARDABLE Resource can be discarded if no longer needed.
  default for binary resources is MOVEABLE.  - filename  資源所在的檔名  
  Compiler的語法:  rc /r [options] SourceFile(.RC)   - /r		只Compiler .RC file , not linked to any executable.  可用rc /? 來查語法  EXAMPLE  RC /r /fo TEST32.RES TEST.RC

上面的表格中是讀取Test.RC的定義,而產生TEST32.RES,這個檔便是我們程式設計中所需的資源檔, 而在vb5.0中如何來使用呢,在 "專案功能表 的 新增檔案"中來選取該Resource file(.RES) , 之後在專案總管中,會出現

- ---Project1    +-- 表單    --- 相關文件      |        ------TEST32.RES

那便可以使用 LoadResString LoadResPicture LoadResData來抓取相關的資料了 以下是Test.RC的內容,而BitMap, Icon寫的檔名,請自行更改成您對映的檔案

#define IDS_HELLO    1 #define IDS_GOODBYE  2  STRINGTABLE BEGIN     IDS_HELLO,   "Hello"     IDS_GOODBYE, "Goodbye"     3,           "This is a Test" END STRINGTABLE BEGIN     101,        "您好"     102,        "再見了"     103,        "這是一個測試" END  ///////////////////////////////////////////////// // Bitmap  //////////////////////////////////////////////// 1          BITMAP "CLI.BMP" 101        BITMAP "CLI2.BMP" BITMAP3    BITMAP "多多.BMP"  //////////////////////////////////////////////// // ICON /////////////////////////////////////////////// 1          ICON   CLIENT.ICO

以下是在form中,需3個Command Button 3個Label 1個PictureBox 另需在 專案功能表 中選 新增檔案 並進而選取Test32.Res

Option Explicit  Private Sub Command1_Click() Call ShowRtn(0) End Sub  Private Sub ShowRtn(ByVal i As Long) Label1.Caption = LoadResString(i + 1) Label2.Caption = LoadResString(i + 2) Label3.Caption = LoadResString(i + 3) Set Picture1 = LoadResPicture(i + 1, vbResBitmap) End Sub  Private Sub Command2_Click() Call ShowRtn(100) End Sub  Private Sub Command3_Click() Set Picture1 = LoadResPicture("BITMAP3", vbResBitmap) End Sub