bitmaps and icons overviewclasses: wxbitmap, wxbitmaphandler, wxicon, wxcursor. the wxbitmap class encapsulates the concept of a platform-dependent bitmap, either monochrome or colour. platform-specific methods for creating a wxbitmap object from an existing file are catered for, and this is an occasion where conditional compilation will sometimes be required. a bitmap created dynamically or loaded from a file can be selected into a memory device context (instance of wxmemorydc). this enables the bitmap to be copied to a window or memory device context using wxdc::blit, or to be used as a drawing surface. see wxmemorydc for an example of drawing onto a bitmap. all wxwidgets platforms support xpms for small bitmaps and icons. you may include the xpm inline as below, since it's c code, or you can load it at run-time.
#include "mondrian.xpm"sometimes you wish to use a .ico resource on windows, and xpms on other platforms (for example to take advantage of windows' support for multiple icon resolutions). a macro, wxicon, is available which creates an icon using an xpm on the appropriate platform, or an icon resource on windows.
wxicon icon(wxicon(mondrian)); // equivalent to: #if defined(__wxgtk__) || defined(__wxmotif__) wxicon icon(mondrian_xpm); #endif #if defined(__wxmsw__) wxicon icon("mondrian"); #endifthere is also a corresponding wxbitmap macro which allows to create the bitmaps in much the same way as wxicon creates icons. it assumes that bitmaps live in resources under windows or os2 and xpm files under all other platforms (for xpms, the corresponding file must be included before this macro is used, of course, and the name of the bitmap should be the same as the resource name under windows with _xpm suffix). for example:
// an easy and portable way to create a bitmap wxbitmap bmp(wxbitmap(bmpname)); // which is roughly equivalent to the following #if defined(__wxmsw__) || defined(__wxpm__) wxbitmap bmp("bmpname", wxbitmap_type_resource); #else // unix wxbitmap bmp(bmpname_xpm, wxbitmap_type_xpm); #endifyou should always use wxicon and wxbitmap macros because they work for any platform (unlike the code above which doesn't deal with wxmac, wxx11, ...) and are more short and clear than versions with #ifdefs. even better, use the same xpms on all platforms.
supported bitmap file formats
supported bitmap file formatsthe following lists the formats handled on different platforms. note that missing or partially-implemented formats are automatically supplemented by the wximage to load the data, and then converting it to wxbitmap form. note that using wximage is the preferred way to load images in wxwidgets, with the exception of resources (xpm-files or native windows resources). writing an image format handler for wximage is also far easier than writing one for wxbitmap, because wximage has exactly one format on all platforms whereas wxbitmap can store pixel data very differently, depending on colour depths and platform. wxbitmap under windows, wxbitmap may load the following formats:
under wxgtk, wxbitmap may load the following formats:
under wxmotif and wxx11, wxbitmap may load the following formats:
wxicon under windows, wxicon may load the following formats:
under wxgtk, wxicon may load the following formats:
under wxmotif and wxx11, wxicon may load the following formats:
wxcursor under windows, wxcursor may load the following formats:
under wxgtk, wxcursor may load the following formats (in additional to stock cursors):
under wxmotif and wxx11, wxcursor may load the following formats:
bitmap format handlersto provide extensibility, the functionality for loading and saving bitmap formats is not implemented in the wxbitmap class, but in a number of handler classes, derived from wxbitmaphandler. there is a static list of handlers which wxbitmap examines when a file load/save operation is requested. some handlers are provided as standard, but if you have special requirements, you may wish to initialise the wxbitmap class with some extra handlers which you write yourself or receive from a third party. to add a handler object to wxbitmap, your application needs to include the header which implements it, and then call the static function wxbitmap::addhandler. note: bitmap handlers are not implemented on all platforms, and new ones rarely need to be implemented since wximage can be used for loading most formats, as noted earlier.
|