DELPHI的奇異菜單的編寫

來源:互聯網
上載者:User
Custom Menus, Text, Lines / Delphi 4, 5
自訂菜單,文本,線/ Delphi 4, 5
Fancy Menus, etc.
奇異菜單,等等
Custom Menus, Rotated 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.
在Delphi 4之前,要想自訂一個菜單是困難的(例如加上一個BMP映像,改變字型等),因為owner drawing事件(也就是custom drawing事件)-雖然是由Windows來執行,但是卻並不在TMainMenu class中出現.自從Delphi 4開始後,
這種情況有了改變,我們於是有了可以自訂菜單的功能了.

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 demonstrated in projects available for download。
這篇文章將主要著重論述可以用來自訂你的DELPHI應用程式中的菜單的外形的一些技術巧.我們將論述文本的放置,菜單的大小,字型的設定,以及用BMP檔案和SHAPE控制項來加強菜單的顯示效果。僅僅出於娛樂的目的,這篇文章也將對旋轉的文本和自訂線條的技巧進行特寫。這篇文章所論述到的所有技巧都已在工程檔案中通過了調試並且可以到網上下載這些工程檔案。
Custom Fonts and Sizes
設定字型和大小
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:
為了建立一個自訂的菜單,將TmainMenu或TpopupMenu組件的OwnerDraw屬性設為TRUE,並且建立它的OnDrawItem和OnMeasureItem的事件程序。例如,一個OnMeasureItem事件程序可以聲明如下:

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 should do something like this:
設定上面事件程序中的功能表項目的Width 和Height變數到合適的大小.所有主要的事情都要由OnDrawItem事件來觸發;它是你要重畫菜單和作任何特殊設定的地方。舉例,為了用Times New Roman字型來重畫功能表項目,你可以如下面這樣做:

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 should calculate the space needed for this checkmark with code like that shown in Figure 1. Figure 2 shows the resulting menu.
然而這段代碼是有缺陷的。如果運行這段代碼,功能表項目的標題(caption)會在功能表項目中靠左對齊.這並不是Windows的預設行為,通常,在菜單左邊那兒有一個空間用來放置BMP映像和選擇標誌的。因此,你應該用代碼計算要多少空間來放置這個選擇標誌的,就象Figure 1中顯示的那樣。Figure 2顯示的是菜單的運行效果。

procedure TForm1.Times2DrawItem(Sender: TObject;
  ACanvas: TCanvas; ARect: TRect; Selected: Boolean);
var
  dwCheck : Integer;
  MenuCaption : string;
begin
  // Get the checkmark dimensions.
擷取選擇標誌所需的像素數
  dwCheck := GetSystemMetrics(SM_CXMENUCHECK);
  // Adjust 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.
畫文本
  DrawText(ACanvas.Handle, PChar(MenuCaption),
           Length(MenuCaption), ARect, 0);
end;

Figure 1: This OnDrawItem event handler places menu item text correctly.
[譯者省略掉所有的FigureS,以下同樣]
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 should 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.
如果文本太長,Windows會自動裁剪長度來合適。因此,你應該設定菜單大小使所有的文本都可以顯示出來。在OnMeasureItem事件中也應如此,這在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
這個長度是菜單的選擇標誌的長度
  // plus the width of the item text.
再加上功能表項目的長度
  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
設定圖形和位元影像
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 could use the OnDrawItem event handler shown in Figure 4. Figure 5 shows the result.
用位元影像和其它圖形來設定菜單是可能的事.要想添加一個位元影像,只需在設計時簡單地在Object Inspector中把一個BMP檔案賦給TmenuItem的Bitmap屬性即可,或者運行時用代碼賦值也可以。要想用一個有顏色的矩形來代替功能表標題,你可以使用OnDrawItem事件,例如在Figure 4中顯示的那樣。在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.
將功能表項目的標題轉換為顏色
  MenuColor :=
    StringToColor((Sender as TMenuItem).Caption);
  // Change the canvas brush color.
改變畫布canvas的畫筆顏色
  ACanvas.Brush.Color := MenuColor;
  // Draws the rectangle. If the item is selected,
畫矩形,如果功能表項目是被選擇的
  // draw a border.
畫邊框
   if Selected then
    ACanvas.Pen.Style := psSolid
   else
    ACanvas.Pen.Style := psClear;
  ACanvas.Rectangle(ARect.Left, ARect.Top,
   &nb

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.