Using Device-Independent Bitmaps in WinCE Development
Device-independent bitmaps take their name from the fact that they include color information along with image data. The advantage of this is that they can be dynamically adjusted to paint properly on any display device, with the disadvantage that the color information takes up space and significantly complicates the process of reading a file and rendering the bitmap. For this reason, if you use DIBs, you use different APIs to access them. On CE devices, unlike on the desktop, we use the function SHLoadDIBitmap() to get a handle to the DIB. After that, the steps required to manipulate and display the image are more or less the same ones you would use with a device-dependent bitmap. The example application PasteDIB shows these steps, but because it's much like the bitmap code you've already seen, we'll just highlight the differences here. (The sources and support files are included in their entirety on the accompanying CD.)
First, for many CE platforms, you'll need a special header file, shellapi.h:
Next, we load the bitmap file by name in response to the WM_PAINT message:
case WM_PAINT: RECT rt; hdc = BeginPaint(hWnd, &ps); GetClientRect(hWnd, &rt); //use a null-terminated Unicode string for the file name hbmpDIB = SHLoadDIBitmap( TEXT("\\Windows\\Clouds.dib")); //now, use the returned bitmap handle in the typical ways hdcMemory = CreateCompatibleDC( hdc ); SelectObject( hdcMemory, hbmpDIB ); GetObject( hbmpDIB, sizeof(BITMAP), &bmp ); BitBlt(hdc, 0, (GetSystemMetrics(SM_CYSCREEN ) / 8 ), bmp.bmWidth, bmp.bmHeight, hdcMemory, 0, 0, SRCCOPY ); EndPaint(hWnd, &ps); break;
Notice two things about this code fragment. First, we use the TEXT() macro to format a literal string for the file name. Under CE, all filenames and user interface text strings are Unicode. SHLoadDIBitmap() will fail if the filename string is not Unicode. Also, notice that we have specified a fully qualified pathname, \Windows\Clouds.dib, for the DIB file. When you copy files, a CE device to try DIBPaste; you'll need make sure that Clouds.dib ends up there. Finally, notice that once we retrieve a handle to the bitmap data contained in the DIB file, we can treat it in the same way as we'd treat any other bitmap.
Perhaps the most noticeable differences in the graphics programming you'll undertake for CE hinge on the fact that users rely heavily or even exclusively on the "touchable screen" metaphor implemented in the stylus. In some cases, programming for stylus "taps" isn't awfully different than programming for mouse clicks. When it comes to the stylus as a text input device, however, things definitely begin to become a bit more complex.
In the next series of examples, we'll explore handling the stylus and the ink control. In the process, we'll look into what you can do to make your Win32 applications stylus capable as quickly and effectively as possible.
About the Author
Nancy Nicolaisen is a software engineer who has designed and implemented highly modular Windows CE products that include features such as full remote diagnostics, CE-side data compression, dynamically constructed user interface, automatic screen size detection, entry time data validation.
In addition to writing for Developer.com, she has written several books including Making Win 32 Applications Mobile. She has also written numerous articles on programming technology for national publications including Dr. Dobbs, BYTE Magazine, Microsoft Systems Journal, PC Magazine; Computer Shopper, Windows Sources and Databased Advisor.
# # #
Page 2 of 2