1. 程式人生 > >為VB6建立純資源DLL(圖片教程)

為VB6建立純資源DLL(圖片教程)

雖然vb可以把資源存在*.RES的資原始檔裡,一起編譯成exe。但如果要換膚、多語言支援,顯然DLL更方便

如何把圖片、圖示、自定義資源存放在DLL裡面?下面我Step by Step教大家。

VB只能建立Activex DLL,所以用VC++建立純正的DLL,這樣的DLL無需註冊(基於COM的才要註冊)。下面以VC++ 6.0為例(VC++ 2005也差不多)。

1.開啟VC++ 6.0,檔案->新建

再選擇“一個簡單的DLL工程”,完成。

新建一個資源指令碼(*.rc),即檔案->新建...

在resource.rc單擊右鍵,右鍵->屬性,取消“啟動MFC特性”,再右鍵->引入,新增資源

引入後,右鍵相應資源的屬性,重新命名ID為100~32767之間的數值。

工程->設定

修改引數堆疊方式為__stdcall(VB用的是這種),這是一個純資源DLL,沒有函式供VB呼叫沒什麼必要改

注意橙色區域選項要一致,在紅色區域加上/noentry,如果你是新手,我教你就在引數最後面打個空格,再輸入/noentry

這個引數表示DLL沒有入口,本來在純資源DLL就不打算寫函式嘛

最後選擇Release版本編譯,有3種方法(VC++ 2005 有個配置管理器)

(1)在vc右上角選單欄或工具欄的空白處右鍵,選上Build項,使build工具欄可見,在Build工具欄上就可以選擇工程的Release版本。

(2)組建

->批組建,在彈出的對話方塊中選擇編譯哪個版本,或者兩個版本都編譯。

(3)組建->配置,移除Win32 Debug。

組建->組建[myDLL.DLL],在Release資料夾下就生成了myDLL.DLL。PS:debug模式編出來的DLL很肥

2.在VB中新建工程,建立按鈕Command1、Command2。圖片框Picture1

將DLL放在同一個資料夾下,DLL內容如下:

點陣圖                          資源ID:100

自定義資源"WAVE"   資源ID:101

form1.frm程式碼:

Option Explicit

Private Sub Command1_Click()
Picture1.Picture = LoadPicture("") '清空圖片框控制元件
load_pic (100)
End Sub

Private Sub Command2_Click()
load_sound (101)
End Sub

Module1.bas程式碼:

Option Explicit
Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Public Declare Function LoadBitmap Lib "user32" Alias "LoadBitmapA" (ByVal hInstance As Long, ByVal lpBitmapName As Long) As Long
Public Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function FindResource Lib "kernel32" Alias "FindResourceA" (ByVal hInstance As Long, ByVal lpName AsLong, ByVal lpType As String) As Long
Public Declare Function LockResource Lib "kernel32" (ByVal hResData As Long) As Long
Public Declare Function LoadResource Lib "kernel32" (ByVal hInstance As Long, ByVal hResInfo As Long) As Long
Public Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Public Declare Function GetLastError Lib "kernel32" () As Long

'點陣圖資料結構型別
Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
'光柵操作程式碼常量
Public Const SRCCOPY = &HCC0020
'聲音播放狀態常量
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4

'呼叫點陣圖資源子例程
Sub load_pic(picName As Integer)
Dim hDLL As Long
Dim hdcMemory, hLoadedbitmap, hOldBitmap As Long
Dim retVal As Long
Dim bmpInfo As BITMAP
'安裝動態連結庫DEMO.DLL
hDLL = LoadLibrary(App.Path & "/myDLL6.DLL")
'根據資源名從動態連結庫中載入相應的點陣圖資源
hLoadedbitmap = LoadBitmap(hDLL, 1000)
'獲取點陣圖資訊
retVal = GetObject(hLoadedbitmap, Len(bmpInfo), bmpInfo)
'建立一個與圖片框控制元件的裝置描述表相容的記憶體裝置描述表
hdcMemory = CreateCompatibleDC(Form1.Picture1.hdc)
'將點陣圖選入記憶體裝置描述表
hOldBitmap = SelectObject(hdcMemory, hLoadedbitmap)
'將點陣圖從記憶體裝置描述表中拷入圖片框控制元件的裝置描述表中
retVal = BitBlt(Form1.Picture1.hdc, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, hdcMemory, 0, 0, SRCCOPY)
'將原始點陣圖選入記憶體裝置描述表
retVal = SelectObject(hdcMemory, hOldBitmap)
'刪除載入的點陣圖,釋放其佔用的所有系統資源
retVal = DeleteObject(hLoadedbitmap)
'刪除記憶體裝置描述表
retVal = DeleteDC(hdcMemory)
'釋放動態連結庫
FreeLibrary (hDLL)
End Sub
'呼叫聲音資源子例程
Sub load_sound(wavName As Integer)
Dim hDLL As Long
Dim hloadwave As Long
Dim hwaveres As Long
Dim hsound As Long
Dim hrelease As Long
Dim resVal As Long
hDLL = LoadLibrary(App.Path & "/myDLL6.DLL")

'在動態連結庫中查詢資源型別為WAVE,資源名為wavName的資源
hwaveres = FindResource(hDLL, wavName, "WAVE")
'將該資源裝入記憶體
hloadwave = LoadResource(hDLL, hwaveres)
'鎖定該資源
hsound = LockResource(hloadwave)
'以同步方式播放記憶體中的聲音資源
'resVal = sndPlaySound(0, SND_ASYNC)
resVal = sndPlaySound(hsound, 1 + 4)

End Sub