Bit chart menu example
This instance has created two menus, each with several bitmap menu items. For each menu, the application adds the corresponding menu name to the menu bar of the main window.
First menu contains three menu items for three chart types: round, linear and strip. This menu item bitmap is used as a resource definition, and is loaded through the LoadBitmap function. Assigned in the menu bar in the "Chart" menu name.
The second menu contains five line-style menu items that use the Createpen function: ps_solid, ps_dash, ps_dot, ps_dashdot, and ps_dashdotdot to be established. The application uses the GDI painting function when running to new bitmaps. Associate in the "Lines" menu name in the menu bar.
Two static arrays of bitmap handles are defined in the application's window process. An array contains the handle of the three bitmaps used for the chart menu. Another array contains the handle of the five bitmaps used for the linear menu. When processed the WM_CREATE message, the window process is loaded into the chart, the new line graph, and add them to the corresponding menu item. When processed the WM_DESTROY message, the window process deletes all bitmaps.
The following is part of the header file of the corresponding application.
// Menu item identifier
#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 line menu
#define c_lines 5
#define c_charts 3
// bitmap resource identifier
#define IDB_PIE 1
#define IDB_LINE 2
#define idb_bar 3
// Line bit map size
#define CX_LineBMP 40
#define CY_LineBMP 10
The following is the corresponding part of the window process. The window process is initialized by calling the application-defined LoadChartBitMaps, CreateLineBitmaps, and AddbitMapMenu functions.
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 the application defined function to install the bitmap for the chart menu and build those on the line menu
LoadchartBitmaps (AhbmChart);
CreateLineBitmaps (Ahbmlines);
// Call the application defined function to create a menu that contains the bitmap menu item.
// The function also adds a menu name to the window to the window.
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 DeleteObject (Ahbmlines [i]); For (i = 0; i DeleteObject (AhbmChart [i]); PostquitMessage (0); Break; // Treat additional news in this DEFAULT: Return (DEFWindowProc (HWND, UMSG, WPARAM, LPARAM); } Return 0; } The LoadChartBitMaps function defined by the application is installed into the bitmap 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 is a new bitmap using the GDI drawing function. This function creates a memory device descriptor using the same attribute as the device descriptor with the desktop window. For each line style, a function will create a bitmap, select it in the memory device descriptor and draw it. Void WinApi CreatelineBitmaps (Hbitmap * PAHBM) { HWND HWNDDESKTOP = GetDesktopWindow (); HDC hdcdesktop = getdc (hwndesktop); HDC HDCMEM = CREATECOMPATIBLEDC (HDCDESKTOP); ColorRef CLRMENU = GetSysColor (Color_Menu); Hbrush hbrold; HPEN HPENOLD; Hbitmap hbmold; Int fndrawmode; INT I; // Use the menu background color to create a brush and select it in the memory device descriptor. HBrold = SelectObject (HDCMEM, Createsolidbrush (CLRMENU)); // Create a bitmap. Select each of the memory device descriptors to create and draw it. For (i = 0; i { // New bitmap and select it in the device descriptor PAHBM [I] = CREATECOMPATIBLEBITMAP (HDCDESKTOP, CX_LineBMP, CY_LineBMP; HBmold = SELECTOBJECT (HDCMEM, PAHBM [I]); // Fill the background with a brush PATBLT (HDCMEM, 0, 0, CX_LineBMP, CY_LINEBMP, PATCOPY); / / New brush and select it in the device descriptor Hpenold = SELECTOBJECT (HDCMEM, Createpen (PS_SOLID I, 1, RGB (0, 0, 0))); // Draw line shape. Go to save the background color and set the brush as white, use the R2_Maskpen to draw 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 brush and select the old brush and bitmap. DeleteObject (SELECTOBJECT (HDCMEM, HPENOLD); SelectObject (HDCMEM, HBMOLD); } / / Delete the brush and select the original brush. DeleteObject (SelectObject (HDCMEM, HBrOLD); / / Delete the memory device descriptor and release the desktop device descriptor Deletedc (HDCMEM); ReleaseDC (HWNDDESKTOP, HDCDESKTOP); } The application-defined AddbitMapMenu function creates a menu and adds a bitmap menu item that specifies the number to it. At this point it adds a corresponding menu name to the specified window menu bar. Void WinApi AddbitMapmenu HWND HWND, // Menu Bar Owner Window Handle LPSTR LPSZTEXT, // Text on the menu bar UINT UID, / / The ID of the first bitmap menu item Hbitmap * pahbm, / / bitmap for menu items INT CITEMS) // Bit Chart Menu Number { HMENU HMENUBAR = GetMenu (hwnd); HMENU HMENUPOPUP = CREATEPOPUPMENU (); Menuiteminfo MII; INT I; // Add bitmap menu item to the menu For (i = 0; 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; mi.hsubmenu = HMenupopup; mii.dwtypedata = lpsztext; InsertMenuItem (HMenubar, GetMenuitemcount (Hmenubar), True, & Mii); }