Windows菜单可以分为系统菜单、主菜单、弹出菜单三种。
//1、在FormCreate中往系统菜单中添加自己的菜单
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
i := GetSystemMenu(Handle,False);
AppendMenu(i,mf_separator,0,nil); //添加分割线
AppendMenu(i,mf_string,100,'我的菜单(&E)'); //添加我的菜单
end;
//2、设置自己定义的菜单的响应事件
声明为: procedure sysmenu(var msg: TWMMENUSELECT); Message wm_MENUSELECT;
procedure TForm1.sysmenu(var msg:TWMMENUSELECT);
begin
if msg.IDItem = 100 then
ShowMessage('我的菜单')
else
inherited;
end;
//1、点击【打开】按钮,选择文件
procedure TForm1.pmiN2Click(Sender: TObject);
var
TempMItem: TmenuItem;
begin
TempMItem := TmenuItem.Create(nil);
if DlgOpen1.Execute then
begin
TempMItem.Caption := DlgOpen1.FileName;
//往【主菜单】中的【第一列菜单组】中的【第三行】插入新菜单项
mm1.Items.Items[0].Insert(2,TempMItem);
end;
end;
//2、设置相应的点击事件
procedure TForm1.mm1Change(Sender: TObject; Source: TMenuItem;
Rebuild: Boolean);
var
i: Integer;
begin
//为新建的菜单指定事件
for i:=2 to mm1.Items.Items[0].Count - 3 do
begin
mm1.Items.Items[0].Items[i].OnClick := Btn1.Onclick;
end;
end;
//3、响应菜单的事件
procedure TForm1.btn1Click(Sender: TObject);
begin
ShowMessage(TMenuItem(Sender).Caption);
end;
//1、pm1弹出菜单中添加pm2弹出菜单的所有项
procedure TForm1.btn2Click(Sender: TObject);
begin
pm1.Items.Add(pm2.Items);
end;
//2、在当前按钮的位置显示弹出菜单,主要是坐标的编写
procedure TForm1.btn3Click(Sender: TObject);
begin
pm1.Popup(Form1.Left + btn3.Left,Form1.Top + btn3.Top+ btn3.Height);
end;
//1、设置私有变量Private中添加MBitmap :TbitMap;
//2、加载图片
procedure TForm1.FormCreate(Sender: TObject);
begin
MBitmap := TBitmap.Create;
MBitmap.LoadFromFile(ExtractFileDir(Application.ExeName)+ '\pic\2.bmp');
end;
//3、释放图片
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
MBitmap.Free;
end;
//4、画图片
procedure TForm1.pmiN6AdvancedDrawItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; State: TOwnerDrawState);
var
mRect,cRect: Trect;
begin
aCanvas.TextWidth(TMenuItem(Sender).Caption); //文字的宽度
//画图片
Mrect := Rect(0,0,MBitmap.Width,MBitmap.Height);
aCanvas.Draw(Mrect.Left,aRect.Top-aRect.Bottom,mBitmap);
//写主菜单中菜单项的文字
cRect := Rect(aRect.Left + mBitmap.Width+2,aRect.Top,aRect.Right,aRect.Bottom);
//也可以使用DrawText,但是区域只会是文字的区域
aCanvas.TextRect(cRect,aRect.Left + mBitmap.Width+2,aRect.top + 3,TMenuItem(Sender).Caption);
//文字背景透明
SetBkmode(aCanvas.Handle,TRANSPARENT);
//在图片中画文字,使上面的TextRect方法会覆盖图片
DrawText(aCanvas.Handle,'我'+#13+'的'+#13+'菜'+#13+'单',-1,mRect,DT_LEFT+DT_Center);
end;
State的状态有:
odSelected:被选中;
odGrayed:为huis;
odDisabled:不可用;
odChecked:被选中;
odFocused:处于焦点;
odDefault: 默认状态;
odHotLine:高亮显示;
odInactive:菜单及其关联的子菜单处于非活动状态;
odNoAccel:在菜单被绘制时,忽略加速键;
odNoFocusRect:不显示焦点区域;
odComboBoxEdit:组合框被绘制时发生;
odReserved1,odReserved2:保留;
//在AdvancedDrawItem中设置不同状态下按钮的颜色
procedure TForm1.pmiN7AdvancedDrawItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; State: TOwnerDrawState);
begin
ACanvas.Brush.Color := clwhite;
if odSelected in State then
begin
ACanvas.Font.Color := clRed;
ACanvas.Pen.Color := clBlue;
ACanvas.Rectangle(Rect(1,(TMenuItem(Sender).Tag-1)*20,ARect.Right-1,
(TMenuItem(Sender).Tag-1)*20+15));
end
else
begin
ACanvas.Font.Color := clBlack;
ACanvas.Pen.Color := clWhite;
ACanvas.Rectangle(Rect(1,(TMenuItem(Sender).Tag-1)*20,ARect.Right-1,
(TMenuItem(Sender).Tag-1)*20+15));
end;
ACanvas.TextRect(ARect,aRect.Left+2,aRect.top+3,TMenuItem(Sender).Caption);
end;
设置Visible属性