Use a bitmap menu item - Add a line and graphics to the menu

zhaozj2021-02-08  226

Add lines and graphics to menus

The following code example shows how to create a menu that contains bitmaps. It has built two menus. The first is a chart menu containing three bitmaps: a circular chart, a linear chart, and a bar chart. This example illustrates how to load these bitmaps from the application's resource file, and how to use the createPopUpMenu and the AppendMenu function to create a new menu and menu item.

The second menu is a linear menu. It contains bitmaps that displays a line-style style (if these are system-defined) line-style bitmaps are newly created using the GDI function at runtime.

This is a bitmap resource defined in the application resource definition file.

PIE Bitmap Pie.BMP

Line Bitmap line.bmp

Bar bitmap bar.bmp

This is the relevant part of the application header file.

// Menu item identifier

#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 tag

#define solid 0

#define dot 1

#define dash 2

#define dashdot 3

#define dashdotdot 4

// Brush is big

#define cpens 5

/ / Chart Type Tag

#define Pie 1

#define line 2

#define bar 3

// Function prototype

LResult Apientry MainWndProc (HWND, UINT, WPARAM, LPARAM);

Void makechartMenu (hwnd);

Void MakelineMenu (HWND, HPEN, HBITMAP);

The following example shows how to create a new menu and menu item in the application.

LResult Apientry MainWndProc (HWND HWND, UINT UMSG, WPARAM WPARAM, LPARAM LPARAM)

{

Static HPEN HPEN [CPENS];

Static Hbitmap HBMP [CPENS];

INT I;

Switch (UMSG)

{

Case WM_CREATE:

// New chart and line menu

MakechartMenu (hwnd);

MakelineMenu (HWND, HPEN, HBMP);

Return 0;

// Handle additional window message

Case WM_DESTROY:

For (i = 0; i

{

DeleteObject (HBMP [i]);

DeleteObject (HPEN [I]);

}

PostquitMessage (0);

Break;

DEFAULT:

Return DefwindowProc (HWND, UMSG, WPARAM, LPARAM);

}

Return NULL;

}

Void MakechartMenu (HWND HWND)

{

Hbitmap HBMPPIE; / / Circular Statistical Piration Event Fiber

Hbitmap hbmpline; // Line-shaped chart horizon

Hbitmap hbmpbar; // bar chart level image segment

HMenu Hmenumain; // Main menu handle

HMENU HMENUCHART; / / Chart Menu Handle

/ / Load graphics, linear and bar charts from the resource file

HBMPPIE = LoadBitmap (Hinst, MakeintResource (PIE));

HBMPLINE = loadingBitmap (Hinst, makeintresource (line)); hbmpbar = loadingBitmap (Hinst, makeintResource (bar));

// New Chart menu and add it to the menu bar

// Additional circular, linear and strip menu to the chart menu.

HMenumain = GetMenu (hwnd);

HMenuchart = CreatePopupmenu ();

Appendmenu (HMenumain, MF_String | MF_POPUP, (UINT) HMenuchart,

"CharT");

Appendmenu (HMenuchart, MF_Bitmap, IDM_PIE, (LPCTSTSTSTSTSTSTSTS HBMPPIE);

Appendmenu (HMenuchart, MF_Bitmap, IDM_LINE,

(Lpctstr) HBMPLINE

Appendmenu (HMenuchart, MF_Bitmap, IDM_Bar, (LPCTSTSTSTSTS HBMPBAR);

Return;

}

Void MakelineMenu (HWnd Hwnd, HPEN PHPEN, HBITMAP PHBMP)

{

HMENU HMENULINES; // Line-shaped menu handle

HMENU HMENU; // Main menu handle

ColorRef crMenuclr; // Menu Bar Background Color

Hbrush hbrbackground; // Background brush handle

Hbrush Hbrold; // Early brush handle

Word WLINEX; // Wide line

Word WLINEY; / / High line bitmap

HDC HDCMAIN; // Main window DC handle

HDC HDClines; // Compatible with DC handle

Hbitmap hbmpold; // earlier first graph section

INT i; // cycle counter

// New line menu. Add it to the menu bar

HMENU = GetMenu (hwnd);

Hmenulines = creagepopupmenu ();

Appendmenu (HMENU, MF_STRING | MF_POPUP,

(Uint) HMENULINES, "& LINES");

// Enjoy a brush for the menu item background color.

CRMENUCLR = GetSysColor (Color_Menu);

HbRBackground = Createsolidbrush (CRMENUCLR);

/ / For a new compatible device descriptor for line-shaped bitmaps,

/ / And select the background brush in it.

HDCMAIN = Getdc (hwnd);

HDClines = CreateCompatibleDC (HDCMain);

Hbrold = SelectObject (HDClines, HBRBackground);

// Get the size of the check mark bitmap.

// Width of the line shadow map will be five times that of the check mark bit

WLINEX = GetSystemMetrics (SM_CXMENUCHECK) * (Word) 5;

WLINEY = GetSystemMetrics (SM_CYMENUCHECK);

// New bitmap in the device descriptor and select them, once one,

// Fill it with a menu item background to initialize each bitmap.

For (i = 0; 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);

}

// New brush pen

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 brush and a bitmap in the compatible device descriptor.

// Draw a line shape in the bitmap and apply this bitmap to the line menu as an item.

For (i = 0; 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 device descriptor of the main window and destroy the compatible device descriptor. Similarly, destruction background brush

ReleaseDC (hwnd, hdcmain);

SelectObject (HDClines, HBrold);

DeleteObject (HBRBACKGROUND);

SelectObject (HDClines, HBMPOLD);

Deletedc (HDClines);

Return;

}

转载请注明原文地址:https://www.9cbs.com/read-3424.html

New Post(0)