Autoit3 Library – ResourcesEx
Informatie (ENG)
So if you haven’t noticed  Zedna’s Resources UDF has been a very popular UDF over the last 7 years and was last updated in 2011, but now is the time to bring the UDF up to date with the current AutoIt language syntax (v3.3.14.2). So with the blessing of Zedna may I present to you ResourcesEx. It’s built using the UDF standards I have tried to enforce around here, with improved documentation and an overall UDF structure.
I please ask that you try and break the UDF, criticise, educate, whatever, just play around with the functions and example. I do ask that you just don’t say “you’ve missed the point” and leave at that. Provide an alternative or constructive reason as to why I have “missed the point”.
Voorbeeld scripts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 ; Binary/text resources. #AutoIt3Wrapper_Res_File_Add=binary_1.dat, RT_RCDATA, BIN_1, 0 #AutoIt3Wrapper_Res_File_Add=text_1.txt, RT_RCDATA, TXT_1, 0 ; Cursor resources. #AutoIt3Wrapper_Res_File_Add=%AUTOITDIR%\Examples\Helpfile\Extras\horse.ani, RT_ANICURSOR, ANI_CUR_1 ; Font resources. #AutoIt3Wrapper_Res_File_Add=%AUTOITDIR%\Examples\Helpfile\Extras\SF Square Head Bold.ttf, RT_FONT, TTF_1, 0 ; Image resources. #AutoIt3Wrapper_Res_File_Add=bmp_1.bmp, RT_BITMAP, BMP_1, 0 #AutoIt3Wrapper_Res_File_Add=gif_1.gif, RT_RCDATA, GIF_1, 0 #AutoIt3Wrapper_Res_File_Add=jpg_1.jpg, RT_RCDATA, JPG_1, 0 #AutoIt3Wrapper_Res_File_Add=png_1.png, RT_RCDATA, PNG_1, 0 #AutoIt3Wrapper_Res_File_Add=png_1.png, RT_RCDATA, PNG_1, 0 #AutoIt3Wrapper_Res_File_Add=%AUTOITDIR%\Examples\GUI\merlin.gif, RT_RCDATA, MERLIN_1, 0 ; Icon resources. #AutoIt3Wrapper_Res_File_Add=%AUTOITDIR%\Aut2Exe\Icons\SETUP01.ico, RT_ICON, ICO_1, 0 #AutoIt3Wrapper_Res_File_Add=%AUTOITDIR%\Examples\Helpfile\Extras\Script.ico, RT_ICON, ICO_2, 0 ; Icon resources added with #AutoIt3Wrapper_Res_Icon_Add and that fail to specify a resource name, start from 201 and up. ; See the AutoIt3Wrapper documentation for more details about this directive. #AutoIt3Wrapper_Res_Icon_Add=%AUTOITDIR%\Examples\Helpfile\Extras\Soccer.ico ; Sound resources. #AutoIt3Wrapper_Res_File_Add=C:\WINDOWS\Media\tada.wav, SOUND, WAV_1, 0 ; String resources. #AutoIt3Wrapper_Res_File_Add=string_1.ini, RT_STRING #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include '..\ResourcesEx.au3' ; Improved by guinness. Global $g_hCursor = 0, $g_hGUI = 0 ; Display only if compiled. If @Compiled Then If IsXP() Then MsgBox($MB_SYSTEMMODAL, '', 'This example has been created for Vista and above. Therefore it''s recommended you change your system or the example. You choose.') Else Example() EndIf Else MsgBox($MB_SYSTEMMODAL, '', 'Please compile the example before running, as the resouces need to be added to the executable.') EndIf Func Example() Local Const $iHeight = 450, $iWidth = 470 $g_hGUI = GUICreate('Resources Example', $iWidth, $iHeight, Default, Default, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX)) Local $iText_1 = GUICtrlCreateLabel('', 5, 300, 195, 60) Local $iDimensions = GUICtrlCreateLabel('', 200, 300, 200, 60) Local $iFont = GUICtrlCreateLabel("Different font Loaded from resources", 5, 380, $iWidth - 10, 20) Local $iPic_1 = GUICtrlCreatePic('', 0, 0, 200, 100) Local $iPic_2 = GUICtrlCreatePic('', 200, 0, 200, 100) Local $iPic_3 = GUICtrlCreatePic('', 0, 100, 200, 100) Local $iPic_4 = GUICtrlCreatePic('', 200, 100, 200, 100) Local $iPic_5 = GUICtrlCreatePic('', 0, 200, 200, 100) ; Set an image to a picture controlid using an external dll. _Resource_SetToCtrlID($iPic_5, '#701', $RT_BITMAP, @SystemDir & '\shell32.dll', True) Local $iIcon_1 = GUICtrlCreateIcon(@AutoItExe, 0, 200, 205, 32, 32) Local $iLoadCursor = GUICtrlCreateButton('Load Cursor', 5, $iHeight - 30, 85, 25) Local $iLoadIcon = GUICtrlCreateButton('Load Icon', 90, $iHeight - 30, 85, 25) Local $iLoadSound = GUICtrlCreateButton('Load Sound', 175, $iHeight - 30, 85, 25) Local $iSaveFile = GUICtrlCreateButton('Save File', 260, $iHeight - 30, 85, 25) Local $iMerlin = GUICtrlCreateButton('', $iWidth - 68, 0, 68, 71) _Resource_SetToCtrlID(-1, 'MERLIN_1') ; Use the last controlid. Local $iBall = GUICtrlCreateButton('Click', $iWidth - 68, 72, 68, 71, $BS_ICON) GUICtrlSetImage($iBall, @AutoItExe, '201') ; Icon resources added with #AutoIt3Wrapper_Res_Icon_Add, can be directly used without the UDF. GUISetState(@SW_SHOW, $g_hGUI) ; Get as a string from the executable's resources. Local $sString = _Resource_GetAsString('TXT_1') GUICtrlSetData($iText_1, $sString & @CRLF & 'Length: ' & @extended) ; Get a bitmap image resource. Local $hHBITMAP = _Resource_GetAsBitmap('BMP_1', $RT_BITMAP) ; Set the bitmap image handle to the $iPic_2 controlid. _Resource_SetBitmapToCtrlID($iPic_1, $hHBITMAP) ; Set a gif image resource to a picture controlid. _Resource_SetToCtrlID($iPic_2, 'GIF_1') ; Set a jpg image resource to a picture controlid. _Resource_SetToCtrlID($iPic_3, 'JPG_1') ; Set a png image resource to a picture controlid. _Resource_SetToCtrlID($iPic_4, 'PNG_1') ; Set a font resource to a label. _Resource_LoadFont('TTF_1') GUICtrlSetFont($iFont, 8, 800, 0, 'SF Square Head', 5) ; Custom function to display the size of a specific resource. GUICtrlSetData($iDimensions, GetImageSize('BMP_1', $RT_BITMAP) & @CRLF & _ GetImageSize('GIF_1') & @CRLF & _ GetImageSize('JPG_1') & @CRLF & _ GetImageSize('PNG_1') & @CRLF) Local $bIsCursor = False, $bIsIcon = False While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $iLoadCursor If $bIsCursor Then ; Remove the cursor. _WinAPI_SetCursor(0) ; Unregister the WM_CURSOR message. GUIRegisterMsg($WM_SETCURSOR, '') ; Destroy the cursor handle. _WinAPI_DestroyCursor($g_hCursor) $g_hCursor = 0 Else ; Retrieve the animated cursor and set to the current instance. $g_hCursor = _Resource_GetAsCursor('ANI_CUR_1', $RT_ANICURSOR) _WinAPI_SetCursor($g_hCursor) ; Register the WM_CURSOR message to reinstate the cursor after is passes over a static control. GUIRegisterMsg($WM_SETCURSOR, WM_SETCURSOR) EndIf $bIsCursor = Not $bIsCursor Case $iLoadIcon If $bIsIcon Then _Resource_SetIconToCtrlID($iIcon_1, 0) ; Clear the icon. Else _Resource_SetToCtrlID($iIcon_1, 'ICO_2', $RT_ICON, Default, True) #cs ; Workaround for re-sizing the icon control. Local $hParent = _WinAPI_GetParent(GUICtrlGetHandle($iIcon_1)) Local $aWinGetPos = WinGetPos($hParent) _WinAPI_MoveWindow($hParent, $aWinGetPos[0], $aWinGetPos[1], $aWinGetPos[2], $aWinGetPos[3] + 1) _WinAPI_MoveWindow($hParent, $aWinGetPos[0], $aWinGetPos[1], $aWinGetPos[2], $aWinGetPos[3] - 1) #ce EndIf $bIsIcon = Not $bIsIcon Case $iLoadSound ; Play a wav file from the executable's resources (sync/async). _Resource_LoadSound('WAV_1') _Resource_LoadSound('WAV_1', $SND_ASYNC) Case $iBall, $iMerlin MsgBox($MB_SYSTEMMODAL, '', 'An example of an icon or image on a button.', 3, $g_hGUI) Case $iSaveFile ; Save binary data stored in the executable's resources. _Resource_SaveToFile(@ScriptDir & '\binary_data_1.dat', 'BIN_1') ShellExecute(@ScriptDir) EndSwitch WEnd ; Destroy all the resource handles. _Resource_DestroyAll() ; Delete the GUI. GUIDelete($g_hGUI) Return True EndFunc ;==>Example Func GetImageSize($sResName, $iResType = $RT_RCDATA) Local $sString = '' Local $hImage = _Resource_GetAsImage($sResName, $iResType) If Not @error Then Local $iSize = @extended Local $iWidth = _GDIPlus_ImageGetWidth($hImage) Local $iHeight = _GDIPlus_ImageGetHeight($hImage) $sString = $sResName & ' is: ' & $iWidth & ' x ' & $iHeight & ' and ' & $iSize & ' byte' & (($iSize = 1) ? '' : 's') & '.' EndIf Return $sString EndFunc ;==>GetImageSize Func IsXP() Return $__WINVER < 0x0600 EndFunc ;==>IsXP Func WM_SETCURSOR($hWnd, $iMsg, $wParam, $lParam) #forceref $iMsg, $wParam, $lParam If $hWnd = $g_hGUI And $g_hCursor Then Return _WinAPI_SetCursor($g_hCursor) EndIf Return $GUI_RUNDEFMSG EndFunc ;==>WM_SETCURSOR |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
Changelog: 2015/09/26 Changed: Comments throughout the UDF, removing trailing dot Fixed: Various cosmetic changes 2015/01/12 Fixed: Example directive using double equals sign Fixed: Delete functions not being cast as a bool value. (Thanks Synix) Fixed: @error and @extended not be passed back in nested functions e.g. _Resource_GetAsRaw() 2014/07/19 Added: _Resource_SetBitmapToCtrlID() formely known as _Resource_SetImageToCtrlID() Added: Note about using #AutoIt3Wrapper_Res_Icon_Add to the example. (Thanks Zedna) Added: Passing a blank string to _Resource_SetToCtrlID() through the $sResNameOrID parameter, will delete the image and previous handle Changed: _Resource_SetImageToCtrlID() now accepts a hBitmap not a HBITMAP object Fixed: _Resource_GetAsBitmap() now works the same way as _ResourceGetAsBitmap() did, by converting a jpg, png etc... to HBITMAP Fixed: Memory management of some functions 2014/07/18 Fixed: Destroying a cursor Fixed: Regression from loading the current of external module. (Thanks UEZ) 2014/07/17 Added: Additonal checks to destroy cursors and icons Added: Checking if the dll or exe filepath has a valid extension Added: Example of using an icon and image on a button control Fixed: Icons and cursors (finally) being re-sized to a control Fixed: Using GUIGetStyle() on a non-AutoIt handle would cause issue with controls Fixed: Variable naming of $sDLL to $sDllOrExePath for improved clarity Removed: Workaround for setting icons to AutoIt controls 2014/07/17 Added: Commented workaround in the example for re-sizing an icon control Added: ResourcesEx_PE.au3 created by PreExpand for all you constant variable haters out there!!! Fixed: Changelog comments and source code comments Fixed: Re-sizing icons when the control was different to the icon's size. (Thanks czardas for the MSDN link and Jon.) Fixed: Re-sizing cursors and icons in general 2014/07/15 Added: Comments about using SOUND for wav files and RT_RCDATA for mp3 files. (Thanks Melba23) Added: Option to relevant functions to re-size the image based on the control's dimensions. (Requested by kinch: http://www.autoitscript.com/forum/topic/51103-resources-udf/?p=1147525) Added: Using _Resource_LoadFont() example. (Thanks UEZ) Changed: Certain example resources to now use those found in %AUTOITDIR%\Examples\Helpfile\Extras Changed: Constants and enums readability. (Thank mLipok) Changed: Internal functions for destroying resources Changed: Removed changes made from the previous version for loading resources multiple times. The design needs to be re-thought Changed: Setting styles of controls using native AutoIt functions Fixed: Destroying control resource images would fail to show if reinstated again Fixed: Documentation comments Fixed: Missing certain users who helped with creating this UDF Fixed: Outdated SciTE files 2014/07/14: Added: _Resource_GetAsCursor(), for the loading of animated cursors and standard cursors which can then be used with _WinAPI_SetCursor() Added: _Resource_GetAsIcon(), for loading icon resource types Added: _Resource_LoadFont(), which retrieves a font resource and adds to the current memory of the associated module Added: _Resource_SetCursorToCtrlID() and _Resource_SetIconToCtrlID() Added: Additional resource types to destroy on exit, including $RT_FONT, $RT_ICON and $RT_MENU Added: Playing Mp3s to _Resource_LoadSound(). (Thanks to UEZ and Melba23 with changes made by me.) Changed: _Resource_GetAsBitmap() returns a HTBITMAP handle without converting from hBitmap to HBITMAP Changed: _Resource_PlaySound() to _Resource_LoadSound() Changed: _Resource_SetBitmapToCtrlID() to _Resource_SetImageToCtrlID() Changed: _SendMessage() to GUICtrlSendMsg() Changed: Example files Changed: Setting $iError in the internal get function Changed: Signature of _Resource_Destroy() Changed: Updated example to reflect major changes to the ResourcesEx UDF Changed: Various UDF tweaks that I didn't document because I simply couldn't keep track of all the playing around I did in the last week Fixed: _Resource_GetAsImage() not returning an error when a bitmap couldn't be found in the resource table Fixed: Retrieving length of a string Fixed: Using the current module instead of zero in _Resource_LoadSound() Fixed: Various comment changes. (Thanks mLipok) Fixed: Loading resources multiple times. This is fixed thanks to using the internal storage array 2014/07/07: Added: _Resource_Destroy() and _Resource_DestroyAll() to destroy a particular resource name or all resources Added: Checking if the resource name of id value is empty Added: Descriptions, though could do with a little tweaking Changed: _Resource_Get() to _Resource_GetAsRaw() Changed: Internal workings of __Resource_Storage() Changed: Re-size the storage array when destroyed or on shutdown Fixed: _Resource_GetAsString() with default encoding of ANSI Fixed: Calltips API referencing Resources.au3 and not ResourcesEx.au3 Removed: _Resource_Shudown() due to the addition of _Resource_Destroy() and _Resource_DestroyAll() 2014/07/06: Added: _Resource_Shudown() to free up those resources which aren't loaded using _WinAPI_LockResource(). UnlockResource is obsolete Added: Support for using $RT_STRING Changed: _Resource_GetAsString() now works correctly for most encodings. (Thanks Jos) Changed: _Resource_GetAsString() will now load as a string if the resource type requested is $RT_STRING 2014/07/04: Added: #Regions. (Thanks mLipok) Added: #Tidy_Parameters=/sort_funcs /reel (Thanks mLipok) Added: All optional params now accept the default keyword Added: Link to this thread. (Thanks mLipok) Added: Main header. (Thanks mLipok) Changed: $f.... >> $b..... (Thanks mLipok) 2014/07/03: Initial release |
[#/autoit3/libraries/resourcesex” ]