确定模块内指定资源类型和名称的位置.
#Include <WinAPIEx.au3>
_WinAPI_FindResource ( $hInstance, $sType, $sName )
$hInstance | 可执行文件中资源的模块句柄. 值为 0, 表示模块句柄关联操作系统用于创建当前进程的图像文件. |
$sType | 资源的类型. 参数可以是字符串或整数类型. |
$sName | 资源的名称. 参数可以是字符串或整数类型. |
成功: | 返回指定资源的信息块句柄. 要获得这个句柄的资源, |
传递这个句柄到 _WinAPI_LoadResource() 函数. | |
失败: | 返回 0,并设置@error标志为非 0 值. |
在MSDN中搜索
#Include <APIConstants.au3>
#include <GUIConstantsEx.au3>
#Include <Memory.au3>
#Include <StaticConstants.au3>
#Include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Global Const $sJpg = @TempDir & '\~Tech.jpg'
Global $Msg, $Button, $hFile, $hFont, $hInstance, $hResource, $hData, $pData, $tData, $hWave, $pWave, $sText, $iSize
; 加载 Resources.dll 到内存
$hInstance = _WinAPI_LoadLibraryEx(@ScriptDir & '\Extras\Resources.dll', $LOAD_LIBRARY_AS_DATAFILE)
If Not $hInstance Then
MsgBox(16, 'Error', @ScriptDir & '\Extras\Resources.dll not found.')
Exit
EndIf
; 从 Resources.dll 库加载 JPEG 资源
$hResource = _WinAPI_FindResource($hInstance, 'JPEG', 1)
$iSize = _WinAPI_SizeOfResource($hInstance, $hResource)
$hData = _WinAPI_LoadResource($hInstance, $hResource)
$pData = _WinAPI_LockResource($hData)
; 保存资源到 .jpg 文件
$hFile = FileOpen($sJpg, 2 + 16)
$tData = DllStructCreate('byte[' & $iSize & ']', $pData)
FileWrite($hFile, DllStructGetData($tData, 1))
FileClose($hFile)
; 从 Resources.dll 库加载字体资源
$hResource = _WinAPI_FindResource($hInstance, $RT_FONT, 'TECHNOVIA_CAPS')
$iSize = _WinAPI_SizeOfResource($hInstance, $hResource)
$hData = _WinAPI_LoadResource($hInstance, $hResource)
$pData = _WinAPI_LockResource($hData)
; 添加内存中的字体到系统
$hFont = _WinAPI_AddFontMemResourceEx($pData, $iSize)
; 从 Resources.dll 库加载 SOUND 资源
$hResource = _WinAPI_FindResource($hInstance, 'SOUND', 1)
$iSize = _WinAPI_SizeOfResource($hInstance, $hResource)
$hData = _WinAPI_LoadResource($hInstance, $hResource)
$pData = _WinAPI_LockResource($hData)
; 复制 WAV 到内存为了以后使用
$hWave = _MemGlobalAlloc($iSize, $GMEM_MOVEABLE)
$pWave = _MemGlobalLock($hWave)
_MemMoveMemory($pData, $pWave, $iSize)
;_MemGlobalUnlock($hWave)
; 从 Resources.dll 库加载字符串资源
$sText = _WinAPI_LoadString($hInstance, 1)
; 从内存中卸载 Resources.dll
_WinAPI_FreeLibrary($hInstance)
; 创建 GUI
GUICreate('MyGUI', 350, 350)
GUICtrlCreatePic($sJpg, 0, 0, 350, 350)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateLabel($sText, 10, 18, 330, 36, $SS_CENTER)
GUICtrlSetFont(-1, 30, -1, -1, 'Technovia Caps')
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetColor(-1, 0xF06000)
$Button = GUICtrlCreateButton('Play Sound', 125, 316, 100, 23)
GUISetState()
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case $GUI_EVENT_CLOSE
ExitLoop
Case $Button
_WinAPI_PlaySound($pWave, BitOR($SND_ASYNC, $SND_MEMORY, $SND_NOWAIT))
EndSwitch
WEnd
; 释放资源
_WinAPI_RemoveFontMemResourceEx($hFont)
FileDelete($sJpg)