Code: Select all
'====================================================================
'' Owner-draw menu with icons demo, fully programmed
'' (no menu resource), modal dialog as main.
'====================================================================
#include "dialogs.bas"
#include once "win/shellapi.bi"
#define MENU_ITEM_ID1 1000
#define MENU_ITEM_ID2 1001
#define MENU_ITEM_ID3 1002
'====================================================================
function InitMenu() as HMENU
dim as HMENU hMenu, hFileMenu
hMenu = CreateMenu
hFileMenu = CreateMenu
AppendMenu( hMenu, MF_POPUP, cast(UINT_PTR,hFileMenu), "&Menu" )
AppendMenu( hFileMenu, MF_STRING, MENU_ITEM_ID1, "Item&1" )
AppendMenu( hFileMenu, MF_STRING, MENU_ITEM_ID2, "Item&2" )
AppendMenu( hFileMenu, MF_SEPARATOR, 0, 0 )
AppendMenu( hFileMenu, MF_STRING, MENU_ITEM_ID3, "Item&3" )
return hMenu
end function
'====================================================================
function DialogProc( byval hDlg as HWND, _
byval uMsg as UINT, _
byval wParam as WPARAM, _
byval lParam as LPARAM ) as integer
static as HMENU hMenu
static as HICON hIcon
dim as RECT rc
dim as MENUITEMINFO mii
dim as MEASUREITEMSTRUCT ptr pmis
dim as DRAWITEMSTRUCT ptr pdis
select case uMsg
case WM_INITDIALOG
hMenu = InitMenu()
SetMenu( hDlg, hMenu )
mii.cbSize = sizeof(MENUITEMINFO)
mii.fMask = MIIM_FTYPE
mii.fType = MFT_OWNERDRAW
SetMenuItemInfo( hMenu, MENU_ITEM_ID1, 0, @mii )
SetMenuItemInfo( hMenu, MENU_ITEM_ID2, 0, @mii )
SetMenuItemInfo( hMenu, MENU_ITEM_ID3, 0, @mii )
'--------------------------------------------------
'' Fblogo.ico is actually 32x32, but since even a
'' 16x16 icon displayed too large it was necessary
'' to scale it with DrawIconEx anyway.
'--------------------------------------------------
hIcon = ExtractIcon( GetModuleHandle(null), "fblogo.ico", 0 )
return true
case WM_MEASUREITEM
'-------------------------------------------------
'' wParam = 0 if this message was sent by a menu.
'-------------------------------------------------
if wParam = 0 then
pmis = cast(MEASUREITEMSTRUCT ptr,lParam)
'-----------------------------------------------------
'' Use an arbitrary value for the width of the menu
'' items and the system-defined value for the height.
'-----------------------------------------------------
pmis->itemWidth = 60
pmis->itemHeight = GetSystemMetrics(SM_CYMENU)
end if
case WM_DRAWITEM
'-------------------------------------------------
'' wParam = 0 if this message was sent by a menu.
'-------------------------------------------------
if wParam = 0 then
pdis = cast(DRAWITEMSTRUCT ptr,lParam)
if pdis->CtlType = ODT_MENU then
'--------------------------------------
'' Position everything relative to the
'' DRAWITEMSTRUCT rcItem member.
'--------------------------------------
select case pdis->itemID
case MENU_ITEM_ID1
DrawIconEx( pdis->hDC, pdis->rcItem.left+4, _
pdis->rcItem.top+2, hIcon, _
16, 16, 0, 0, DI_NORMAL )
pdis->rcItem.left += 30
pdis->rcItem.right += 30
DrawText( pdis->hDC, "Item&1", -1, @pdis->rcItem, _
DT_LEFT or DT_VCENTER or DT_SINGLELINE )
case MENU_ITEM_ID2
DrawIconEx( pdis->hDC, pdis->rcItem.left+4, _
pdis->rcItem.top+2, hIcon, _
16, 16, 0, 0, DI_NORMAL )
pdis->rcItem.left += 30
pdis->rcItem.right += 30
DrawText( pdis->hDC, "Item&2", -1, @pdis->rcItem, _
DT_LEFT or DT_VCENTER or DT_SINGLELINE )
case MENU_ITEM_ID3
DrawIconEx( pdis->hDC, pdis->rcItem.left+4, _
pdis->rcItem.top+2, hIcon, _
16, 16, 0, 0, DI_NORMAL )
pdis->rcItem.left += 30
pdis->rcItem.right += 30
DrawText( pdis->hDC, "Item&3", -1, @pdis->rcItem, _
DT_LEFT or DT_VCENTER or DT_SINGLELINE )
end select
end if
end if
case WM_COMMAND
select case loword(wParam)
case MENU_ITEM_ID1
MessageBox( hDlg, "Item 1", "", 0 )
case MENU_ITEM_ID2
MessageBox( hDlg, "Item 2", "", 0 )
case MENU_ITEM_ID3
MessageBox( hDlg, "Item 3", "", 0 )
case IDCANCEL
EndDialog( hDlg, null )
end select
case WM_CLOSE
EndDialog( hDlg, null )
end select
return 0
end function
'====================================================================
dim as LPDLGTEMPLATE lpdt
Dialog( 1, 0, 0, 100, 75, "Menu Demo", lpdt, _
WS_OVERLAPPEDWINDOW or DS_CENTER or WS_VISIBLE )
'--------------------------------------------------------
'' The dialog must have at least one control or the menu
'' will not activate correctly with Alt + access key.
'--------------------------------------------------------
LText( 100, 0, 0, 0, 0, "" )
CreateModalDialog( 0, @DialogProc, 0, lpdt )
'====================================================================