In SWT, custom combo may be implemented to maintain platform independence. SWT does not open custom interfaces for many controls. For example, in Win32, buttons, label, list, and ComboBox can all be self-painted (owner draw), but SWT does not open these painting methods. A new feature added in the latest version 3.2 is that table and tree now support custom draw (but not integrated into the viewer system ), however, the above controls are still supported.
Last time, I implemented a self-painted button. Now, you can see someone asking if you can add images to the combo list. In fact, this is quite easy. You only need to reload the combo widget and expose the self-painting interface. The following is a simple code example:
Package org. Eclipse. SWT. widgets;
Import java. Io .*;
Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. Graphics .*;
Import org. Eclipse. SWT. Internal. win32 .*;
Public class customcombo extends combo
{
Public customcombo (composite parent, int style)
{
Super (parent, style );
Try
{
Inputstream is = getclass (). getresourceasstream ("bullet.gif ");
Image = new image (getdisplay (), is );
Is. Close ();
}
Catch (ioexception E)
{
E. printstacktrace ();
}
Final int cb_setitemheight = 0x0153;
OS. sendmessage (handle, cb_setitemheight, 0, 24 );
OS. sendmessage (handle, cb_setitemheight,-1, 24 );
}
@ Override
Int widgetstyle ()
{
Final int cbs_ownerdrawfixed = 0x0010;
Final int cbs_hasstrings = 0x0200;
// Final int cbs_ownerdrawvariable = 0x0020;
Return super. widgetstyle () | cbs_ownerdrawfixed | cbs_hasstrings;
}
@ Override
Protected void checksubclass ()
{
}
@ Override
Public void dispose ()
{
Image. Dispose ();
Super. Dispose ();
}
/* @ Override
Lresult wmmeasurechild (INT wparam, int lparam)
{
Measureitemstruct MIS = new measureitemstruct ();
OS. movememory (MIS, lparam, measureitemstruct. sizeof );
Mis. itemheight = 40;
OS. movememory (lparam, MIS, measureitemstruct. sizeof );
Return NULL; // super. wmmeasurechild (wparam, lparam );
}*/
@ Override
Lresult wmdrawchild (INT wparam, int lparam)
{
Drawitemstruct Dis = new drawitemstruct ();
OS. movememory (DIS, lparam, drawitemstruct. sizeof );
GC = new GC (New dcwrapper (DIS. HDC ));
Rectangle rc = new rectangle (DIS. Left, dis. Top, dis. Right-dis. Left,
Dis. Bottom-dis. Top );
Display display = getdisplay ();
If (DIS. itemstate & OS. ods_selected )! = 0)
{
GC
. Setbackground (Display
. Getsystemcolor (SWT. color_list_selection ));
GC. setforeground (Display
. Getsystemcolor (SWT. color_list_selection_text ));
GC. fillrectangle (RC );
}
Else
{
GC. setbackground (Display
. Getsystemcolor (SWT. color_list_background ));
GC. setforeground (Display
. Getsystemcolor (SWT. color_list_foreground ));
GC. fillrectangle (RC );
}
String text = getitem (DIS. Itemid );
GC. drawimage (image, dis. Left + 1, dis. Top + 1 );
GC. drawtext (text, dis. Left + 20, dis. Top );
GC. Dispose ();
Return NULL;
}
Private Static class dcwrapper implements drawable
{
Private int HDC;
Dcwrapper (int hdc)
{
This. HDC = HDC;
}
Public int internal_new_gc (gcdata data)
{
Return HDC;
}
Public void internal_dispose_gc (INT handle, gcdata data)
{
}
}
Private image;
}
It is worth noting that if combo is set to ownerdraw variable style, the wmmeasurechild method must be overloaded to specify the height of each item. If you use the ownerdraw fixed style, you only need to send a cb_setitemheight message during the construction.
Another option worth considering is to wrap the Win32 comboboxex control into a SWT widget. However, this requires converting several structures and providing interfaces. The imagelist Management Mechanism of Win32 differs greatly from the image packaging method of SWT, which makes implementation of this method much more troublesome.