Image/Icon in a ComboBoxGadget()

Just starting out? Need help? Post your questions and find answers here.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Image/Icon in a ComboBoxGadget()

Post by netmaestro »

As promised...

Code: Select all

Prototype AlphaBlend(hdcDest,DestX,DestY,DestW,DestH,hdcSrc,SrcX,SrcY,SrcW,SrcH,BLENDFUNCTION) 
Declare WinProc(hwnd, msg, wparam, lparam)
Declare PreMultiply(image) 
Declare AlphaRender(imgSrc, hDCDest, x, y)

If FileSize(GetTemporaryDirectory()+"sportsicons.png") = -1
  InitNetwork()
  If Not ReceiveHTTPFile("http://www.lloydsplace.com/sportsicons.png", GetTemporaryDirectory()+"sportsicons.png")
    MessageRequester("oops...","Couldn't get image - ending",#MB_ICONERROR)  
  EndIf
EndIf

; If we're still here, we have the image in our temp directory

UsePNGImageDecoder()
allicons = LoadImage(#PB_Any, GetTemporaryDirectory()+"sportsicons.png")
cbicons = Premultiply(allicons)

Global Dim icons(4)
For i=0 To 4
  icons(i) = GrabImage(cbicons,i,i*20,0,20,20)
Next
FreeImage(cbicons)

OpenWindow(0, 0, 0, 270, 140, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowCallback(@WinProc())
ComboBoxGadget(1, 10, 40, 250, 26,#CBS_OWNERDRAWVARIABLE)
For a = 1 To 5
  AddGadgetItem(1, -1,"ComboBox item " + Str(a))
Next
SetGadgetState(1, 1) 

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

;===============================================
;            Declared Procedures
;===============================================

Procedure WinProc(hwnd, msg, wparam, lparam)
  result = #PB_ProcessPureBasicEvents
  
  Select msg
  
    Case #WM_DRAWITEM 
      *dis.DRAWITEMSTRUCT = lparam
      If *dis\ctlid = 1 ; This gets changed to your gadget#
        With *dis
          text$ = GetGadgetItemText(\ctlid, \itemid)
          If \itemstate & #ODS_SELECTED  
            hBrush = CreateSolidBrush_(GetSysColor_(#COLOR_HIGHLIGHT))
            oldbrush = SelectObject_(\hdc, hBrush)
            FillRect_(\hdc, \rcitem, hBrush)
            DeleteObject_(hBrush)
            DrawFocusRect_(\hdc, \rcitem)
            If \itemid >=0 And \itemid <= CountGadgetItems(\ctlid)-1
              AlphaRender(icons(\itemid), \hdc, 4,\rcitem\top )
            EndIf
            SelectObject_(\hdc, oldbrush)
            SetBkColor_( \hdc, GetSysColor_(#COLOR_HIGHLIGHT))
            TextOut_( \hdc, \rcItem\left+28, \rcItem\top+3, text$, Len(text$))
          Else
            hBrush = CreateSolidBrush_(#White)
            oldbrush = SelectObject_(\hdc, hBrush)
            FillRect_(\hdc, \rcitem, hBrush)
            DeleteObject_(hBrush)
            SelectObject_(\hdc, oldbrush)
            If \itemid >=0 And \itemid <= CountGadgetItems(\ctlid)-1
              AlphaRender(icons(\itemid), \hdc, 4,\rcitem\top )
            EndIf
            SetBkColor_( \hdc, #White)
            TextOut_( \hdc, \rcItem\left+28, \rcItem\top+3, text$, Len(text$))
          EndIf
        EndWith
      EndIf
      
    Case #WM_MEASUREITEM
      *mis.MEASUREITEMSTRUCT = lparam
      If *mis\ctlid = 1  ; This gets changed to your gadget#
        *mis\itemheight = 20
      EndIf
  EndSelect
  
  ProcedureReturn result
EndProcedure

Procedure PreMultiply(image) 

  imgout = GrabImage(image, #PB_Any, 0,0,ImageWidth(image),ImageHeight(image)) 
  GetObject_(ImageID(imgout), SizeOf(BITMAP), bmp.BITMAP)
  *bits.RGBQUAD = bmp\bmBits 
  For i=0 To bmp\bmHeight-1 
    For j=0 To bmp\bmWidthBytes-1 Step 4 
      *px.RGBQUAD = *bits + bmp\bmWidthBytes * i + j 
      *px\rgbBlue = *px\rgbBlue & $FF * *px\rgbReserved & $FF / $FF      
      *px\rgbGreen = *px\rgbGreen & $FF * *px\rgbReserved & $FF / $FF 
      *px\rgbRed = *px\rgbRed & $FF * *px\rgbReserved & $FF / $FF 
    Next 
  Next 
  
  ProcedureReturn imgout 

EndProcedure 

Procedure AlphaRender(imgSrc, hDCDest, x, y)
 
  hdcSrc  = CreateCompatibleDC_(#Null) 
  SelectObject_(hdcSrc, imgSrc) 
  GetObject_(imgSrc, SizeOf(BITMAP), bmpSrc.BITMAP) 
  msimg32 = OpenLibrary(#PB_Any, "msimg32.dll") 
  AlphaBlend_.AlphaBlend = GetFunction(msimg32, "AlphaBlend") 
  result=AlphaBlend_(hdcDest,x,y,bmpSrc\bmWidth,bmpSrc\bmHeight,hdcSrc,0,0,bmpSrc\bmWidth,bmpSrc\bmHeight,$1FF0000) 
  DeleteDC_(hdcSrc) 
  If IsLibrary(msimg32) 
    CloseLibrary(msimg32) 
  EndIf 
  
  ProcedureReturn result 

EndProcedure
BERESHEIT
Octavius
User
User
Posts: 15
Joined: Tue May 20, 2008 11:17 am
Location: France

Re: Image/Icon in a ComboBoxGadget()

Post by Octavius »

Excellent!

I have a last question. It seems that you open and close a dll each time the function AlphaRender is called, doesn't it slow the program? I mean, wouldn't it be better to open the dll at the begin of the program and close it at the end?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Image/Icon in a ComboBoxGadget()

Post by netmaestro »

Sure.
BERESHEIT
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

Re: Image/Icon in a ComboBoxGadget()

Post by +18 »

another cute share by netmaestro, Thanks man
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Image/Icon in a ComboBoxGadget()

Post by rsts »

Nice one. Thanks.

cheers
Post Reply