Type Class_ToolBar
Private : 
   hWndControl As.HWnd 'ؼ
   m_IDC As Long     'ؼIDC
Public : 
   Declare Property hWnd() As.hWnd                    '/ÿؼ
   Declare Property HWnd(ByVal hWndNew As.HWnd)
   Declare Property hWndForm() As.HWnd         'ڶ࿪ʱҪʹÿؼǰָؼĸ
   Declare Property hWndForm(ByVal hWndParent As.HWnd)         'ڶ࿪ʱҪʹÿؼǰָؼĸ
   Declare Property ButtonText(idButton As ULong) As StringW          '/ıidButton ťʶ
   Declare Property ButtonText(idButton As ULong, nText As Wstring)
   Declare Property ButtonTips(idButton As ULong) As StringW          '/ʾıidButton ťʶ
   Declare Property ButtonTips(idButton As ULong, nText As Wstring)
   Declare Property ButtonBitmap(idButton As uLong) As Long          '/бͼĴ㿪ʼidButton ťʶΪI_IMAGENONEָʾðťûͼ
   Declare Property ButtonBitmap(idButton As uLong, iBitmap As Long)  '
   Declare Function ButtonCount() As Long               'ذť
   Declare Property ButtonCheck(idButton As uLong) As Boolean   'ȡѡйеĸť,idButton ťʶ{=.True.False}
   Declare Property ButtonCheck(idButton As uLong, bValue As Boolean)
   Declare Property ButtonEnabled(idButton As uLong) As Boolean                 '/ÿؼǷidButton ťʶ{=.True.False}
   Declare Property ButtonEnabled(idButton As uLong,ByVal bValue As Boolean)
   Declare Property ButtonVisible(idButton As uLong) As Boolean                 'ʾؿؼidButton ťʶ{=.True.False}
   Declare Property ButtonVisible(idButton As uLong, bValue As Boolean)
   Declare Property Enabled() As Boolean                 '/ÿؼǷ{=.True.False}
   Declare Property Enabled(ByVal bValue As Boolean)
   Declare Property Visible() As Boolean                 'ʾؿؼ{=.True.False}
   Declare Property Visible(ByVal bValue As Boolean)
   Declare Property Tag() As StringW                      '洢ĸݡ
   Declare Property Tag(sText As Wstring)
   Declare Function Left() As Long                   'ڸڵ Xأ
   Declare Function Top() As Long                  'ڸڵ Yأ
   Declare Function Width() As Long                'ؿؼȣأ
   Declare Function Height() As Long               'ؿؼ߶ȣأ
   Declare Property IDC() As Long             '/ÿؼIDCؼʶ1ÿؼIDCΨһģؼ顣޸ģϵͳԶ
   Declare Property IDC(NewIDC As Long)
   Declare Sub SetFocus()  ' ʹؼȡ̽
   Declare Sub SetImgSize(imSize As Long )  ' ðťͼߴ磬Ӧťǰá
   Declare Function AddButton(idCommand As Long,ResImg As String ,zText As Wstring, nTips As Wstring,tbStyle As Long ,AutoSize As Long ,ShowText As Long,xChecked As Long, xEnabled As Long,xHidden As Long ,xEllipses As Long,dwData As lParam  =0) As BOOLEAN  '򹤾ӰťɹTRUE򷵻FALSEʽ {4.0 ͨť.1 ѡť.2 ѡť.3 б.4 ָ}
   Declare Property UserData(idx AS LONG) As Integer      '/ûݣidxţΧΪ0991ؼԴ100ֵ
   Declare Property UserData(idx As Long ,bValue As Integer)   
   
   '{벻ʾ}
   Declare Sub UnLoadControl() ' ڱʱҪĹɿؼڲá
End Type


Type Class_ToolBar_Data
   idCommand AS Long     'ťID
   ResImg As zString *50  'Դ
   idxBitmap AS Long      'Դͼб
   nTips As Wstring *100  'ʾ
   
End Type 

Sub Class_ToolBar.UnLoadControl() ' ڱʱҪĹɿؼڲá
   Dim siml As HIMAGELIST = Cast(HIMAGELIST, SendMessageW(hWndControl, TB_GETIMAGELIST, 0, 0)) 
   If siml Then ImageList_Destroy siml
   Dim fp As FormControlsPro_TYPE ptr = vfb_Get_Control_Ptr(hWndControl)
    If fp Then
       fp->nData=""
    End If    
End Sub

Property Class_ToolBar.hWnd() As.hWnd                    '
  Return hWndControl
End Property
Property Class_ToolBar.HWnd(ByVal hWndNew As.HWnd)        '
  hWndControl = hWndNew
End Property
Property Class_ToolBar.IDC() As Long
  Return ID_In_Number(hWndControl)
End Property
Property Class_ToolBar.IDC(NewIDC As Long)  
   m_IDC = NewIDC 
End property
Property Class_ToolBar.hWndForm() As.HWnd         'ڶ࿪ʱҪʹÿؼǰָؼĸ
   If IsWindow(hWndControl) = 0 Then Return 0
   Dim fp As FormControlsPro_TYPE Ptr = vfb_Get_Control_Ptr(hWndControl)
   If fp Then Return fp->hWndParent
   Property = 0
End Property
Property Class_ToolBar.hWndForm(ByVal hWndParent As.HWnd)         'ڶ࿪ʱҪʹÿؼǰָؼĸ
   Dim fp   As FormControlsPro_TYPE Ptr = vfb_Get_Control_Ptr(hWndParent)
   Dim nIDC As Long
   If fp Then
      nIDC = fp->IDC
   End If
   hWndControl = GadgetID(m_IDC + nIDC) 'GetDlgItem(hWndParent, m_IDC + nIDC)  
End Property
Property Class_ToolBar.Tag() As StringW
   Dim fp As FormControlsPro_TYPE ptr = vfb_Get_Control_Ptr(hWndControl)
   If fp Then
      Return fp->Tag
   End If
End Property
Property Class_ToolBar.Tag(sText As WString)
   Dim fp As FormControlsPro_TYPE ptr = vfb_Get_Control_Ptr(hWndControl)
   If fp Then
      fp->Tag = sText
   End If
End Property
Function Class_ToolBar.Left() As Long                '
  Dim rc As Rect
  GetWindowRect hWndControl, @rc          'ôС
  MapWindowPoints HWND_DESKTOP, GetParent(hWndControl), Cast(LPPOINT, @rc), 2
  Return rc.Left
End Function

Function Class_ToolBar.Top() As Long     '
  Dim rc As Rect
  GetWindowRect hWndControl, @rc     'ôС
  MapWindowPoints HWND_DESKTOP, GetParent(hWndControl), Cast(LPPOINT, @rc), 2
  Return rc.Top
End Function

Function Class_ToolBar.Height() As Long                  '
  Dim rc As Rect
  GetWindowRect hWndControl, @rc          'ôС
  Return  rc.Bottom - rc.Top
End Function

Function Class_ToolBar.Width() As Long
  Dim rc As Rect
  GetWindowRect hWndControl, @rc          'ôС
  Return rc.Right - rc.Left
End Function

Property Class_ToolBar.Visible() As Boolean                 'ɼ
  Return IsWindowVisible(hWndControl)
End Property
Property Class_ToolBar.Visible(ByVal bValue As Boolean)
  If bValue Then
      ShowWindow(hWndControl, SW_SHOW)
  Else
      ShowWindow(hWndControl, SW_HIDE)
  End If
End Property
Property Class_ToolBar.Enabled() As Boolean                 'ʹ
  Return IsWindowEnabled(hWndControl)
End Property
Property Class_ToolBar.Enabled(ByVal bValue As Boolean)
  EnableWindow(hWndControl, bValue)
End Property
Property Class_ToolBar.ButtonText(idButton As ULong) As StringW                  '/ı
   Dim nLen As Long = SendMessageW( hWndControl , TB_GETBUTTONTEXT , idButton , NULL )   'ĬΪ SendMessageW(hWndControl, idButton)
   If nLen=0 Then Return ""
   ReDim by(nLen * 2 + 2) As Byte 
   SendMessageW(hWndControl, TB_GETBUTTONTEXTW, Cast(wParam, idButton), Cast(lParam, @by(0)))
   Return  *CPtr(Wstring Ptr ,@by(0))
End Property
Property Class_ToolBar.ButtonText(idButton As ULong, nText As WString)
   Dim tb As TBBUTTONINFOW
   tb.cbSize = SizeOf(TBBUTTONINFOW)
   tb.dwMask = TBIF_COMMAND Or TBIF_TEXT
   tb.idCommand = idButton
   tb.pszText = @nText
   tb.cchText = Len(nText) * 2
   SendMessageW(hWndControl, TB_SETBUTTONINFOW, Cast(wParam, idButton), Cast(lParam, @tb))
End Property
Property Class_ToolBar.ButtonTips(idButton As ULong) As StringW                  '/ı
   Dim fp As FormControlsPro_TYPE ptr = vfb_Get_Control_Ptr(hWndControl)
   If fp Then
      Dim u As Long = Len(fp->nData) / SizeOf(Class_ToolBar_Data)
      if u > 0 Then
         Dim i As Long ,td As Class_ToolBar_Data Ptr = Cast(Any Ptr ,StrPtr(fp->nData))
         For i = 0 To u -1
            if td[i].idCommand = idButton Then
               Return td[i].nTips
            End if
         Next
      End if
   End If
End Property
Property Class_ToolBar.ButtonTips(idButton As ULong ,nText As WString)
   Dim fp As FormControlsPro_TYPE ptr = vfb_Get_Control_Ptr(hWndControl)
   If fp Then
      Dim u As Long = Len(fp->nData) / SizeOf(Class_ToolBar_Data)
      if u > 0 Then
         Dim i As Long ,td As Class_ToolBar_Data Ptr = Cast(Any Ptr ,StrPtr(fp->nData))
         For i = 0 To u -1
            if td[i].idCommand = idButton Then
               td[i].nTips = nText
               Exit For
            End if
         Next
      End if
   End If
End Property
Property Class_ToolBar.ButtonBitmap(idButton As ULong) As Long
  Return SendMessageW(hWndControl, TB_GETBITMAP, Cast(wParam, idButton), 0) 
End Property
Property Class_ToolBar.ButtonBitmap(idButton As uLong, iBitmap As Long)
   Dim tb As TBBUTTONINFOW
   tb.cbSize = SizeOf(TBBUTTONINFOW)
   tb.dwMask = TBIF_COMMAND Or TBIF_IMAGE
   tb.idCommand = idButton
   tb.iImage = iBitmap
   SendMessageW(hWndControl, TB_SETBUTTONINFOW, Cast(wParam, idButton), Cast(lParam,  @tb))
End Property
Property Class_ToolBar.ButtonCheck(idButton As uLong) As Boolean
  Return SendMessageW(hWndControl, TB_ISBUTTONCHECKED, Cast(wParam, idButton), 0)  
End Property
Property Class_ToolBar.ButtonCheck(idButton As uLong, bValue As Boolean)
   SendMessageW(hWndControl, TB_CHECKBUTTON, Cast(wParam, idButton), Cast(lParam, MAKELONG(bValue, 0)))
End Property

Function Class_ToolBar.ButtonCount() As Long
   Function=SendMessageW(hWndControl, TB_BUTTONCOUNT, 0, 0)
End Function
Property Class_ToolBar.ButtonEnabled(idButton As uLong) As Boolean 
   Return SendMessageW(hWndControl, TB_ISBUTTONENABLED, Cast(wParam, idButton), 0) 
End Property
Property Class_ToolBar.ButtonEnabled(idButton As uLong,ByVal bValue As Boolean)
   if bValue Then 
      SendMessageW(hWndControl, TB_ENABLEBUTTON, Cast(wParam, idButton), Cast(lParam, MAKELONG(CTRUE, 0)))
   Else
      SendMessageW(hWndControl, TB_ENABLEBUTTON, Cast(wParam, idButton), Cast(lParam, MAKELONG(False, 0)))
   End if 
End Property
Property Class_ToolBar.ButtonVisible(idButton As uLong) As Boolean 
   Return SendMessageW(hWndControl, TB_ISBUTTONHIDDEN, Cast(wParam, idButton), 0)
End Property
Property Class_ToolBar.ButtonVisible(idButton As uLong, bValue As Boolean)
     SendMessageW(hWndControl, TB_HIDEBUTTON, idButton, MAKELONG(bValue, 0)) 
End Property
Sub Class_ToolBar.SetFocus()  ' ȡ̽
     .SetFocus hWndControl
End Sub
Sub Class_ToolBar.SetImgSize(imSize As Long)  ' ðťͼߴ磨صλӦťǰá
   Dim siml As HIMAGELIST = Cast(HIMAGELIST, SendMessageW(hWndControl, TB_GETIMAGELIST, 0, 0)) 
   if siml Then ImageList_Destroy siml
   siml = ImageList_Create(imSize, imSize, ILC_COLOR32, 10, 2)
   ImageList_SetBkColor siml,GetSysColor(COLOR_BTNFACE)
   SendMessage(hWndControl, TB_SETIMAGELIST, 0, cast(lParam, siml))   'ĬΪ SendMessageW
End Sub
Function Class_ToolBar.AddButton(idCommand As Long ,ResImg As String ,zText As WString ,nTips As WString ,tbStyle As Long ,AutoSize As Long ,ShowText As Long ,xChecked As Long ,xEnabled As Long ,xHidden As Long ,xEllipses As Long ,dwData As lParam = 0) As Boolean
   Dim fp As FormControlsPro_TYPE ptr = vfb_Get_Control_Ptr(hWndControl)
   If fp Then
      fp->nData &= String(SizeOf(Class_ToolBar_Data) ,0)
      Dim u  As Long                   = Len(fp->nData) / SizeOf(Class_ToolBar_Data)
      dim ti As Long                   = u -1 ,i As Long
      Dim td As Class_ToolBar_Data Ptr = Cast(Any Ptr ,StrPtr(fp->nData))
      td[ti].idCommand = idCommand
      td[ti].ResImg    = ResImg
      td[ti].nTips     = nTips
      td[ti].idxBitmap = -1
      if u > 1 Then
         For i = 0 To u -2
            if td[i].ResImg = ResImg Then
               td[ti].idxBitmap = td[i].idxBitmap
               Exit For
            End If
         Next
      End if
      if td[ti].idxBitmap = -1 Then
         Dim siml  As HIMAGELIST = Cast(HIMAGELIST, SendMessageW(hWndControl, TB_GETIMAGELIST, 0, 0)) 
         Dim nIcon As HICON
         If .Left(ResImg ,7) = "BITMAP_" Then
            Dim nBmp As HBITMAP = LoadImageA(App.HINSTANCE ,ResImg ,IMAGE_BITMAP ,0 ,0 ,LR_DEFAULTCOLOR)
            Dim po   As ICONINFO
            po.fIcon    = TRUE
            po.hbmColor = nBmp
            po.hbmMask  = nBmp
            nIcon       = CreateIconIndirect(@po)
            DeleteObject nBmp
         ElseIf .Left(ResImg ,5) = "ICON_" Or ResImg = "AAAAA_APPICON" Then
            nIcon = LoadImageA(App.HINSTANCE ,ResImg ,IMAGE_ICON ,0 ,0 ,LR_DEFAULTCOLOR) 'Դͼ
         Else
           nIcon =ResourceLoadImage(App.HINSTANCE,ResImg) ' CreateIconOrCursorFromBitmap(LoadImageFromResourceA(StrPtr(ResImg)))
         End If
         td[ti].idxBitmap = ImageList_ReplaceIcon(siml , -1 ,nIcon)
         DestroyIcon nIcon
      End If
      Dim ppn As TBBUTTON ,ppm As WString * 20
      If tbStyle = 4 Then
         'Toolbar_AddSeparator hWndControl
         ppn.fsStyle = TBSTYLE_SEP
      Else
         Dim As ULong fsState ,fSStyle
         if xChecked <> 0  Then fsState Or= TBSTATE_CHECKED
         if xEnabled <> 0  Then fsState Or= TBSTATE_ENABLED
         if xHidden <> 0   Then fsState Or= TBSTATE_HIDDEN
         If xEllipses <> 0 Then fsState Or= TBSTATE_ELLIPSES
         Select Case tbStyle
            Case 0    : fSStyle = BTNS_BUTTON
            Case 1    : fSStyle = BTNS_CHECK
            Case 2    : fSStyle = BTNS_CHECKGROUP
            Case 3    : fSStyle = BTNS_DROPDOWN
            Case Else : fSStyle = BTNS_BUTTON
         End Select
         if AutoSize <> 0 Then fSStyle Or= BTNS_AUTOSIZE
         if ShowText <> 0 Then fSStyle Or= BTNS_SHOWTEXT
         'Function = Toolbar_AddButton(hWndControl, td[ti].idxBitmap, idCommand, fsState, fSStyle, 0, zText)
         ppn.iBitmap   = td[ti].idxBitmap
         ppn.idCommand = idCommand
         ppn.fsState   = fsState
         ppn.fsStyle   = fsStyle
         ppn.dwData = dwData
         ppn.iString   = Cast(INT_PTR ,@zText)
      End If
        
      Function = SendMessageW(hWndControl ,TB_ADDBUTTONS ,1 ,Cast(lParam ,@ppn)) 'ĬΪ SendMessageW
   End If

End Function
Property Class_ToolBar.UserData(idx AS LONG) As Integer      '/ûݣ1ؼԴ100ֵ
   If idx < 0 Or idx > 99 Then Return 0
   Dim fp As FormControlsPro_TYPE ptr = vfb_Get_Control_Ptr(hWndControl)
   If fp  Then
      Return fp->UserData(idx)
   End If   

End Property
Property Class_ToolBar.UserData(idx AS LONG, bValue As Integer)
   If idx < 0 Or idx > 99 Then Return 
   Dim fp As FormControlsPro_TYPE Ptr = vfb_Get_Control_Ptr(hWndControl)
   If fp  Then
      fp->UserData(idx) = bValue
   End If    
End Property

