Image/Icon in a ComboBoxGadget()

Just starting out? Need help? Post your questions and find answers here.
Octavius
User
User
Posts: 15
Joined: Tue May 20, 2008 11:17 am
Location: France

Image/Icon in a ComboBoxGadget()

Post 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?
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Re: Image/Icon in a ComboBoxGadget()

Post by Sparkie »

What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Octavius
User
User
Posts: 15
Joined: Tue May 20, 2008 11:17 am
Location: France

Re: Image/Icon in a ComboBoxGadget()

Post 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 )
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Image/Icon in a ComboBoxGadget()

Post 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.)
I may look like a mule, but I'm not a complete ass.
Octavius
User
User
Posts: 15
Joined: Tue May 20, 2008 11:17 am
Location: France

Re: Image/Icon in a ComboBoxGadget()

Post 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?
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 »

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
Octavius
User
User
Posts: 15
Joined: Tue May 20, 2008 11:17 am
Location: France

Re: Image/Icon in a ComboBoxGadget()

Post 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)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Image/Icon in a ComboBoxGadget()

Post by srod »

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.
Octavius
User
User
Posts: 15
Joined: Tue May 20, 2008 11:17 am
Location: France

Re: Image/Icon in a ComboBoxGadget()

Post 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?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Image/Icon in a ComboBoxGadget()

Post 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! :wink:
I may look like a mule, but I'm not a complete ass.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Re: Image/Icon in a ComboBoxGadget()

Post by Sparkie »

Nice work netmaestro. 8)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Octavius
User
User
Posts: 15
Joined: Tue May 20, 2008 11:17 am
Location: France

Re: Image/Icon in a ComboBoxGadget()

Post 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.
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 »

Code: Select all

hdcIn = StartDrawing(ImageOutput(#myimage))
  BitBlt_(\hdc,4,\rcitem\top+1,16,16,hdcIn,0,0,#SRCCOPY) 
StopDrawing()
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 »

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.
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 »

I'll give you some png code later today.
BERESHEIT
Post Reply