Page 1 of 2
Image/Icon in a ComboBoxGadget()
Posted: Tue Sep 29, 2009 8:58 am
by Octavius
Hello,
As you surely know AddGadgetItem() does not support the ImageID parameter for ComboBoxGadget().
So how can I add an image or an icon on the left of each item in a ComboBox? Do I have to use an API function? Which one? Is there a simple way to do that?
Re: Image/Icon in a ComboBoxGadget()
Posted: Tue Sep 29, 2009 1:00 pm
by Sparkie
Re: Image/Icon in a ComboBoxGadget()
Posted: Tue Sep 29, 2009 1:39 pm
by Octavius
Yes it helps a lot.
Could it be possible to adapt this function so that the handle (hCombo) is a predetermined constant?
Code: Select all
hCombo = CreateWindowEx_(0, "ComboBoxEx32", "", #WS_BORDER | #WS_CHILD | #WS_VISIBLE | #CBS_DROPDOWN, 10, 10, 200, 200, WindowID(), 0, GetModuleHandle_(0), 0 )
Re: Image/Icon in a ComboBoxGadget()
Posted: Tue Sep 29, 2009 3:40 pm
by srod
Could it be possible to adapt this function so that the handle (hCombo) is a predetermined constant?
No chance; you are dealing with Window's handles here which are allocated dynamically. (I presume they are actually pointers to internal structures; but the point is that Windows itself allocates these values and are what you subsequently need to identify the particular control etc.)
Re: Image/Icon in a ComboBoxGadget()
Posted: Tue Sep 29, 2009 6:38 pm
by Octavius
Mmm...
If I understand well, the windows handle is the same thing than the PB GadgetID() function.
But then, if I want to detect an event with the WaitWindowEvent() function, I need the constant.
So, how can I detect the event if I don't have any associated constant?
Re: Image/Icon in a ComboBoxGadget()
Posted: Wed Sep 30, 2009 5:41 am
by netmaestro
Another way to skin the cat...
Code: Select all
Global Dim icons(4)
icons(0) = LoadIcon_(0, #IDI_APPLICATION)
icons(1) = LoadIcon_(0, #IDI_HAND)
icons(2) = LoadIcon_(0, #IDI_INFORMATION)
icons(3) = LoadIcon_(0, #IDI_QUESTION)
icons(4) = LoadIcon_(0, #IDI_WARNING)
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
DrawIconEx_(\hdc, 4,\rcitem\top+1, icons(\itemid),16,16, 0,0,#DI_NORMAL|#DI_COMPAT)
EndIf
SelectObject_(\hdc, oldbrush)
SetBkColor_( \hdc, GetSysColor_(#COLOR_HIGHLIGHT))
TextOut_( \hdc, \rcItem\left+24, \rcItem\top+1, 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
DrawIconEx_(\hdc, 4,\rcitem\top+1, icons(\itemid),16,16, 0,0,#DI_NORMAL)
EndIf
SetBkColor_( \hdc, #White)
TextOut_( \hdc, \rcItem\left+24, \rcItem\top+1, text$, Len(text$))
EndIf
EndWith
EndIf
Case #WM_MEASUREITEM
*mis.MEASUREITEMSTRUCT = lparam
If *mis\ctlid = 1 ; This gets changed to your gadget#
*mis\itemheight = 18
EndIf
EndSelect
ProcedureReturn result
EndProcedure
OpenWindow(0, 0, 0, 270, 140, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowCallback(@WinProc())
ComboBoxGadget(1, 10, 40, 250, 23,#CBS_OWNERDRAWVARIABLE)
For a = 1 To 5
AddGadgetItem(1, -1,"ComboBox item " + Str(a))
Next
SetGadgetState(1, 1)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Re: Image/Icon in a ComboBoxGadget()
Posted: Wed Sep 30, 2009 8:12 am
by Octavius
This works well, but I have problems loading my own images (with the LoadImage() PB function).
This code does not work :
Code: Select all
DrawIconEx_(\hdc,4,\rcitem\top+1,ImageID(#MyImage),16,16,0,0,#DI_NORMAL|#DI_COMPAT)
Re: Image/Icon in a ComboBoxGadget()
Posted: Wed Sep 30, 2009 9:56 am
by srod
The code is setup to draw icons only. Are you trying to load a bitmap?
Re: Image/Icon in a ComboBoxGadget()
Posted: Wed Sep 30, 2009 10:17 am
by Octavius
Yes, I try to load a TGA file 16*16 pixels. Is it possible to adapt the code? Or is it possible to convert the image into icon?
Re: Image/Icon in a ComboBoxGadget()
Posted: Wed Sep 30, 2009 10:27 am
by srod
Both things are possible.
I haven't time myself, but you can either switch the DrawIconEx_() for BitBlt_() if drawing bitmaps or use the ImageList api functions to create an icon. You could also switch the DrawIconEx_() and use the image list functions exclusively; adding icons and bitmaps as you see fit.
If you're unsure about these kind of things then just wait for that Canadian layabout to drift by; I'm sure he'll take a look!

Re: Image/Icon in a ComboBoxGadget()
Posted: Wed Sep 30, 2009 1:27 pm
by Sparkie
Nice work netmaestro.

Re: Image/Icon in a ComboBoxGadget()
Posted: Wed Sep 30, 2009 3:05 pm
by Octavius
I tried this :
Code: Select all
BitBlt_(\hdc,4,\rcitem\top+1,16,16,ImageID(#MyImage),0,0,0)
But I only got a black rectangle, again I need some help.
Re: Image/Icon in a ComboBoxGadget()
Posted: Wed Sep 30, 2009 3:07 pm
by netmaestro
Code: Select all
hdcIn = StartDrawing(ImageOutput(#myimage))
BitBlt_(\hdc,4,\rcitem\top+1,16,16,hdcIn,0,0,#SRCCOPY)
StopDrawing()
Re: Image/Icon in a ComboBoxGadget()
Posted: Wed Sep 30, 2009 3:23 pm
by Octavius
Thank you all, it finally works !
I have a last request however. My image contains an alpha chanel, so I'd like that these pixels were not displayed. Any idea?
I tried to change the constant #SCRCOPY to something else but it does not seem to work.
Re: Image/Icon in a ComboBoxGadget()
Posted: Wed Sep 30, 2009 3:35 pm
by netmaestro
I'll give you some png code later today.