虽然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
位图 资源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 As Long,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,bmpInfo.bmWidth,bmpInfo.bmHeight,hdcMemory,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