Page 1 of 1

Setting background image of a TreeGadget

Posted: Mon Aug 30, 2004 12:17 pm
by oryaaaaa
Code updated For 5.20+

I've found a way to make a Treegadget with an image for a background. The other method I found on this board only enabled you to use black letters, but this sample displays everything as a sprite by automatically generates a mask. Hope this helps someone!

Code: Select all

Global WindowID, OldTreeGadgetProc, hDC, mDC, m2DC, width, height, Painting, lngSrcDefaultBkColor

Declare TreeGadgetProc(hWnd, uMsg, wParam, lParam)
#TVM_SETBKCOLOR = #TV_FIRST + 29
#TVM_SETTEXTCOLOR = #TV_FIRST + 30
#TVM_SETLINECOLOR = #TV_FIRST + 40
#SPRITE_TRANSPARENTCOLOR=$0088FF

If OpenWindow(0, 128, 96, 640, 480, "TreeGadget background image example", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
  WindowID = WindowID(0)
  TreeGadget = TreeGadget(0, 50, 50,500, 250)
  SendMessage_(GadgetID(0),#TVM_SETBKCOLOR,0,#SPRITE_TRANSPARENTCOLOR)
  SendMessage_(GadgetID(0),#TVM_SETLINECOLOR,0,$FF0000)
  SendMessage_(GadgetID(0),#TVM_SETTEXTCOLOR,0,$FF0000)
  
  For k=0 To 3
    AddGadgetItem(0, -1, "General "+Str(k))
    AddGadgetItem(0, -1, "ScreenMode")
    ;           OpenTreeGadgetNode(0)
    AddGadgetItem(0, -1, "640*480",0,1)
    AddGadgetItem(0, -1, "800*600",0,1)
    AddGadgetItem(0, -1, "1024*768",0,1)
    AddGadgetItem(0, -1, "1600*1200",0,1)
    ;           CloseTreeGadgetNode(0)
    AddGadgetItem(0, -1, "Joystick")
  Next
  LoadImage(0, "c:\body.bmp")
  width = ImageWidth(0)
  height = ImageHeight(0)
  hDC = GetDC_(WindowID)
  mDC = CreateCompatibleDC_(hDC) ; Device context of background image
  mOldObject = SelectObject_(mDC, ImageID(0))
  
  m2DC = CreateCompatibleDC_(hDC)
  hmBitmap = CreateCompatibleBitmap_(hDC, width, height) ; Device context of tree view image
  lngSrcDefaultBkColor = SetBkColor_(m2DC, #SPRITE_TRANSPARENTCOLOR)
  m2OldObject = SelectObject_(m2DC, hmBitmap)
  ReleaseDC_(WindowID, hDC)
  ; Callback to get tree view image
  OldTreeGadgetProc = SetWindowLong_(TreeGadget, #GWL_WNDPROC, @TreeGadgetProc())
  Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
  SelectObject_(mDC, mOldObject)
  DeleteDC_(mDC)
  SelectObject_(m2DC, m2OldObject)
  DeleteObject_(hmBitmap)
  DeleteDC_(m2DC)
  
EndIf
End

Procedure TreeGadgetProc(hWnd, uMsg, wParam, lParam)
  result = 0
  Select uMsg
    Case #WM_ERASEBKGND
      result = 1
    Case #WM_PAINT
      If Painting=0
        Painting = 1
        hDC = GetDC_(hWnd)
        
        result = CallWindowProc_(OldTreeGadgetProc, hWnd, uMsg, m2DC, 0) ; Get the tree view image.
        ngSrcDefaultBkColor = SetBkColor_(m2DC, #SPRITE_TRANSPARENTCOLOR)
        ; The background makes a white mask.
        hMaskBmpDC=CreateCompatibleDC_(hDC);
        hMaskBmp=CreateBitmap_(width,height,1,1,0);
        SelectObject_(hMaskBmpDC,hMaskBmp)
        BitBlt_(hMaskBmpDC,0,0,width,height,m2DC,0,0,#SRCCOPY)
        ;The background is a black mask image.
        hdcBlackMask = CreateCompatibleDC_(hDC)
        hbmBlackMask = CreateBitmap_(width, height, 1, 1, 0)
        hbmDefaultBlackMask = SelectObject_(hdcBlackMask, hbmBlackMask)
        BitBlt_(hdcBlackMask, 0, 0, width, height, hMaskBmpDC, 0, 0, #NOTSRCCOPY)
        ; Create a mask by converting the background color to black, and then paint the transparent parts by ANDing this mask,
        
        SetBkColor_(m2DC,lngSrcDefaultBkColor);
        BitBlt_(m2DC, 0, 0, width, height, hdcBlackMask, 0, 0, #SRCAND)
        ; The mask ..white.. is reproduced by same number of colors as a forwarding former image.
        hdcMask = CreateCompatibleDC_(hDC)
        hbmMask = CreateCompatibleBitmap_(hDC, width, height)
        hbmMaskDefault = SelectObject_(hdcMask, hbmMask)
        BitBlt_(hdcMask, 0, 0, width, height, hMaskBmpDC, 0, 0, #SRCCOPY)
        ; The background image is reproduced.
        hdcmDCcp = CreateCompatibleDC_(hDC)
        hbmmDCcp = CreateCompatibleBitmap_(hDC, width, height)
        hbmDCcpDefault = SelectObject_(hdcmDCcp, hbmmDCcp)
        BitBlt_(hdcmDCcp, 0, 0, width, height, mDC, 0, 0, #SRCCOPY)
        ; Penetration forwarding
        BitBlt_(hdcmDCcp, 0, 0, width, height, hdcMask, 0, 0, #SRCAND)
        BitBlt_(m2DC, 0, 0, width, height, hdcmDCcp, 0, 0, #SRCPAINT)
        
        BitBlt_(hDC, 0, 0, width, height, m2DC, 0, 0, #SRCCOPY) ; Copy a finished image onto hDC.
        
        DeleteDC_(hMaskBmpDC)
        DeleteDC_(hdcBlackMask)
        DeleteDC_(hdcMask)
        DeleteDC_(hdcmDCcp)
        DeleteObject_(hMaskBmp)
        DeleteObject_(hbmBlackMask)
        DeleteObject_(hbmMask)
        DeleteObject_(hbmmDCcp)
        ReleaseDC_(hWnd, hDC)
        Painting = 0
      EndIf
    Default
      result = CallWindowProc_(OldTreeGadgetProc, hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

Thanks

Posted: Mon Aug 30, 2004 9:41 pm
by Ralf
very nice example! thanks for sharing! ;-)

Posted: Fri Sep 03, 2004 9:52 pm
by oryaaaaa
However, it fails in the re-drawing when it is made to coexist with other Gadget.
Isn't there something good method?

Posted: Sat Sep 04, 2004 10:01 am
by Psychophanta
Hi oryaaaaa,
Thank you very much to show how to put a color to treeview fonts. You are the first I've seen in the forum to get it. I've requested for it several times, and finally you show it; thanx :) :D
However, it fails in the re-drawing when it is made to coexist with other Gadget.
Isn't there something good method?
I don't know, but i guess no. If you take a look to any other user interfaces which have image backgrounded treegadgets like MAME32 or MAME32Plus for example, you can see the tree gadgets fail in the re-drawing, and when you resize window too.
Here is an example based in Elchoni and modified by me which demonstrates that failure when redrawing, you can see it when resizing and you can see same effect in other apps like MAME32 o MESS32 user interfaces.

Code: Select all

Global WindowID, OldTreeGadgetProc, hDC, mDC, m2DC, width, height, Painting
Procedure.l MyWindowCallBack(WID,Message.l,wParam.l,lParam.l)
  Result.l=#PB_ProcessPureBasicEvents
    Select Message
    Case #WM_SIZE
      ResizeGadget(0,-1,-1,lParam&$FFFF,lParam>>16)
      Result=0
    EndSelect
  ProcedureReturn Result
EndProcedure

Procedure TreeGadgetProc(hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_ERASEBKGND
      result = 1
    Case #WM_PAINT
      If Painting=0
        Painting = 1
        ResizeGadget(0,-1,-1,WindowWidth(),WindowHeight())
        result = CallWindowProc_(OldTreeGadgetProc, hWnd, uMsg, m2DC, 0)
        BitBlt_(m2DC, 0, 0, width, height, mDC, 0, 0, #SRCAND)
        hDC = GetDC_(hWnd)
        BitBlt_(hDC, 0, 0, width, height, m2DC, 0, 0, #SRCCOPY)
        ReleaseDC_(hWnd, hDC)
        Painting = 0
      EndIf
    Default
      result=CallWindowProc_(OldTreeGadgetProc, hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure
WindowID=OpenWindow(0, 128, 96, 640, 480,#PB_Window_SizeGadget|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget, "TreeGadget background image example")
If WindowID=0:End:EndIf
If CreateGadgetList(WindowID)=0:End:EndIf
TreeGadget = TreeGadget(0, 0, 0, WindowWidth(), WindowHeight())
For k=0 To 3
  AddGadgetItem(0, -1, "General "+Str(k))
  AddGadgetItem(0, -1, "ScreenMode")
  OpenTreeGadgetNode(0)
    AddGadgetItem(0, -1, "640*480")
    AddGadgetItem(0, -1, "800*600")
    AddGadgetItem(0, -1, "1024*768")
    AddGadgetItem(0, -1, "1600*1200")
  CloseTreeGadgetNode(0)
  AddGadgetItem(0, -1, "Joystick")
Next
ima1.l=LoadImage(1,"prueba.BMP")
steph=ImageWidth():stepv=ImageHeight()
width = 1024;<-Full screen width
height = 768;<-Full screen height
GrabImage(1,0,0,0,width,height) ;<-Create a new image #0
;tile image #1 onto image #0
  StartDrawing(ImageOutput())
  hh=steph:vv=0
  While vv<height
    While hh<width
      DrawImage(ima1,hh,vv)
      hh+steph
    Wend
    hh=0:vv+stepv
  Wend
  StopDrawing()
hDC = GetDC_(WindowID)
mDC = CreateCompatibleDC_(hDC)
mOldObject = SelectObject_(mDC, UseImage(0))
m2DC = CreateCompatibleDC_(hDC)
hmBitmap = CreateCompatibleBitmap_(hDC, width, height)
m2OldObject = SelectObject_(m2DC, hmBitmap)
ReleaseDC_(WindowID, hDC)
OldTreeGadgetProc = SetWindowLong_(TreeGadget, #GWL_WNDPROC, @TreeGadgetProc())
;SetWindowCallback(@MyWindowCallBack())
Repeat:Until WaitWindowEvent()=#PB_EventCloseWindow
SelectObject_(mDC, mOldObject)
DeleteDC_(mDC)
SelectObject_(m2DC, m2OldObject)
DeleteObject_(hmBitmap)
DeleteDC_(m2DC)