PB603B1 - SetWindowCallback() extended example

Share your advanced PureBasic knowledge/code with the community.
Axolotl
Enthusiast
Enthusiast
Posts: 435
Joined: Wed Dec 31, 2008 3:36 pm

PB603B1 - SetWindowCallback() extended example

Post by Axolotl »

Hi there,
with the new improvements on SetWindowCallback() this is easily possible again .....
Small glitch: You have to use #PB_Window_Invisible with OpenWindow() and HideWindow() later.

Code: Select all

; 
; Purpose: The selected tab is using #SelectedTabColor as text color 
;          This example is based on PB-Help PanelGadget() example 
; 
EnableExplicit  ; <-- always a good idea to use this 

#SelectedTabColor = #Red   ; #Blue, #Yellow, etc. 


Procedure CallbackWindowProc(hWnd, uMsg, wParam, lParam) 
  Static hBrushStatic, hBrushButton 
  Protected bkcolor 

  Select uMsg 
    Case #WM_CTLCOLORSTATIC 
      ; Debug "WM_CTLCOLORSTATIC  " + lParam + " = " + GetDlgCtrlID_(lParam) 
;     SetTextColor_(wParam, #Black) 
      bkcolor = GetSysColor_(#COLOR_BTNFACE) 
;     SetBkColor_(wParam, bkcolor) 
      If hBrushStatic = #Null 
        hBrushStatic = CreateSolidBrush_(bkcolor) 
      EndIf 
      ProcedureReturn hBrushStatic 

    Case #WM_CTLCOLORBTN                        
      ; Debug "WM_CTLCOLORBTN  " + lParam + " = " + GetDlgCtrlID_(lParam) 
      bkcolor = GetSysColor_(#COLOR_BTNFACE) 
      If hBrushButton = #Null 
        hBrushButton = CreateSolidBrush_(bkcolor) 
      EndIf 
      ProcedureReturn hBrushButton 
    
    Case #WM_DESTROY 
      DeleteObject_(hBrushStatic)  ; not needed with GetSysColorBrush_(), but with CreateSolidBrush_() 
      DeleteObject_(hBrushButton)  ; not needed with GetSysColorBrush_(), but with CreateSolidBrush_() 

    Case #WM_DRAWITEM 
      Protected *drawItem.DRAWITEMSTRUCT 
      Protected text.s, textColor, brush  

      *drawItem = lParam  ; type cast :) 
      
      If *drawItem\itemID <> -1 And *drawItem\CtlType = #ODT_TAB  
        text = GetGadgetItemText(wParam, *drawItem\itemID) 

        brush = CreateSolidBrush_(GetSysColor_(#COLOR_3DFACE)) 
        If (*drawItem\itemState & #ODS_SELECTED) <> 0  ; for the selected tab we use RED 
          textColor = #SelectedTabColor 
        Else   
          textColor = GetSysColor_(#COLOR_WINDOWTEXT) 
        EndIf 

        FillRect_(*drawItem\hdc, *drawItem\rcItem, brush) 
        DeleteObject_(brush)  ; no longer needed 
        SetTextColor_(*drawItem\hdc, textColor) 
        SetBkMode_(*drawItem\hdc, #TRANSPARENT)  

        DrawText_(*drawItem\hdc, @text, Len(text), *drawItem\rcItem, #DT_CENTER | #DT_SINGLELINE | #DT_VCENTER | #DT_NOCLIP)
       ;TextOut_(*drawItem\hdc, *drawItem\rcItem\left+2, *drawItem\rcItem\top, @text, Len(text)) 

        ProcedureReturn 0  
      EndIf 

  EndSelect 
  ProcedureReturn #PB_ProcessPureBasicEvents  ; <-- PureBasic default 
EndProcedure


 If OpenWindow(0, 0, 0, 322, 220, "PanelGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered|#PB_Window_Invisible)
    PanelGadget     (0, 8, 8, 306, 203)
    SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) | #TCS_OWNERDRAWFIXED) 

      AddGadgetItem (0, -1, "Panel 1")
        PanelGadget (1, 5, 5, 290, 166)
        SetWindowLongPtr_(GadgetID(1), #GWL_STYLE, GetWindowLongPtr_(GadgetID(1), #GWL_STYLE) | #TCS_OWNERDRAWFIXED) 
          AddGadgetItem(1, -1, "Sub-Panel 1") 
            TextGadget(101,40,10,100,20,"Important text message ") 
          AddGadgetItem(1, -1, "Sub-Panel 2")
            TextGadget(102,40,20,100,20,"Important text message ") 
          AddGadgetItem(1, -1, "Sub-Panel 3")
            TextGadget(103,40,30,100,20,"Important text message ") 
        CloseGadgetList()

      AddGadgetItem (0, -1,"Panel 2")
        ButtonGadget(2, 10, 15, 80, 24,"Button 1")
        ButtonGadget(3, 95, 15, 80, 24,"Button 2")
        
        TextGadget(10,50,60,100,20,"Important text message ") 

    CloseGadgetList()

    SetGadgetState(0, 1)

    SetWindowCallback(@CallbackWindowProc(), 0) 
    HideWindow(0, 0)  ; first drawing is not triggered otherwise 

    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 
  EndIf
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home