Home   Index   About
Ultimate Pack


Custom Search
Adding Lines and Graphs to a Menu

The following code sample shows how to create a menu that contains menu-item bitmaps. It creates two menus. The first is a Chart menu that contains three menu-item bitmaps: a pie chart, a line chart, and a bar chart. The example demonstrates how to load these bitmaps from the application's resource file, and then use the CreatePopupMenu and AppendMenu functions to create the menu and menu items.

The second menu is a Lines menu. It contains bitmaps showing the line styles provided by the predefined pen in Windows. The line-style bitmaps are created at run time by using GDI functions.

Here are the definitions of the bitmap resources in the application's resource-definition file.

PIE BITMAP pie.bmp

LINE BITMAP line.bmp

BAR BITMAP bar.bmp

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

// Menu-item identifiers

#define IDM_SOLID PS_SOLID

#define IDM_DASH PS_DASH

#define IDM_DASHDOT PS_DASHDOT

#define IDM_DASHDOTDOT PS_DASHDOTDOT

#define IDM_PIE 1

#define IDM_LINE 2

#define IDM_BAR 3

// Line-type flags

#define SOLID 0

#define DOT 1

#define DASH 2

#define DASHDOT 3

#define DASHDOTDOT 4

//Count of pens

#define CPENS 5

// Chart-type flags

#define PIE 1

#define LINE 2

#define BAR 3

// Function prototypes

LRESULT APIENTRY MainWndProc(HWND, UINT, WPARAM, LPARAM);

VOID MakeChartMenu(HWND);

VOID MakeLineMenu(HWND, HPEN, HBITMAP);

The following example shows how menus and menu-item bitmaps are created in an application.

LRESULT APIENTRY MainWndProc(hwnd, uMsg, wParam, lParam)

HWND hwnd;

UINT uMsg;

WPARAM wParam;

LPARAM lParam;

{

static HPEN hpen[CPENS];

static HBITMAP hbmp[CPENS];

int i;

switch (uMsg) {

case WM_CREATE:

// Create the Chart and Line menus.

MakeChartMenu(hwnd);

MakeLineMenu(hwnd, hpen, hbmp);

return 0;

.

. // Process other window messages.

.

case WM_DESTROY:

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

DeleteObject(hbmp[i]);

DeleteObject(hpen[i]);

}

PostQuitMessage(0);

break;

default:

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

}

return NULL;

}

VOID MakeChartMenu(hwnd)

HWND hwnd; // handle of owner window

{

HBITMAP hbmpPie; // handle of pie chart bitmap

HBITMAP hbmpLine; // handle of line chart bitmap

HBITMAP hbmpBar; // handle of bar chart bitmap

HMENU hmenuMain; // handle of main menu

HMENU hmenuChart; // handle of Chart menu

// Load the pie, line, and bar chart bitmaps from the

// resource-definition file.

hbmpPie = LoadBitmap(hinst, MAKEINTRESOURCE(PIE));

hbmpLine = LoadBitmap(hinst, MAKEINTRESOURCE(LINE));

hbmpBar = LoadBitmap(hinst, MAKEINTRESOURCE(BAR));

// Create the Chart menu and add it to the menu bar.

// Append the Pie, Line, and Bar menu items to the Chart

// menu.

hmenuMain = GetMenu(hwnd);

hmenuChart = CreatePopupMenu();

AppendMenu(hmenuMain, MF_STRING | MF_POPUP, (UINT) hmenuChart,

"Chart");

AppendMenu(hmenuChart, MF_BITMAP, IDM_PIE, (LPCTSTR) hbmpPie);

AppendMenu(hmenuChart, MF_BITMAP, IDM_LINE,

(LPCTSTR) hbmpLine);

AppendMenu(hmenuChart, MF_BITMAP, IDM_BAR, (LPCTSTR) hbmpBar);

return;

}

VOID MakeLineMenu(hwnd, phpen, phbmp)

HWND hwnd;

HPEN *phpen;

HBITMAP *phbmp;

{

HMENU hmenuLines; // handle of Lines menu

HMENU hmenu; // handle of main menu

COLORREF crMenuClr; // menu-item background color

HBRUSH hbrBackground; // handle of background brush

HBRUSH hbrOld; // handle of previous brush

LONG lCheckXY; // dimensions of check-mark bitmap

WORD wLineX; // width of line bitmaps

WORD wLineY; // height of line bitmaps

HDC hdcMain; // handle of main window's DC

HDC hdcLines; // handle of compatible DC

HBITMAP hbmpOld; // handle of previous bitmap

int i; // loop counter

// Create the Lines menu. Add it to the menu bar.

hmenu = GetMenu(hwnd);

hmenuLines = CreatePopupMenu();

AppendMenu(hmenu, MF_STRING | MF_POPUP,

(UINT) hmenuLines, "&Lines");

// Create a brush for the menu-item background color.

crMenuClr = GetSysColor(COLOR_MENU);

hbrBackground = CreateSolidBrush(crMenuClr);

// Create a compatible device context for the line bitmaps,

// and then select the background brush into it.

hdcMain = GetDC(hwnd);

hdcLines = CreateCompatibleDC(hdcMain);

hbrOld = SelectObject(hdcLines, hbrBackground);

// Get the dimensions of the check-mark bitmap. The width of

// the line bitmaps will be five times the width of the

// check-mark bitmap.

lCheckXY = GetMenuCheckMarkDimensions();

wLineX = LOWORD(lCheckXY) * (WORD) 5;

wLineY = HIWORD(lCheckXY);

// Create the bitmaps and select them, one at a time, into the

// compatible device context. Initialize each bitmap by

// filling it with the menu-item background color.

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

phbmp[i] = CreateCompatibleBitmap(hdcMain, wLineX, wLineY);

if (i == 0)

hbmpOld = SelectObject(hdcLines, phbmp[i]);

else

SelectObject(hdcLines, phbmp[i]);

ExtFloodFill(hdcLines, 0, 0, crMenuClr, FLOODFILLBORDER);

}

// Create the pens.

phpen[0] = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));

phpen[1] = CreatePen(PS_DOT, 1, RGB(0, 0, 0));

phpen[2] = CreatePen(PS_DASH, 1, RGB(0, 0, 0));

phpen[3] = CreatePen(PS_DASHDOT, 1, RGB(0, 0, 0));

phpen[4] = CreatePen(PS_DASHDOTDOT, 1, RGB(0, 0, 0));

// Select a pen and a bitmap into the compatible device

// context, draw a line into the bitmap, and then append

// the bitmap as an item in the Lines menu.

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

SelectObject(hdcLines, phbmp[i]);

SelectObject(hdcLines, phpen[i]);

MoveToEx(hdcLines, 0, wLineY / 2, NULL);

LineTo(hdcLines, wLineX, wLineY / 2);

AppendMenu(hmenuLines, MF_BITMAP, i + 1,

(LPCTSTR) phbmp[i]);

}

// Release the main window's device context and destroy the

// compatible device context. Also, destroy the background

// brush.

ReleaseDC(hwnd, hdcMain);

SelectObject(hdcLines, hbrOld);

DeleteObject(hbrBackground);

SelectObject(hdcLines, hbmpOld);

DeleteDC(hdcLines);

return;

}


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