
'====================================================================
'' 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 )

'====================================================================
 