• Welcome to Theos PowerBasic Museum 2017.

EnumResourceNames - Getting Icon Names

Started by Gary Beene, December 13, 2009, 03:33:42 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Gary Beene

I tried out the EnumResourceNames code at
http://www.jose.it-berater.org/smfforum/index.php?topic=741.0.

From this example of RC file info (there are more lines, using the same format):

Quote1000      icon    "pepsi.ico"
texas     icon    "texas.ico"
cowgirl   bitmap  "cowgirl.bmp"

I can get EnumResourceNames to return bitmap names like "cowgirl".

But when I try it with RT_ICON resource type, all of the icon resource names come out as #1, #2, #3 - instead of "texas" or some other alpha name.

Can anyone explain why?

Here's the PowerBASIC code.

'Compilable Example:
#Compile Exe
#Dim All
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"

Global hDlg as DWord, rNames$, rType&
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   rType& = %RT_Bitmap
   Control Add Button, hDlg, 100,"Get Resource Names", 20,10,160,20
   Control Add Option, hDlg, 200,"Bitmap", 30,40,80,20
   Control Add Option, hDlg, 300,"Icon", 110,40,80,20
   Control Set Option hDlg, 200,200,300
   Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
   Select Case CB.Msg
      Case %WM_Command
         Select Case CB.Ctl
            Case 100
               EnumResourceNames GetModuleHandle(""), ByVal rType&, CodePTR(EnumResNameProc), 0
               MsgBox rNames$
            Case 200 : rType& = %RT_Bitmap
            Case 300 : rType& = %RT_Icon
         End Select
   End Select
End Function

Function EnumResNameProc (BYVAL hModule AS DWord, BYVAL lpszType AS ASCIIZ PTR, _
                          BYVAL lpszName AS ASCIIZ PTR, BYVAL lParam AS LONG) AS LONG
   Local strType AS String, strName AS String
   If Is_IntResource(lpszName) Then
      strName = "#" + Format$(lpszName)
   Else
      strName = @lpszName
   End If
   rNames$ = rNames$ + $crlf + strName
   Function = %TRUE
End Function


I've not posted here before,


José Roca

 
Use %RT_GROUP_ICON instead of %RT_ICON. Otherwise, it will return an index instead of the identifier (a number or a name).

Gary Beene

#2
Thanks Jose!  That works great.

May I also ask if you know how to determine the size of a resource icon/bitmap?  (wxh)

Well, actually, all I need to know is how to get w,h for an icon.

For a bitmap, this simple code does the trick:

       Local hBMP As Dword, w As Long, h As Long
      Graphic Bitmap Load "cowgirl", 0, 0 To hBMP
      Graphic Attach hBMP, 0
      Graphic Get Client To w,h


It takes advantage of Graphic Bitmap Load, which loads a bitmap at it's normal size when zero is used for w/h.

Unfortunately, that works only for bitmaps.  PowerBASIC only supports loading an icon via an imagelist, and imagelists require that the w,h values be specified.  It would have been great if there were a similar DDT method of loading an icon at normal size.



José Roca

#4
 
Hi Gary,

I use GDI+:


hStatus = GdipCreateBitmapFromResource(hInstance, STRPTR(strResourceName), pImage)
hStatus = GdipGetImageWidth(pImage, dwImageWidth)
hStatus = GdipGetImageHeight(pImage, dwImageHeight)


The resource name must be in unicode format. Alternatively, this parameter can consist of the resource identifier in the low-order word and zero in the high-order word.

José Roca


Jürgen Huhn

#6
Maybe this can also be Helpful

and important  is, if the high-order word of the lpName or lpType parameter is zero, the
low-order word specifies the integer identifier of the name or type of the given resource.

Otherwise, those parameters are long pointers to null-terminated strings. If the first character of the string is a pound sign (#), the remaining characters represent a decimal number that specifies the integer identifier of the resource's name or type. For example, the string "#258" represents the integer identifier 258.

An application should reduce the amount of memory required for the resources by referring to them by integer identifier instead of by name.

The WinApi provides a few usefull functions:
An application can use FindResource to find any type of resource, but this function should be used only if the application must access the binary resource data when making subsequent calls to LoadLibrary and LockResource.
To use a resource immediately, an application should use one of the following resource-specific functions to find and load the resources in one call:

Function           Action
FormatMessage           Loads and formats a message-table entry.
LoadAccelerators   Loads an accelerator table.
LoadBitmap           Loads a bitmap resource.
LoadCursor           Loads a cursor resource.
LoadIcon           Loads an icon resource.
LoadMenu           Loads a menu resource.
LoadString           Loads a string-table entry.


For example, an application can use the LoadIcon function to load an icon for display on the screen. However, the application should use FindResource and LoadResource if it is loading the icon to copy its data to another application.

In some cases a handle to the resource's info block is needed first to obtain a handle to the resource identify.
The resource's info block handle must be passed to the LoadResource function to obtain a handle to the resource identify.

for example, The MSGBOXPARAMS member "lpszIcon" to set any Icon to a Messagebox...


FUNCTION JH_GetMsgBox(BYVAL hwnd AS DWORD,BYVAL szMessage AS ASCIIZ * %MAX_PATH, BYVAL dwStyle AS DWORD) AS DWORD
   LOCAL dwLangID, AS DWORD, hIcon AS LONG
   STATIC iname, TpCount AS LONG
   LOCAL  sIcon AS STRING, szCaption, szFileName, szIcon AS ASCIIZ * %MAX_PATH
   LOCAL MSGBOXPARAM AS MSGBOXPARAMS  
   
   dwLangID = MAKELANGID(%LANG_NEUTRAL, %SUBLANG_DEFAULT)
   MSGBOXPARAM.cbSize = SIZEOF(MSGBOXPARAMS)
   MSGBOXPARAM.hwndOwner = %NULL   '        hwnd
   MSGBOXPARAM.hInstance = GetModuleHandle( " ")
   MSGBOXPARAM.lpszText = VARPTR(szMessage)
   MSGBOXPARAM.lpszCaption = VARPTR(szCaption)
   MSGBOXPARAM.dwStyle = dwStyle + %MB_USERICON
   MSGBOXPARAM.lpszIcon =                                          '<<-- %IDI_APPLICATION or user defined
   MSGBOXPARAM.dwContextHelpId = %NULL
   MSGBOXPARAM.lpfnMsgBoxCallback = %NULL
   MSGBOXPARAM.dwLanguageId = dwLangID
  FUNCTION = MessageBoxIndirect(MSGBOXPARAM)

END FUNCTION


I use in the EnumResourceNamesProc also this


  IF (lpszType AND &HFFFF0000) THEN
      strType = @lpszType
      MKstrType = FORMAT$(MAKEINTRESOURCE(VAL(strType)))
      strType = JH_GetResourceTypeString (VAL(strType))
  ELSE
      MKstrType = FORMAT$(MAKEINTRESOURCE(VARPTR(@lpszType)))
      strType = JH_GetResourceTypeString (VARPTR(@lpszType))
  END IF


to get the Strings...


FUNCTION JH_GetResourceTypeString (BYVAL dwType AS DWORD) AS STRING

 SELECT CASE dwType
    CASE %RT_ACCELERATOR  : FUNCTION = "RT_ACCELERATOR - Accelerator table."
    CASE %RT_ANICURSOR    : FUNCTION = "RT_ANICURSOR - Animated cursor."
    CASE %RT_ANIICON      : FUNCTION = "RT_ANIICON - Animated icon."
    CASE %RT_BITMAP       : FUNCTION = "RT_BITMAP - Bitmap resource."
    CASE %RT_CURSOR       : FUNCTION = "RT_CURSOR - Hardware-dependent cursor resource."
    CASE %RT_DIALOG       : FUNCTION = "RT_DIALOG - Dialog box."
    CASE %RT_DLGINCLUDE   : FUNCTION = "RT_DLGINCLUDE Allows a resource editing tool to associate" + SPACE$(10) + $CRLF + _
                                       "a string with an .rc file. Typically, the string is the" + $CRLF + _
                                       "name of the header file that provides symbolic names." + $CRLF + $CRLF + _
                                       "The resource compiler parses the string but otherwise" + $CRLF + _
                                       "ignores the value." + $CRLF + $CRLF + _
                                       "For example," + $CRLF + _
                                       SPACE$(30) + "/* file MyFile.dlg */" + $CRLF + _
                                       SPACE$(25) + " 1 DLGINCLUDE ""MyFile.h"""
    CASE %RT_FONT         : FUNCTION = "RT_FONT - Font resource."
    CASE %RT_FONTDIR      : FUNCTION = "RT_FONTDIR - Font directory resource."
    CASE %RT_GROUP_CURSOR : FUNCTION = "RT_GROUP_CURSOR - Hardware-independent cursor resource."
    CASE %RT_GROUP_ICON   : FUNCTION = "RT_GROUP_ICON - Hardware-independent icon resource."
    CASE %RT_HTML         : FUNCTION = "RT_HTML - HTML."
    CASE %RT_ICON         : FUNCTION = "RT_ICON - Hardware-dependent icon resource."
    CASE %RT_MANIFEST     : FUNCTION = "RT_MANIFEST - Microsoft Windows XP: Side-by-Side Assembly XML Manifest."
    CASE %RT_MENU         : FUNCTION = "RT_MENU - Menu resource."
    CASE %RT_MESSAGETABLE : FUNCTION = "RT_MESSAGETABLE - Message-table entry."
    CASE %RT_PLUGPLAY     : FUNCTION = "RT_PLUGPLAY - Plug and Play resource."
    CASE %RT_RCDATA       : FUNCTION = "RT_RCDATA - Application-defined resource (raw data)."
    CASE %RT_STRING       : FUNCTION = "RT_STRING - String-table entry."
    CASE %RT_VERSION      : FUNCTION = "RT_VERSION - Version resource."
    CASE %RT_VXD          : FUNCTION = "RT_VXD - VXD."
    CASE %RT_VXD          : FUNCTION = "RT_VXD - VXD."
  END SELECT
END FUNCTION


Or is there someone who know`s another solution to set an User defined Icon on a Messagebox??

GDI+?? Please let me know

...
.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨

Jürgen Huhn

#7
I put the Function "JH_GetResourceTypeString" into Jose Roca`s Example
and translated it for PB 9.02 Compiler.

To see the usage of it.

SourceCode is included.

...
.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨

Patrice Terrier

QuoteOr is there someone who know`s another solution to set an User defined Icon on a Messagebox??
Yes, WinLIFT  ;)

--Jürgen

Do you think you could complete the Matrix plugin, to post it when BassBox lurkers reach the number of 100 000 ?

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Jürgen Huhn

#9
Thank you for the Information Patrice!

I didn`t used WinLIFT until now, but i try it next.

The MatrixFall is in work, but I post a first Version from the Project.
I Hope do you like it.
http://www.jose.it-berater.org/smfforum/index.php?topic=2975.msg11396#new
... :)
.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨