  
'====================================================================
' Simple modal dialog as main.
'====================================================================
$ filename "ImgButtonX.exe"
'use rtl32
use rtl64

'% review
use corewin
uses dialogs
'namespace

% DS_CENTER=0x0800
% LR_LOADFROMFILE=0x0010
% IMAGE_BITMAP=0
% ICON_BIG=1
% WM_SETICON=0x80

' declarations of functions
! GetDlgItem lib "user32.dll" (sys hDlg, int nIDDlgItem) as sys
! MapDialogRect lib "user32.dll" (sys hDlg, lpRect) as bool
! 
! WindowFromDC lib "user32.dll" (sys hDc) as sys
! CreateSolidBrush lib "GDi32.dll" (byte COLORREF crColor) 


'  id for buttons and dialog
#define IDD_DLG1 1000
#define IDC_BTN1 1001
#define IDC_BTN2 1002

'  bitmap and icon ids as given in the resource  or rc file 
% IDI_APPICON=500
% IDB_BITMAP1 =501
% IDB_BITMAP2 =502




'===================================================
'  Call back function
function DialogProc( sys hDlg, uint uMsg, sys wParam, lParam ) as int callback

  float pixelX, pixelY
 float pixelX2, pixelY2
  dim  hbmp1, hbmp2 as sys
 
 ' handle for the button
  sys hButton1=GetDlgItem(hDlg, IDC_BTN1)
  sys hButton2=GetDlgItem(hDlg, IDC_BTN2)
       
  select case uMsg
 
    case WM_INITDIALOG
       sys AppIcon = LoadIcon(GetModuleHandle(NULL), IDI_APPICON)
       'Set Icon to Main Window
       SendMessage(hDlg, WM_SETICON, ICON_BIG, AppIcon)
      SetDialogBkColor(  RGB(192, 192, 192), RGB(0,0,0))



  '    obtain the size of the button 1 in pixel
       RECT rc = {0, 0, 2, 2}
       MapDialogRect (hDlg, @rc)
       pixelX = rc.right /6
       pixelY = rc.bottom /6
         
    '  size of button 1 in screen pixels
       int width1=54*pixelX
       int height1 =60*pixelY

                 
'       hBmp1=LoadImage(hDlg,"Print_25.bmp",IMAGE_BITMAP, width1,height1, LR_LOADFROMFILE )
       hBmp1 = LoadImage(GetModuleHandle(BYVAL  NULL), IDB_BITMAP1, IMAGE_BITMAP, width1,height1,0)
       SendMessage (hButton1, BM_SETIMAGE, IMAGE_BITMAP, hBmp1)


  '    obtain the size of the button 2 in pixel
       RECT rc2 = {0, 0, 2, 2}
       MapDialogRect (hDlg, @rc2)
       pixelX2 = rc2.right /6
       pixelY2 = rc2.bottom /6

   '    This is the required size of the small button corresponding to PB imagebuttonX
    '  size of button 2 in screen pixels  
       int width2=36*pixelX2
       int height2 =30*pixelY2

'       hBmp2=LoadImage(hDlg,"Mglass_B.bmp",IMAGE_BITMAP, width2,height2, LR_LOADFROMFILE )
       hBmp2 = LoadImage(GetModuleHandle(BYVAL  NULL), IDB_BITMAP2, IMAGE_BITMAP, width2,height2,0)
       SendMessage (hButton2, BM_SETIMAGE, IMAGE_BITMAP, hBmp2)


       return true

 
    case WM_COMMAND
      select case loword(wParam)
     
        case IDC_BTN1    
          '  Button1 is click
           mbox "Pushed!"
           
       case IDC_BTN2    
          '  Button2  is click
             mbox " Button 2 clicked"


        case IDCANCEL
           EndDialog( hDlg, null )
      end select
     
    case WM_CLOSE
      EndDialog( hDlg, null )
               
  end select

  return 0
end function


'==========================================
' draws with color gradient
SUB DrawGradient (BYVAL hDC AS DWORD)
   LOCAL rectFill AS RECT, rectClient AS RECT, fStep AS SINGLE, hBrush AS DWORD, lOnBand AS LONG
   GetClientRect WindowFromDC(hDC), rectClient
   fStep = rectClient.bottom / 200

   FOR lOnBand = 0 TO 199
      SetRect rectFill, 0, lOnBand * fStep, rectClient.right + 1, (lOnBand + 1) * fStep
      ' paint the background -- change the first 2 colors R and G
      ' to vary the color gradient
      hBrush = CreateSolidBrush(rgb(228, 250, 238 - lOnBand))
      Fillrect hDC, rectFill, hBrush
      DeleteObject hBrush
   NEXT

END SUB





'========================================
'    Display the Main dialog
sub Dispmain()

  sys lpdt
 
  'provide memory for DLGTEMPLATE structure etc   
  dyn::init(lpdt) '1024

 ' display the main dialog
  Dialog( 2,  10,10,230,220, "ImgButton Test", lpdt,
          WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU or DS_CENTER or DS_SETFONT,
          8,"MS Sans Serif" )



'  add in the buttons  
  CONTROL "IDC_BTN", IDC_BTN1,"Button", _
  WS_CHILDWINDOW or WS_VISIBLE  or WS_TABSTOP or BS_LEFT or BS_PUSHBUTTON  or WS_Border, _
  82,182,25,25

'  This is the correct size of the small button coresponding to PB Imagebuttonx
  CONTROL "IDC_BTN", IDC_BTN2,"Button", _
  WS_CHILDWINDOW or WS_VISIBLE  or WS_TABSTOP or BS_LEFT or   BS_PUSHBUTTON or WS_Border, _
  182,189,18,15
             

  CreateModalDialog( null, @DialogProc, 0, lpdt )

end sub



'======================
'  Program starts here
Dispmain()

