Image/Icon in a ComboBoxGadget()
Image/Icon in a ComboBoxGadget()
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?
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()
This should help: http://www.purebasic.fr/english/viewtop ... 262#p84262
What goes around comes around.
PB 5.21 LTS (x86) - Windows 8.1
PB 5.21 LTS (x86) - Windows 8.1
Re: Image/Icon in a ComboBoxGadget()
Yes it helps a lot.
Could it be possible to adapt this function so that the handle (hCombo) is a predetermined constant?
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()
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.)Could it be possible to adapt this function so that the handle (hCombo) is a predetermined constant?
I may look like a mule, but I'm not a complete ass.
Re: Image/Icon in a ComboBoxGadget()
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?
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?
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Image/Icon in a ComboBoxGadget()
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
BERESHEIT
Re: Image/Icon in a ComboBoxGadget()
This works well, but I have problems loading my own images (with the LoadImage() PB function).
This code does not work :
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()
The code is setup to draw icons only. Are you trying to load a bitmap?
I may look like a mule, but I'm not a complete ass.
Re: Image/Icon in a ComboBoxGadget()
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()
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!

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!

I may look like a mule, but I'm not a complete ass.
Re: Image/Icon in a ComboBoxGadget()
Nice work netmaestro. 

What goes around comes around.
PB 5.21 LTS (x86) - Windows 8.1
PB 5.21 LTS (x86) - Windows 8.1
Re: Image/Icon in a ComboBoxGadget()
I tried this :
But I only got a black rectangle, again I need some help.
Code: Select all
BitBlt_(\hdc,4,\rcitem\top+1,16,16,ImageID(#MyImage),0,0,0)
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Image/Icon in a ComboBoxGadget()
Code: Select all
hdcIn = StartDrawing(ImageOutput(#myimage))
BitBlt_(\hdc,4,\rcitem\top+1,16,16,hdcIn,0,0,#SRCCOPY)
StopDrawing()
BERESHEIT
Re: Image/Icon in a ComboBoxGadget()
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.
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.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada