Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OwnerDraw for TPopupMenu

90 views
Skip to first unread message

Michael Gressman

unread,
May 7, 2003, 4:28:38 PM5/7/03
to
I've used the attached code below from Peter Below to change the
colors of my popup menu and its menu items, however, mnu.caption in
the DrawMenuItem method always contains a leading ampersand(&), even
if I didn't use one for an accelerator character. This code works
great and I really want to use it, but I can't as long as the caption
property always prints with the leading ampersand. Can anyone explain
why that is?

Certainly:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
Menus;

type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
Arial10pt1: TMenuItem;
Arial12pt1: TMenuItem;
Arial14pt1: TMenuItem;
Arial18pt1: TMenuItem;
procedure MeasureMenuitem(Sender: TObject; ACanvas: TCanvas; var
Width,
Height: Integer);
procedure DrawMenuItem(Sender: TObject; ACanvas: TCanvas; ARect:
TRect;
Selected: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MeasureMenuitem(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer);
var
mnu: TMenuitem;
begin
mnu := Sender As TMenuitem;
aCanvas.font.name := 'Arial';
aCanvas.font.size := mnu.tag;
width := aCanvas.TextWidth( mnu.caption )+10;
height := aCanvas.TextHeight( mnu.caption ) + 4;
end;

procedure TForm1.DrawMenuItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean);
var
mnu: TMenuitem;
begin
mnu := Sender As TMenuitem;
aCanvas.font.name := 'Arial';
aCanvas.font.size := mnu.tag;
If selected then begin
aCanvas.font.color := clRed;
aCanvas.brush.color := clAqua;
end
else begin
aCanvas.font.color := clRed;
aCanvas.brush.color := $C0FFFF;
end;
acanvas.brush.style := bsSolid;
aCanvas.fillrect( aRect );
acanvas.textrect( aRect, arect.left+4, arect.top+2, mnu.caption );
end;

end.

{
object Form1: TForm1
Left = 192
Top = 128
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PopupMenu = PopupMenu1
PixelsPerInch = 120
TextHeight = 16
object PopupMenu1: TPopupMenu
OwnerDraw = True
Left = 20
Top = 48
object Arial10pt1: TMenuItem
Tag = 10
Caption = 'Arial 10pt'
OnDrawItem = DrawMenuItem
OnMeasureItem = MeasureMenuitem
end
object Arial12pt1: TMenuItem
Tag = 12
Caption = 'Arial 12 pt'
OnDrawItem = DrawMenuItem
OnMeasureItem = MeasureMenuitem
end
object Arial14pt1: TMenuItem
Tag = 14
Caption = 'Arial 14 pt'
OnDrawItem = DrawMenuItem
OnMeasureItem = MeasureMenuitem
end
object Arial18pt1: TMenuItem
Tag = 18
Caption = 'Arial 18 pt'
OnDrawItem = DrawMenuItem
OnMeasureItem = MeasureMenuitem
end
end
end
}

0 new messages