虽然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版本。 @H_404_73@ @H_404_73@ (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()@H_404_73@ Picture1.Picture = LoadPicture("") '清空图片框控件@H_404_73@ load_pic (100)@H_404_73@ End Sub
Private Sub Command2_Click()@H_404_73@ load_sound (101)@H_404_73@ End Sub
Module1.bas代码:
Option Explicit@H_404_73@ Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long@H_404_73@ Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long@H_404_73@ Public Declare Function LoadBitmap Lib "user32" Alias "LoadBitmapA" (ByVal hInstance As Long,ByVal lpBitmapName As Long) As Long@H_404_73@ Public Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long,ByVal nCount As Long,lpObject As Any) As Long@H_404_73@ Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long@H_404_73@ Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long,ByVal hObject As Long) As Long@H_404_73@ 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@H_404_73@ Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long@H_404_73@ Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long@H_404_73@ Public Declare Function FindResource Lib "kernel32" Alias "FindResourceA" (ByVal hInstance As Long,ByVal lpName As Long,ByVal lpType As String) As Long@H_404_73@ Public Declare Function LockResource Lib "kernel32" (ByVal hResData As Long) As Long@H_404_73@ Public Declare Function LoadResource Lib "kernel32" (ByVal hInstance As Long,ByVal hResInfo As Long) As Long@H_404_73@ Public Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long@H_404_73@ 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
'位图数据结构类型@H_404_73@ Type BITMAP@H_404_73@ bmType As Long@H_404_73@ bmWidth As Long@H_404_73@ bmHeight As Long@H_404_73@ bmWidthBytes As Long@H_404_73@ bmPlanes As Integer@H_404_73@ bmBitsPixel As Integer@H_404_73@ bmBits As Long@H_404_73@ End Type@H_404_73@ '光栅操作代码常量@H_404_73@ Public Const SRCCOPY = &HCC0020@H_404_73@ '声音播放状态常量@H_404_73@ Public Const SND_ASYNC = &H1@H_404_73@ Public Const SND_NODEFAULT = &H2@H_404_73@ Public Const SND_MEMORY = &H4
'调用位图资源子例程@H_404_73@ Sub load_pic(picName As Integer)@H_404_73@ Dim hDLL As Long@H_404_73@ Dim hdcMemory,hLoadedbitmap,hOldBitmap As Long@H_404_73@ Dim retVal As Long@H_404_73@ Dim bmpInfo As BITMAP@H_404_73@ '安装动态链接库DEMO.DLL@H_404_73@ hDLL = LoadLibrary(App.Path & "/myDLL6.DLL")@H_404_73@ '根据资源名从动态链接库中加载相应的位图资源@H_404_73@ hLoadedbitmap = LoadBitmap(hDLL,1000)@H_404_73@ '获取位图信息@H_404_73@ retVal = GetObject(hLoadedbitmap,Len(bmpInfo),bmpInfo)@H_404_73@ '创建一个与图片框控件的设备描述表兼容的内存设备描述表@H_404_73@ hdcMemory = CreateCompatibleDC(Form1.Picture1.hdc)@H_404_73@ '将位图选入内存设备描述表@H_404_73@ hOldBitmap = SelectObject(hdcMemory,hLoadedbitmap)@H_404_73@ '将位图从内存设备描述表中拷入图片框控件的设备描述表中@H_404_73@ retVal = BitBlt(Form1.Picture1.hdc,bmpInfo.bmWidth,bmpInfo.bmHeight,hdcMemory,SRCCOPY)@H_404_73@ '将原始位图选入内存设备描述表@H_404_73@ retVal = SelectObject(hdcMemory,hOldBitmap)@H_404_73@ '删除加载的位图,释放其占用的所有系统资源@H_404_73@ retVal = DeleteObject(hLoadedbitmap)@H_404_73@ '删除内存设备描述表@H_404_73@ retVal = DeleteDC(hdcMemory)@H_404_73@ '释放动态链接库@H_404_73@ FreeLibrary (hDLL)@H_404_73@ End Sub@H_404_73@ '调用声音资源子例程@H_404_73@ Sub load_sound(wavName As Integer)@H_404_73@ Dim hDLL As Long@H_404_73@ Dim hloadwave As Long@H_404_73@ Dim hwaveres As Long@H_404_73@ Dim hsound As Long@H_404_73@ Dim hrelease As Long@H_404_73@ Dim resVal As Long@H_404_73@ hDLL = LoadLibrary(App.Path & "/myDLL6.DLL")
'在动态链接库中查找资源类型为WAVE,资源名为wavName的资源@H_404_73@ hwaveres = FindResource(hDLL,wavName,"WAVE")@H_404_73@ '将该资源装入内存@H_404_73@ hloadwave = LoadResource(hDLL,hwaveres)@H_404_73@ '锁定该资源@H_404_73@ hsound = LockResource(hloadwave)@H_404_73@ '以同步方式播放内存中的声音资源@H_404_73@ 'resVal = sndPlaySound(0,SND_ASYNC)@H_404_73@ resVal = sndPlaySound(hsound,1 + 4)
End Sub