Re: Image/Icon in a ComboBoxGadget()
Posted: Thu Oct 01, 2009 12:34 am
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