Home   Index   About
Ultimate Pack


Custom Search
Example of Menu-item Bitmaps

The example in this topic creates two menus, each containing several bitmap menu items. For each menu, the application adds a corresponding menu name to the main window's menu bar.

The first menu contains menu items showing each of three chart types: pie, line, and bar. The bitmaps for these menu items are defined as resources and are loaded by using the LoadBitmap function. Associated with this menu is a "Chart" menu name on the menu bar.

The second menu contains menu items showing each of the five line styles used with the CreatePen function: PS_SOLID, PS_DASH, PS_DOT, PS_DASHDOT, and PS_DASHDOTDOT. The application creates the bitmaps for these menu items at run time using GDI drawing functions. Associated with this menu is a "Lines" menu name on the menu bar.

Defined in the application's window procedure are two static arrays of bitmap handles. One array contains the handles of the three bitmaps used for the Chart menu. The other contains the handles of the five bitmaps used for the Lines menu. When processing the WM_CREATE message, the window procedure loads the chart bitmaps, creates the line bitmaps, and then adds the corresponding menu items. When processing the WM_DESTROY message, the window procedure deletes all of the bitmaps.

Following are the relevant portions of the application's header file.

// Menu-item identifiers

#define IDM_PIE 1

#define IDM_LINE 2

#define IDM_BAR 3

#define IDM_SOLID 4

#define IDM_DASH 5

#define IDM_DASHDOT 6

#define IDM_DASHDOTDOT 7

// Number of items on the Chart and Lines menus

#define C_LINES 5

#define C_CHARTS 3

// Bitmap resource identifiers

#define IDB_PIE 1

#define IDB_LINE 2

#define IDB_BAR 3

// Dimensions of the line bitmaps

#define CX_LINEBMP 40

#define CY_LINEBMP 10

Following are the relevant portions of the window procedure. The window procedure performs most of its initialization by calling the application-defined LoadChartBitmaps, CreateLineBitmaps, and AddBitmapMenu functions, described later in this topic.

LRESULT CALLBACK MainWindowProc(

HWND hwnd,

UINT uMsg,

WPARAM wParam,

LPARAM lParam

)

{

static HBITMAP aHbmLines[C_LINES];

static HBITMAP aHbmChart[C_CHARTS];

int i;

switch (uMsg) {

case WM_CREATE:

// Call application-defined functions to load the

// bitmaps for the Chart menu and create those for

// the Lines menu.

LoadChartBitmaps(aHbmChart);

CreateLineBitmaps(aHbmLines);

// Call an application-defined function to create

// menus containing the bitmap menu items. The function

// also adds a menu name to the window's menu bar.

AddBitmapMenu(

hwnd, // menu bar's owner window

"&Chart", // text of menu name on menu bar

IDM_PIE, // ID of first item on menu

aHbmChart, // array of bitmap handles

C_CHARTS // number of items on menu

);

AddBitmapMenu(hwnd, "&Lines", IDM_SOLID,

aHbmLines, C_LINES);

break;

case WM_DESTROY:

for (i = 0; i < C_LINES; i++)

DeleteObject(aHbmLines[i]);

for (i = 0; i < C_CHARTS; i++)

DeleteObject(aHbmChart[i]);

PostQuitMessage(0);

break;

.

. // Process additional messages here.

.

default:

return (DefWindowProc(hwnd, uMsg, wParam, lParam));

}

return 0;

}

The application-defined LoadChartBitmaps function loads the bitmap resources for the chart menu by calling the LoadBitmap function, as follows.

VOID WINAPI LoadChartBitmaps(HBITMAP *paHbm)

{

paHbm[0] = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB_PIE));

paHbm[1] = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB_LINE));

paHbm[2] = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB_BAR));

}

The application-defined CreateLineBitmaps function creates the bitmaps for the Lines menu by using GDI drawing functions. The function creates a memory device context (DC) with the same properties as the desktop window's DC. For each line style, the function creates a bitmap, selects it into the memory DC, and draws in it.

VOID WINAPI CreateLineBitmaps(HBITMAP *paHbm)

{

HWND hwndDesktop = GetDesktopWindow();

HDC hdcDesktop = GetDC(hwndDesktop);

HDC hdcMem = CreateCompatibleDC(hdcDesktop);

COLORREF clrMenu = GetSysColor(COLOR_MENU);

HBRUSH hbrOld;

HPEN hpenOld;

HBITMAP hbmOld;

int fnDrawMode;

int i;

// Create a brush using the menu background color,

// and select it into the memory DC.

hbrOld = SelectObject(hdcMem, CreateSolidBrush(clrMenu));

// Create the bitmaps. Select each one into the memory

// DC that was created and draw in it.

for (i = 0; i < C_LINES; i++) {

// Create the bitmap and select it into the DC.

paHbm[i] = CreateCompatibleBitmap(hdcDesktop,

CX_LINEBMP, CY_LINEBMP);

hbmOld = SelectObject(hdcMem, paHbm[i]);

// Fill the background using the brush.

PatBlt(hdcMem, 0, 0, CX_LINEBMP, CY_LINEBMP, PATCOPY);

// Create the pen and select it into the DC.

hpenOld = SelectObject(hdcMem,

CreatePen(PS_SOLID + i, 1, RGB(0, 0, 0)));

// Draw the line. To preserve the background color where

// the pen is white, use the R2_MASKPEN drawing mode.

fnDrawMode = SetROP2(hdcMem, R2_MASKPEN);

MoveToEx(hdcMem, 0, CY_LINEBMP / 2, NULL);

LineTo(hdcMem, CX_LINEBMP, CY_LINEBMP / 2);

SetROP2(hdcMem, fnDrawMode);

// Delete the pen, and select the old pen and bitmap.

DeleteObject(SelectObject(hdcMem, hpenOld));

SelectObject(hdcMem, hbmOld);

}

// Delete the brush and select the original brush.

DeleteObject(SelectObject(hdcMem, hbrOld));

// Delete the memory DC and release the desktop DC.

DeleteDC(hdcMem);

ReleaseDC(hwndDesktop, hdcDesktop);

}

The application-defined AddBitmapMenu function creates a menu and adds the specified number of bitmap menu items to it. Then it adds a corresponding menu name to the specified window's menu bar.

VOID WINAPI AddBitmapMenu(

HWND hwnd, // window that owned the menu bar

LPSTR lpszText, // text of menu name on menu bar

UINT uID, // ID of first bitmap menu item

HBITMAP *paHbm, // bitmaps for the menu items

int cItems) // number bitmap menu items

{

HMENU hmenuBar = GetMenu(hwnd);

HMENU hmenuPopup = CreatePopupMenu();

MENUITEMINFO mii;

int i;

// Add the bitmap menu items to the menu.

for (i = 0; i < cItems; i++) {

mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_DATA;

mii.fType = MFT_BITMAP;

mii.wID = uID + i;

mii.dwTypeData = (LPSTR) (paHbm[i]);

InsertMenuItem(hmenuPopup, i, TRUE, &mii);

}

// Add a menu name to the menu bar.

mii.fMask = MIIM_TYPE | MIIM_DATA | MIIM_SUBMENU;

mii.fType = MFT_STRING;

mii.hSubMenu = hmenuPopup;

mii.dwTypeData = lpszText;

InsertMenuItem(hmenuBar,

GetMenuItemCount(hmenuBar), TRUE, &mii);

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun