Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Owner-drawn buttons (DDT)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Owner-drawn buttons (DDT)

    Not straight translation a code, published in "Win32 Programming" (4ed.), by B.Rector & J.Newcomer
    Code:
    #Compile Exe
    #Register None
    
    #Include "Win32Api.Inc"
    
    %ID_Button1 = 301
    %ID_Button2 = 302
    %ID_Button3 = 303
    
    CallBack Function hDlg_CB
       Dim hBrush1 As Static Long, hBrush2 As Static Long
       Dim cxb As Static Long, cyb As Static Long, cxi As Static Long, cyi As Static Long
       Local tDrawItem As DRAWITEMSTRUCT Ptr, Icon As Long, ry As Long, tRect As Rect
    
       Select Case CbMsg
          Case %WM_INITDIALOG
             Local Lb As LOGBRUSH
             Lb.lbStyle = %BS_SOLID
             Lb.lbColor = %Blue: hBrush1 = CreateBrushIndirect(Lb)
             Lb.lbColor = %Yellow: hBrush2 = CreateBrushIndirect(Lb)
    
             cxb = GetSystemMetrics(%SM_CXBORDER): cyb = GetSystemMetrics(%SM_CYBORDER)
             cxi = GetSystemMetrics(%SM_CXSMICON): cyi = GetSystemMetrics(%SM_CYSMICON)
          Case %WM_DESTROY
             DeleteObject hBrush1: DeleteObject hBrush2
          Case %WM_DRAWITEM
             tDrawItem = CbLparam
             Select Case @tDrawItem.CtlID
                Case %ID_Button1, %ID_Button2
                   Icon = LoadIcon(0, ByVal %IDI_ASTERISK)
                   tRect = @tDrawItem.RcItem: ry = tRect.nBottom
                   If @tDrawItem.CtlID = %ID_Button2 Then tRect.nLeft = 4 * cxb + cxi
                   FrameRect @tDrawItem.hDc,@tDrawItem.rcItem, GetStockObject(%BLACK_BRUSH)
                   If (@tDrawItem.itemState And %ODS_SELECTED) Then
                      FillRect @tDrawItem.hDc, @tDrawItem.rcItem, hBrush2
                      DrawEdge @tDrawItem.hDC, @tDrawItem.rcItem, %Edge_Sunken, %BF_RECT
                      SetBkMode @tDrawItem.hDC, %TRANSPARENT
                      SetTextColor @tDrawItem.hDC, %BLACK
                      DrawText @tDrawItem.hDC, "Don't touch me", -1, tRect, %DT_SINGLELINE Or %DT_CENTER Or %DT_VCENTER
                   Else
                      Call FillRect(@tDrawItem.hDc, @tDrawItem.rcItem, hBrush1)
                      DrawEdge @tDrawItem.hDC, @tDrawItem.rcItem, %Edge_Raised, %BF_RECT
                      SetBkMode @tDrawItem.hDC, %TRANSPARENT
                      SetTextColor @tDrawItem.hDC, %WHITE
                      DrawText @tDrawItem.hDC, "Click me", -1, tRect, %DT_SINGLELINE Or %DT_CENTER Or %DT_VCENTER
                   End If
                   If (@tDrawItem.itemState And %ODS_FOCUS) Then
                      InflateRect @tDrawItem.RcItem, -3 * cxb, -3 * cyb
                      DrawFocusRect @tDrawItem.hDC, @tDrawItem.rcItem
                    End If
                    If @tDrawItem.CtlID = %ID_Button2 Then DrawIconEx @tDrawItem.hDC, 7 * cxb, (ry - cyi) / 2, icon, cxi, cyi, 0, ByVal 0, %DI_Normal
                    Function = %False
                Case Else
                    Function = %True
                End Select
        End Select
    
    End Function
    
    Function PbMain
    
      Local hDlg As Long
      Dialog New 0 ,"Owner-drawn", 0, 0, 105, 90, %DS_CENTER Or %WS_SYSMENU  To hDlg
      Control Add Button, hDlg, %ID_Button1, "", 10, 5, 80, 16, %WS_TABSTOP Or %BS_OWNERDRAW
      Control Add Button, hDlg, %ID_Button2, "", 10, 25, 80, 16, %WS_TABSTOP Or %BS_OWNERDRAW
      Control Add Button, hDlg, %ID_Button3, "Classic button", 10, 50, 80, 14
      Dialog Show Modal hDlg Call hDlg_CB
    
    End Function
Working...
X