
'====================================================================
'' Owner-draw button demo, modal dialog as main.
''
'' Adapted from an example in the Microsoft PSDK (that does
'' not appear to be available on MSDN) using bitmaps from:
'' examples\GL\NeHe\data\
'====================================================================

#include "dialogs.bas"

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

Function DialogProc( Byval hDlg As  HWND, _
                     Byval uMsg As UINT, _
                     Byval wParam As WPARAM, _
                     Byval lParam As LPARAM ) As Integer

  Static As HBITMAP hBmp1, hBmp2
  Static As HDC hdcMem
  Static As LPDRAWITEMSTRUCT lpdis

  Select Case uMsg

    Case WM_INITDIALOG

      hBmp1 = LoadImage( GetModuleHandle( null ), _
                         "mask1.bmp", _
                         IMAGE_BITMAP , _
                         0, _
                         0, _
                         LR_LOADFROMFILE )

      hBmp2 = LoadImage( GetModuleHandle( null ), _
                         "mask2.bmp", _
                         IMAGE_BITMAP , _
                         0, _
                         0, _
                         LR_LOADFROMFILE )

      Return true

    Case WM_DRAWITEM

      lpdis = cast(LPDRAWITEMSTRUCT,lParam)

      hdcMem = CreateCompatibleDC( lpdis->hDC )

      If (lpdis->itemState And ODS_SELECTED) Then
        SelectObject( hdcMem, hBmp2 )
      Else
        SelectObject( hdcMem, hBmp1)
      End If

      StretchBlt( lpdis->hDC, _
                  lpdis->rcItem.left, _
                  lpdis->rcItem.top, _
                  lpdis->rcItem.right - lpdis->rcItem.left, _
                  lpdis->rcItem.bottom - lpdis->rcItem.top, _
                  hdcMem, _
                  0, _
                  0, _
                  128, _
                  128, _
                  SRCCOPY )

      '' Return hDC member of DRAWITEMSTRUCT to default state.
      '' This is ESSENTIAL for proper button operation.

      DeleteDC( hdcMem )

      Return true

    Case WM_COMMAND

      If loword(wParam) = IDCANCEL Then

        '' This will allow the user to close the
        '' dialog by pressing the Escape key.

        EndDialog( hDlg, null )

      End If

      If hiword(wParam) = BN_CLICKED Then

        Select Case loword(wParam)

          Case 100

            'MessageBox( hDlg, "Click...", "", 0 )

        End Select

        Return true

      End If

    Case WM_CLOSE

      DeleteObject( hBmp1 )
      DeleteObject( hBmp2 )

      EndDialog( hDlg, null )

  End Select

  Return 0

End Function

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

Dim As LPDLGTEMPLATE lpdt

Dialog( 1, 0, 0, 100, 75, "Owner Draw Button Demo", lpdt, _
        WS_OVERLAPPED Or WS_SYSMENU Or DS_CENTER )

Control( 100, 35, 20, 20, 20, "", 0, "BUTTON", BS_OWNERDRAW )

CreateModalDialog( 0, @DialogProc, 0, lpdt )

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