Click to See Complete Forum and Search --> : Icon in a menu ??


ankur26
May 10th, 2001, 06:15 AM
i am displaying excel,word files in a menu which the user can click and the respective file will open. how can i display a word icon with a word file and excel icon with excel file in the menu or it could be notepad file or a powerpoint file.

i want to display the associated icon with the file on the menu.

thanks,
Ankur.

Judgey
May 10th, 2001, 06:39 AM
What are you using for your Menu?, Are you building your own in a ListView for example, if so you could use the ImageControl to store your Icons and apply this to be used with the ListView. Then when you add items to the ListView you could use a case statment based on the Extenstion of the file to determine what Image Index is used.

Iouri
May 10th, 2001, 07:02 AM
' API stuff for putting bitmaps in menus.
Private Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wid As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As Long
cch As Long
End Type

Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal bypos As Long, lpcMenuItemInfo As MENUITEMINFO) As Long

Private Const MF_BITMAP = &H4&
Private Const MFT_BITMAP = MF_BITMAP
Private Const MIIM_TYPE = &H10

Private Sub Form_Load()
Dim main_menu As Long
Dim sub_menu As Long
Dim menu_info As MENUITEMINFO
Dim i As Integer

main_menu = GetMenu(hwnd)
sub_menu = GetSubMenu(main_menu, 0)
For i = 0 To 2
With menu_info
.cbSize = Len(menu_info)
.fMask = MIIM_TYPE
.fType = MFT_BITMAP
.dwTypeData = picFace(i).Picture
End With
SetMenuItemInfo sub_menu, i, True, menu_info
Next i
End Sub




Iouri Boutchkine
iouri@hotsheet.com

ankur26
May 10th, 2001, 11:46 PM
thanks

this code displays a bitmap on the menu but the problem is that the caption disappears.

i the following line of code
.cbSize = Len(menu_info) + Len(mnuOpen.Caption) displays only icon
.cbSize = Len(menu_info) + 300 displays only caption

the above code doesn't works if i put an icon in the picture control ?

Also, this answers only a part of my Question how can i display the associated icon of a exe on a menu. say i am showing a .doc file in the menu and i want the word icon to come in the menu ?

thanks,
Ankur.

John G Duffy
May 11th, 2001, 12:48 PM
The Sample listed below will extract the System Icon for an individual File. You will need a Command Button (cmdLoad), a PictureBox (pixSmall) and a commonDialog control (commonDlg1) plus a Module.

'
'
'The Forms Code
'
private Sub cmdLoad_Click()

'working variables...
Dim hImgSmall as Long 'the handle to the system image list
Dim fName as string 'the file name to get icon from
Dim fnFilter as string 'the file name filter
Dim r as Long

'a little error handling to trap a cancel
on Local error GoTo cmdLoadErrorHandler

'get the file from the user
fnFilter$ = "All Files (*.*)|*.*|"
fnFilter$ = fnFilter$ & "Applications (*.exe)|*.exe|"
fnFilter$ = fnFilter$ & "Windows Bitmap (*.bmp)|*.bmp|"
fnFilter$ = fnFilter$ & "Icon Files (*.ico)|*.ico"

commondlg1.CancelError = true
commondlg1.Filter = fnFilter$
commondlg1.ShowOpen

fName$ = commondlg1.FileName

'get the system icon associated with that file
hImgSmall& = SHGetFileInfo(fName$, 0&, _
shinfo, len(shinfo), _
BASIC_SHGFI_FLAGS Or SHGFI_SMALLICON)

'set a picture box to receive the small icon
'its size must be 16x16 pixels (240x240 twips),
'with no 3d or border!
'clear any existing image
pixSmall.Picture = LoadPicture()
pixSmall.AutoRedraw = true

'draw the associated icon into the picturebox
Call ImageList_Draw(hImgSmall&, shinfo.iIcon, _
pixSmall.hDC, 0, 0, ILD_TRANSPARENT)

'realize the image by assigning its image property
'(where the icon was drawn) to the actual picture property
pixSmall.Picture = pixSmall.Image

'Uncomment out the following code to save
'the image to the current path. Note that
'the background colour of the icon saved
'will be the background colour of the
'pixSmall control.
SavePicture pixSmall, "c:\vb things to look at\test.bmp"

Exit Sub

cmdLoadErrorHandler:
Exit Sub

End Sub
'
'
' The Modules Code
'
'
option Explicit


public Const MAX_PATH = 260
public Const SHGFI_DISPLAYNAME = &H200

public Const SHGFI_EXETYPE = &H2000
public Const SHGFI_SYSICONINDEX = &H4000 'system icon index
public Const SHGFI_LARGEICON = &H0 'large icon
public Const SHGFI_SMALLICON = &H1 'small icon
public Const SHGFI_SHELLICONSIZE = &H4
public Const SHGFI_TYPENAME = &H400

public Const ILD_TRANSPARENT = &H1 'display transparent

public Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME _
Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX _
Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE

public Type SHFILEINFO
hIcon as Long
iIcon as Long
dwAttributes as Long
szDisplayName as string * MAX_PATH
szTypeName as string * 80
End Type

public Declare Function SHGetFileInfo Lib _
"shell32.dll" Alias "SHGetFileInfoA" _
(byval pszPath as string, _
byval dwFileAttributes as Long, _
psfi as SHFILEINFO, _
byval cbSizeFileInfo as Long, _
byval uFlags as Long) as Long

public Declare Function ImageList_Draw Lib "comctl32.dll" _
(byval himl as Long, byval i as Long, _
byval hDCDest as Long, byval x as Long, _
byval y as Long, byval flags as Long) as Long

public shinfo as SHFILEINFO





John G