Background Image in ListIcon

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by searhin.

Hi there,

would it be possible to display a background image in a ListIcon gadget?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Denis.

Hi searhin,

take a look here :

http://msdn.microsoft.com/library/defau ... kimage.asp

Denis
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

Denis, I just had a look at that but there is a structure involvded for the image information so it's not as straighforward. Might be a problem with the repaint too as with an example earlier in the forum for ListViewGadget.

Do you understand how to implement the structure behind it?

We are Dyslexic of Borg, prepare to have your ass laminated!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Hi, try this:

Code: Select all

#LVBKIF_SOURCE_NONE = 0
#LVBKIF_SOURCE_HBITMAP = 1
#LVBKIF_SOURCE_URL = 2
#LVBKIF_SOURCE_MASK = 3
#LVBKIF_STYLE_NORMAL = 0
#LVBKIF_STYLE_TILE = $10
#LVBKIF_STYLE_MASK = $10
#LVM_SETBKIMAGE = #LVM_FIRST + 68
#LVM_SETBKIMAGEW = #LVM_FIRST + 138
#LVM_GETBKIMAGE = #LVM_FIRST + 69
#LVM_GETBKIMAGEW = #LVM_FIRST + 139
#CLR_NONE = -1

Structure LVBKIMAGE
  ulFlags.l
  hbm.l
  pszImage.l
  cchImageMax.l
  xOffsetPercent.l
  yOffsetPercent.l
EndStructure

OleInitialize_(0)
If OpenWindow(0, 384, 288, 640, 480, #PB_Window_SystemMenu|#PB_Window_MinimizeGadget, "ListIconGadget background image example")
  LVWidth = WindowWidth()
  LVCWidth = Int(LVWidth/4)-1
  If CreateGadgetList(WindowID())
    ListIconGadget = ListIconGadget(0, 0, 0, LVWidth, WindowHeight(), "Column 0", LVCWidth, #PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect)
    AddGadgetColumn(0, 1, "Column 1", LVCWidth)
    AddGadgetColumn(0, 2, "Column 2", LVCWidth)
    AddGadgetColumn(0, 3, "Column 3", LVCWidth)
    AddGadgetItem(0, 0, "Aaa 1"+Chr(10)+"Bcc 3"+Chr(10)+"Cdd 2"+Chr(10)+"Eee 3"+Chr(10), 0)
    AddGadgetItem(0, 1, "Aab 2"+Chr(10)+"Bbc 2"+Chr(10)+"Ddd 3"+Chr(10)+"Dde 1"+Chr(10), 0)
    AddGadgetItem(0, 2, "Abb 3"+Chr(10)+"Baa 1"+Chr(10)+"Ccd 1"+Chr(10)+"Dee 2"+Chr(10), 0)
    SendMessage_(ListIconGadget, #LVM_SETTEXTCOLOR, 0, $ff0000)
    SendMessage_(ListIconGadget, #LVM_SETBKCOLOR, 0, #CLR_NONE)
    SendMessage_(ListIconGadget, #LVM_SETTEXTBKCOLOR, 0, #CLR_NONE)
    Buffer = AllocateMemory(0, 512)
    GetModuleFileName_(GetModuleHandle_(0), Buffer, 512)
    InitialDir$ = GetPathPart(PeekS(Buffer))
    FreeMemory(0)
    File$ = OpenFileRequester("Select image", InitialDir$, "ListIcon supported images|*.bmp;*.ico;*.gif;*.jpg;*.wmf;*.emf", 0)
    If File$
      lbk.LVBKIMAGE
      lbk\ulFlags = #LVBKIF_STYLE_NORMAL|#LVBKIF_SOURCE_URL;|#LVBKIF_STYLE_TILE
      lbk\pszImage = @File$
;      lbk\xOffsetPercent;
;      lbk\yOffsetPercent;
      SendMessage_(ListIconGadget, #LVM_SETBKIMAGE, 0, lbk)
    EndIf
    Repeat
      EventID = WaitWindowEvent()
    Until EventID = #PB_EventCloseWindow
  EndIf
EndIf
OleUninitialize_()
End
Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

All I can say is, WOW! El_Choni, you are amazing :)


We are Dyslexic of Borg, prepare to have your ass laminated!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

El_Choni can the same thing be done with TreeGadgets and ListViewGadgets without resorting to callbacks?

I've already integrated your above example into my program for a nice, simple graphic in the ListIconGadget and it looks great.

We are Dyslexic of Borg, prepare to have your ass laminated!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

For TreeViews it's a bit tricky and difficult. For ListView, this may work, but I haven't tested how it works adding items during program execution:

Code: Select all

Global OldListViewProc, mDC, width, height

Declare ListViewProc(hWnd, uMsg, wParam, lParam)
Declare OleOpen(File$)

OleInitialize_(0)
If OpenWindow(0, 128, 96, 640, 480, #PB_Window_SystemMenu|#PB_Window_MinimizeGadget, "ListView background image example")
  If CreateGadgetList(WindowID())
    ListViewGadget = ListViewGadget(0, 0, 0, WindowWidth(), WindowHeight())
    AddGadgetItem(0, 0, "Aaa 1")
    AddGadgetItem(0, 1, "Aab 2")
    AddGadgetItem(0, 2, "Abb 3")
    Buffer = AllocateMemory(0, 512)
    GetModuleFileName_(GetModuleHandle_(0), Buffer, 512)
    InitialDir$ = GetPathPart(PeekS(Buffer))
    FreeMemory(0)
    File$ = OpenFileRequester("Select image", InitialDir$, "Supported images|*.bmp;*.ico;*.gif;*.jpg;*.wmf;*.emf", 0)
    If File$
      OleOpen(File$)
      ; For bitmaps only, delete OleOpen(), delete the above line and use:
      ; LoadImage(0, File$)
      ; hDC = GetDC_(WindowID)
      ; mDC = CreateCompatibleDC_(hDC)
      ; OldObject = SelectObject_(mDC, UseImage(0))
    EndIf
    OldListViewProc = SetWindowLong_(ListViewGadget, #GWL_WNDPROC, @ListViewProc())
    Repeat:Until WaitWindowEvent()=#PB_EventCloseWindow
    SelectObject_(mDC, OldObject)
    DeleteDC_(mDC)
  EndIf
EndIf
OleUninitialize_()
End

Procedure ListViewProc(hWnd, uMsg, wParam, lParam)
  result = 0
  Select uMsg
    Case #WM_LBUTTONDOWN
      result = CallWindowProc_(OldListViewProc, hWnd, uMsg, wParam, lParam)
      SendMessage_(hWnd, #WM_PAINT, 0, 0)
    Case #WM_PAINT
      result = CallWindowProc_(OldListViewProc, hWnd, uMsg, wParam, lParam)
      hDC = GetDC_(hWnd)
      BitBlt_(hDC, 0, 0, width, height, mDC, 0, 0, #SRCAND)
      ReleaseDC_(hWnd, hDC)
    Default
      result = CallWindowProc_(OldListViewProc, hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

#E_POINTER = $80000005
#E_NOINTERFACE = $80000004
#E_OUTOFMEMORY = $80000002

#IPicture_Release = 8
#IPicture_Get_Width = 24
#IPicture_Get_Height = 28
#IPicture_Render = 32

Procedure OleOpen(ImageFile$)
  Shared pstm, gpPicture, hmwidth, hmheight, OldObject
  hBitmap = 0
  If ReadFile(0, ImageFile$)
    FileSize = FileSize(ImageFile$)
    hGlobal = GlobalAlloc_(#GMEM_MOVEABLE, FileSize)
    pvData = GlobalLock_(hGlobal)
    ReadData(pvData, FileSize)
    CloseFile(0)
    GlobalUnlock_(hGlobal)
    CreateStreamOnHGlobal_(hGlobal, #TRUE, @pstm)
    If gpPicture
      CallCOM(#IPicture_Release, gpPicture)
    EndIf
    ErrorNumber = 0
    Select OleLoadPicture_(pstm, FileSize, #FALSE, ?IID_IPicture, @gpPicture)
      Case #E_POINTER
        ErrorNumber = 222
      Case #E_NOINTERFACE
        ErrorNumber = 223
      Case #E_OUTOFMEMORY
        ErrorNumber = 224
    EndSelect
    CallCOM(#IPicture_Release, pstm)
    GlobalFree_(hGlobal)
    If ErrorNumber=0
      CallCOM(#IPicture_Get_Width, gpPicture, @hmwidth)
      CallCOM(#IPicture_Get_Height, gpPicture, @hmheight)
      WindowID = WindowID()
      hDC = GetDC_(WindowID)
      xScreenPixels = GetDeviceCaps_(hDC, #LOGPIXELSX)
      yScreenPixels = GetDeviceCaps_(hDC, #LOGPIXELSY)
      width = Abs((hmwidth*xScreenPixels)/2540)
      height = Abs((hmheight*yScreenPixels)/2540)
      rc.RECT
      rc\left = 0
      rc\top = 0
      rc\right = width
      rc\bottom = height
      mDC = CreateCompatibleDC_(hDC)
      hBitmap = CreateCompatibleBitmap_(hDC, width, height)
      SelectObject_(mDC, hBitmap)
      CallCOM(#IPicture_Render, gpPicture, mDC, 0, 0, width, height, 0, hmheight, hmwidth, -hmheight, rc)
      ReleaseDC_(WindowID, hDC)
      CallCOM(#IPicture_Release, gpPicture)
    EndIf
  EndIf
  ProcedureReturn hBitmap
EndProcedure

!section '.data' Data readable writeable
IID_IPicture:
!dd $07bf80980
!dw $0bf32, $0101a
!db $08b, $0bb, 0, $0aa, 0, $030, $0c, $0ab
Note: you can delete all the OLE stuff if you are using bitmaps, I just put it here so it's as complete as the ListIcon example.

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

This works for TreeView, but flickers a lot with any repainting. Any idea how to solve that?

Code: Select all

Global WindowID, OldTreeGadgetProc, hDC, mDC, width, height, mOldObject

Declare TreeGadgetProc(hWnd, uMsg, wParam, lParam)
Declare OleOpen(File$)

OleInitialize_(0)
If OpenWindow(0, 128, 96, 640, 480, #PB_Window_SystemMenu|#PB_Window_MinimizeGadget, "TreeGadget background image example")
  WindowID = WindowID()
  If CreateGadgetList(WindowID)
    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
    Buffer = AllocateMemory(0, 512)
    GetModuleFileName_(GetModuleHandle_(0), Buffer, 512)
    InitialDir$ = GetPathPart(PeekS(Buffer))
    FreeMemory(0)
    File$ = OpenFileRequester("Select image", InitialDir$, "Supported images|*.bmp;*.ico;*.gif;*.jpg;*.wmf;*.emf", 0)
    If File$
      OleOpen(File$)
      ; For bitmaps only, delete OleOpen(), delete the above line and use:
      ; LoadImage(0, File$)
      ; hDC = GetDC_(WindowID)
      ; mDC = CreateCompatibleDC_(hDC)
      ; mOldObject = SelectObject_(mDC, UseImage(0))
    EndIf
    OldTreeGadgetProc = SetWindowLong_(TreeGadget, #GWL_WNDPROC, @TreeGadgetProc())
    Repeat:Until WaitWindowEvent()=#PB_EventCloseWindow
    SelectObject_(mDC, mOldObject)
    DeleteDC_(mDC)
  EndIf
EndIf
OleUninitialize_()
End

Procedure TreeGadgetProc(hWnd, uMsg, wParam, lParam)
  result = 0
  Select uMsg
    Case #WM_PAINT
      InvalidateRect_(hWnd, 0, #TRUE)
      result = CallWindowProc_(OldTreeGadgetProc, hWnd, uMsg, wParam, lParam)
      hDC = GetDC_(hWnd)
      BitBlt_(hDC, 0, 0, width, height, mDC, 0, 0, #SRCAND)
      ReleaseDC_(hWnd, hDC)
    Default
      result = CallWindowProc_(OldTreeGadgetProc, hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

#E_POINTER = $80000005
#E_NOINTERFACE = $80000004
#E_OUTOFMEMORY = $80000002

#IPicture_Release = 8
#IPicture_Get_Width = 24
#IPicture_Get_Height = 28
#IPicture_Render = 32

Procedure OleOpen(ImageFile$)
  Shared pstm, gpPicture, hmwidth, hmheight, OldObject
  hBitmap = 0
  If ReadFile(0, ImageFile$)
    FileSize = FileSize(ImageFile$)
    hGlobal = GlobalAlloc_(#GMEM_MOVEABLE, FileSize)
    pvData = GlobalLock_(hGlobal)
    ReadData(pvData, FileSize)
    CloseFile(0)
    GlobalUnlock_(hGlobal)
    CreateStreamOnHGlobal_(hGlobal, #TRUE, @pstm)
    If gpPicture
      CallCOM(#IPicture_Release, gpPicture)
    EndIf
    ErrorNumber = 0
    Select OleLoadPicture_(pstm, FileSize, #FALSE, ?IID_IPicture, @gpPicture)
      Case #E_POINTER
        ErrorNumber = 222
      Case #E_NOINTERFACE
        ErrorNumber = 223
      Case #E_OUTOFMEMORY
        ErrorNumber = 224
    EndSelect
    CallCOM(#IPicture_Release, pstm)
    GlobalFree_(hGlobal)
    If ErrorNumber=0
      CallCOM(#IPicture_Get_Width, gpPicture, @hmwidth)
      CallCOM(#IPicture_Get_Height, gpPicture, @hmheight)
      hDC = GetDC_(WindowID)
      xScreenPixels = GetDeviceCaps_(hDC, #LOGPIXELSX)
      yScreenPixels = GetDeviceCaps_(hDC, #LOGPIXELSY)
      width = Abs((hmwidth*xScreenPixels)/2540)
      height = Abs((hmheight*yScreenPixels)/2540)
      rc.RECT
      rc\left = 0
      rc\top = 0
      rc\right = width
      rc\bottom = height
      mDC = CreateCompatibleDC_(hDC)
      hBitmap = CreateCompatibleBitmap_(hDC, width, height)
      mOldObject = SelectObject_(mDC, hBitmap)
      CallCOM(#IPicture_Render, gpPicture, mDC, 0, 0, width, height, 0, hmheight, hmwidth, -hmheight, rc)
      ReleaseDC_(WindowID, hDC)
      CallCOM(#IPicture_Release, gpPicture)
    EndIf
  EndIf
  ProcedureReturn hBitmap
EndProcedure

!section '.data' Data readable writeable
IID_IPicture:
!dd $07bf80980
!dw $0bf32, $0101a
!db $08b, $0bb, 0, $0aa, 0, $030, $0c, $0ab
As in the previous snippet, OLE stuff can be deleted if you only use bitmaps.

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Justin.

very nice examples. i would say you need an ownerdrawn treeview to avoid flickering, i'll look into it.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Now only the cursor flickers XD

Code: Select all

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

Declare TreeGadgetProc(hWnd, uMsg, wParam, lParam)

If OpenWindow(0, 128, 96, 640, 480, #PB_Window_SystemMenu|#PB_Window_MinimizeGadget, "TreeGadget background image example")
  WindowID = WindowID()
  If CreateGadgetList(WindowID)
    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
    LoadImage(0, "prueba.bmp")
    width = ImageWidth()
    height = ImageHeight()
    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())
    Repeat:Until WaitWindowEvent()=#PB_EventCloseWindow
    SelectObject_(mDC, mOldObject)
    DeleteDC_(mDC)
    SelectObject_(m2DC, m2OldObject)
    DeleteObject_(hmBitmap)
    DeleteDC_(m2DC)
  EndIf
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
;        BeginPaint_(hWnd, ps.PAINTSTRUCT)
        result = CallWindowProc_(OldTreeGadgetProc, hWnd, uMsg, m2DC, 0)
;        BitBlt_(m2DC, ps\rcPaint\left, ps\rcPaint\top, ps\rcPaint\right-ps\rcPaint\left, ps\rcPaint\bottom-ps\rcPaint\top, mDC, ps\rcPaint\left, ps\rcPaint\top, #SRCAND)
        BitBlt_(m2DC, 0, 0, width, height, mDC, 0, 0, #SRCAND)
        hDC = GetDC_(hWnd)
;        BitBlt_(hDC, ps\rcPaint\left, ps\rcPaint\top, ps\rcPaint\right-ps\rcPaint\left, ps\rcPaint\bottom-ps\rcPaint\top, m2DC, ps\rcPaint\left, ps\rcPaint\top, #SRCCOPY)
        BitBlt_(hDC, 0, 0, width, height, m2DC, 0, 0, #SRCCOPY)
        ReleaseDC_(hWnd, hDC)
;        EndPaint_(hWnd, ps)
        Painting = 0
      EndIf
    Default
      result = CallWindowProc_(OldTreeGadgetProc, hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure
Removed unnecesary stuff for testing.

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by searhin.

@All: many thanx for all the replies!

El Choni,
I also tried your last code as you pasted it, and it works great, really GREAT!!

(without the BeginPaint/EndPaint lines, however, it is very slow and needs almost all processor time)
Justin
Addict
Addict
Posts: 956
Joined: Sat Apr 26, 2003 2:49 pm

Post by Justin »

Can anyone repost this excellent code by elchoni? , due to the forum backup it can't be paste it(that old problem), and it's big :cry:
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Post by Berikco »

First paste it in wordpad, should work fine
Justin
Addict
Addict
Posts: 956
Joined: Sat Apr 26, 2003 2:49 pm

Post by Justin »

Does not work here.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Which one do you need? ListIcon, ListView or TreeGadget? (BTW, I don't have problems copying pasting from old posts, but you're right, it's MUCH better when [ code ] [ /code ] keywords are used, everybody should do it, IMHO).
El_Choni
Post Reply