Search Pocket PC Life's 126 Pocket PC-related article archive 
Home
EasyPrint
News details Click here for the RSS feed's XML code. This is not a browser URL.
Articles-only Click here for the RSS feed's XML code. This is not a browser URL.
Freeing up DLLs, avoiding suspend mode, and listbox tricks (continued)

As the saying goes, it's a feature not a bug. In fact, this "feature" is present in all releases of Windows CE, Windows NT, Windows 95 and Windows 98. The problem is that, by default, a listbox does not update its horizontal extent as strings are added and removed. The horizontal extent is simply the logical unit width of a string when it's drawn with a particular font. Fortunately, if you manually update a listbox with the maximum horizontal extent of the strings it currently contains, the horizontal scrollbar will magically appear. Here's come code that shows how to do this:

/*
This function updates the horizontal extent for hwndList so that a horizontal scroll bar will show up and work properly.
*/
void UpdateHorzExtent(HWND hwndList)
{
HDC hdc = GetDC(hwndList);
HFONT hFont = (HFONT)SendMessage(hwndList, WM_GETFONT, 0, 0);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);

int iLongestExtent = 0;
int iCount = ListBox_GetCount(hwndList);
TCHAR chBuf[MAX_STRING_LENGTH];

for ( int i = 0; i < iCount; i++ )
{
ListBox_GetText(hwndList, i, chBuf);

SIZE size;
GetTextExtentPoint32(hdc, chBuf, _tcslen(chBuf), &size);

size.cx += 6; // fudge factor

if ( size.cx > iLongestExtent )
iLongestExtent = size.cx;
}

ListBox_SetHorizontalExtent(hwndList, iLongestExtent);

SelectObject(hdc, hOldFont);
ReleaseDC(hwndList, hdc);
}

The first thing we do is get an HDC for the window and select the current font into it. We then walk through the strings in the listbox, calculate the size of each with the GetTextExtentPoint32 API, and record the maximum. A "fudge factor" of six units is added to the width to account for a small right hand margin between the end of the text and the edge of the listbox. After we drop out of the loop, the listbox is updated with a call to ListBox_SetHorizontalExtent and we release the HDC after resetting the original font handle.

There are a couple of drawbacks to UpdateHorzExtent. It's up to you, the developer, to make sure it gets called after each string is inserted or deleted. This could be automated by subclassing the listbox or using a C++ class wrapper. The other problem is that the code assumes that no string in the listbox is bigger than the MAX_STRING_LENGTH constant. An industrial strength application would probably want to dynamically allocate the memory to make sure that chBuf never overflows.

I'm trying to copy some text to the clipboard, but it doesn't seem to work. How come?
Edit controls have built-in capability to perform cut, copy, and paste actions by responding to the WM_CUT, WM_COPY, and WM_PASTE messages, respectively, but it's also possible to manually put data on the clipboard and retrieve it. Unfortunately, the Windows CE implementation of the clipboard APIs has some quirks.

Although the documentation states that the OpenClipboard API allows a NULL parameter, this doesn't work for Windows CE. The call, and all subsequent clipboard calls, will still return TRUE, but the data will not end up on the clipboard. Instead, Windows CE requires a valid window handle for the OpenClipboard call and, additionally, the window must have been created by the process calling OpenClipboard. If the window was created by another process, you can put data on the clipboard, but any paste operation to remove it will cause a series of access violation exceptions and require a warm boot of the device.


« Previous  ·  1  ·  2  ·  3  ·  4  ·  Next »
Other articles you might like
Home > Phones and PDAs > Windows Mobile > Programming (3 articles)
   HP hotkeys, OK buttons, and file existence
   Palm-sized input panel quirks and mounted database volumes
Get Weekly Email Updates
Subscribe to our regular weekly email newsletter. It's packed with tips, reviews, deep analysis, and the latest news.
 
More from the ZATZ journals
Computing Unplugged: Smartphone smarts for a mobile world
David Gewirtz Online: CNN commentary and analysis
DominoPower: It's time for Lotus to double-down on Linux and open source
OutlookPower: The strange case of Outlook losing notes and requiring passwords
-- Advertisement --

NO HASSLE PHOTO PRINTING, SHARING, AND STORAGE -- AS LOW AS $2.54 PER MONTH
Discover an easier way to share, print and manage your photos online! Get your own online photo album site for sharing photos, as well as easy-to-use editing tools to make sure your photos look their very best. You can even order high quality prints directly from your album -- and have them delivered right to your door!

Best of all, you can also get login-free photo sharing at your personal domain name (if you have one), so your friends and family don't have to hassle with signing up or logging in just to view your pictures. It's the perfect solution for sharing, printing and storing all your favorite images!

And it's only from The Duck! Tap here to get started.

-- Advertisement --

Printing emails and attachments has never been simpler
When it comes to printing emails or attachments, you can be confident that our Auto-Print add-in can do what Outlook lacks - print the emails and/or attachments as soon as they arrive.

Discover this professional tool today.
ZATZ Home  ·  News  ·  Back Issues  ·  Credits/Trademarks ·  Link To Us
Copyright © 1999-2010, ZATZ Publishing. All rights reserved worldwide.
Editor's Login