Compilation of singular menus in Delphi

Source: Internet
Author: User
Tags bmp image
Custom menus, text, lines/Delphi 4, 5
Custom menu, text, line/Delphi 4, 5
Fancy menus, etc.
Singular menu, etc.
Custom menus, rotated text, and special lines
Custom menu, rotating text, and special lines

Before Delphi 4, it was difficult to customize a menu (add a bitmap, change a font, etc .), because owner drawing (I. e. custom drawing)-although implemented by Windows-was not exposed by the tmainmenu class. since Delphi 4, however, this situation has been rectified, and we can have our way with menus.
Before Delphi 4, it was difficult to customize a menu (such as adding a BMP image and changing the font) because of the owner drawing event (that is, the custom drawing event) -It is executed in windows but does not appear in tmainmenu class. since Delphi 4,
This situation has changed, so we have the ability to customize menus.

This article will highlight some techniques you can use to customize the appearance of menus in your Delphi applications. we'll discuss text placement, menu sizing, font assignment, and using bitmaps and shapes to enhance a menu's appearance. just for fun, this article also features techniques for creating rotated text and custom lines. all of the techniques discussed in this article are demonstrat Ed in projects available for download.
This article will focus on some technical skills that can be used to define the menu shape in your Delphi application. we will discuss the placement of text, the menu size, the font setting, and the use of BMP files and shape controls to enhance the Display Effect of menus. For entertainment purposes alone, this article will also show up the techniques of rotating text and custom lines. All the techniques described in this article have been debugged in engineering files and can be downloaded online.
Custom fonts and sizes
Set Font and size
To create a custom menu, set the ownerdraw property of the menu component-tmainmenu or tpopupmenu-to true, and provide Event Handlers for its ondrawitem and onmeasureitem events. for example, an onmeasureitem event handler is declared like this:
To create a custom menu, set the ownerdraw attribute of the tmainmenu or tpopupmenu component to true, and create the ondrawitem and onmeasureitem event processes. For example, an onmeasureitem event can be declared as follows:

Procedure tform1.option1measureitem (Sender: tobject;
Acanvas: TCanvas; var width, height: integer );

Set the width and height variables to adjust the size of the menu item. the ondrawitem event handler is where all the hard work is done; it's where you draw your menu and make any special settings. to draw the menu option with Times New Roman font, for example, you shoshould do something like this:
Set the width and height variables of the menu items in the preceding event process to the appropriate size. all major events are triggered by the ondrawitem event. It is the place where you need to redraw the menu and make any special settings. For example, you can use the Times New Roman font to reproduce a menu item as follows:

Procedure tform1.times1drawitem (Sender: tobject;
Acanvas: TCanvas; arect: trect; selected: Boolean );
Begin
Acanvas. Font. Name: = 'times new Roman ';
Acanvas. textout (arect. Left + 1, arect. Top + 1,
(Sender as tmenuitem). Caption );
End;

This code is flawed, however. if it's run, the menu caption will be drawn aligned with the Left Border of the menu. this isn't default Windows behavior; usually, there's a space to put bitmaps and checkmarks in the menu. therefore, you shoshould calculate the space needed for this checkmark with code like that shown in Figure 1. figure 2 shows the resulting menu.
However, this code is flawed. If you run this code, the title (Caption) of the menu item is left aligned in the menu item. this is not the default behavior of windows. Generally, there is a space on the left of the menu to place BMP images and select the logo. Therefore, you should use the code to calculate how much space you need to place this selection flag, as shown in figure 1. Figure 2 shows the running effect of the menu.

Procedure tform1.times2drawitem (Sender: tobject;
Acanvas: TCanvas; arect: trect; selected: Boolean );
VaR
Dwcheck: integer;
Menucaption: string;
Begin
// Get the checkmark dimensions.
Obtain the number of shards required for the flag.
Dwcheck: = getsystemmetrics (sm_cxmenucheck );
// Adjust left position.
Adjust the left position
Arect. Left: = arect. Left + loword (dwcheck) + 1;
Menucaption: = (sender as tmenuitem). Caption;
// The font name is the menu caption.

Acanvas. Font. Name: = 'times new Roman ';
// Draw the text.
Draw text
Drawtext (acanvas. Handle, pchar (menucaption ),
Length (menucaption), arect, 0 );
End;

Figure 1: This ondrawitem event handler places menu item text correctly.
[The translator omitted all figures. The following is the same:]
Figure 2: A menu drawn with custom fonts.

If the text is too large to be drawn in the menu, Windows will cut it to fit. therefore, you shocould set the menu item size so all the text can be drawn. this is the role of the onmeasureitem event handler shown in figure 3.
If the text is too long, Windows will automatically crop the length to fit. Therefore, you should set the menu size so that all texts can be displayed. This is also true for the onmeasureitem event, as shown in figure 3.

Procedure tform1.times2measureitem (Sender: tobject;
Acanvas: TCanvas; var width, height: integer );
Begin
Acanvas. Font. Name: = 'times new Roman ';
Acanvas. Font. Style: = [];
// The width is the space of the menu check
This length is the length of the selection flag of the menu.
// Plus the width of the item text.
Add the length of a single dish
Width: = getsystemmetrics (sm_cxmenucheck) +
Acanvas. textwidth (sender as tmenuitem). Caption) + 2;
Height: = acanvas. textheight (
(Sender as tmenuitem). Caption) + 2;
End;

Figure 3: This onmeasureitem event handler insures that an item fits in its menu.

Custom shapes and bitmaps
Set graphics and bitmap
It's also possible to customize menu items by including bitmaps or other shapes. to add a bitmap, simply assign a bitmap file to the tmenuitem. bitmap property-with the object inspector at design time, or with code at run time. to draw colored rectangles as the caption of a menu item, you can use the ondrawitem event handler shown in Figure 4. figure 5 shows the result.
It is possible to set menus using bitmaps and other images. to add a bitmap, you only need to assign a BMP file to the bitmap attribute of tmenuitem in the object inspector during design, or assign values using code during runtime. To replace the menu title with a colored rectangle, you can use the ondrawitem event, as shown in figure 4. The result is displayed in Figure 5.

Procedure tform1.colordrawitem (Sender: tobject;
Acanvas: TCanvas; arect: trect; selected: Boolean );
VaR
Dwcheck: integer;
Menucolor: tcolor;
Begin
// Get the checkmark dimensions.
Dwcheck: = getsystemmetrics (sm_cxmenucheck );
Arect. Left: = arect. Left + loword (dwcheck );
// Convert the caption of the menu item to a color.
Convert the title of a menu item to color
Menucolor: =
Stringtocolor (sender as tmenuitem). Caption );
// Change the canvas brush color.
Change the paint brush color of the canvas.
Acanvas. Brush. Color: = menucolor;
// Draws the rectangle. If the item is selected,
Draw a rectangle if the menu item is selected
// Draw a border.
Draw borders
If selected then
Acanvas. Pen. Style: = pssolid
Else
Acanvas. Pen. Style: = psclear;
Acanvas. rectangle (arect. Left, arect. Top,
& Nb

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.