Matt's Mind

Wednesday, September 29, 2004

I love (to chuckle at) Win32

This is a work of beauty (taken from this article by Raymond Chen). The code below is handling popup menu selections in order to show help text in the status bar (actually the window title in the example).
void OnMenuSelect(HWND hwnd, HMENU hmenu,

int item, HMENU hmenuPopup, UINT flags)
{
if (g_pcm && item >= SCRATCH_QCM_FIRST &&
item <= SCRATCH_QCM_LAST)
{
WCHAR szBuf[MAX_PATH];

if (FAILED(IContextMenu_GetCommandString(g_pcm,
item - SCRATCH_QCM_FIRST,
GCS_HELPTEXTW, NULL, szBuf, MAX_PATH)))
{
lstrcpynW(szBuf, L"No help available.", MAX_PATH);
}
SetWindowTextW(hwnd, szBuf);
}
}
Ah, so wonderfully readable and obvious (see Raymond's post for more crunchy C/Win32 goodness). No wonder people actually like VB given that this sort of madness is the alternative. And this is from article 6 in a series of how to manage popup menus in Windows. Eeeek!

0 Comments:

Post a Comment

<< Home