• 資源檔(.RES)的應用

資源檔有什麼用處呢?最重要的有兩個地方
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.

- mem-option 可以是以下三種

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

- stringID  自行定義的integer,用來定義字串 resource.

- string    我們定義的字串,字串前後要用雙引號(")將之包圍起來,字串長度不可
            超過255 bytes,而且字串要在同一行


BINARY RESOURCES
語法:

nameID keyword [load-option] [mem-option] filename

參數:

- nameID 定義一個於以下keyword類別中,一個唯一的名稱或數字,即,有三個BITMAP
         類別的Resource,其nameID可以分別為1,2,3不重覆,而另有三個ICON的資源
         ,其nameID亦可分別為1,2,3,不會和BITMAP的1,2,3相衝突。註:ICON類別
         的nameID不可以為0,0保留給 Visual Basic icon。nameID亦可以為字串

- keyword 定義資源類別

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)

- load-option

Option

Description

PRELOAD

Resource is loaded immediately.

LOADONCALL

(Default) Resource is loaded when called.

- mem-option

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
  • 文件出處

  cww

VB心得筆記歡迎各位的指教,如果您有任何文章或資料願意提供給我們的,請來信到VBNote

如果對本站有任何建議,歡迎來信給Honey,我們會盡快給您答覆