procedure TForm1.b1DrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean); var leftstr,itemcaption:String; lp:Integer; begin lp:=5; itemcaption:=TMenuItem(Sender).Caption; leftstr:=Copy(itemcaption,1,pos('&',itemcaption)-1); ACanvas.TextRect(ARect,lp,2,leftstr); inc(lp,ACanvas.TextWidth(leftstr)); ACanvas.Font.Style :=ACanvas.Font.Style+[fsUnderline]; ACanvas.TextRect(ARect,lp,2,copy(itemcaption,pos('&',itemcaption)+1,length(itemcaption))); if Selected then begin //调用要激发的事件。 ShowMessage('ok'); end; end;
记着将菜单的OwnerDraw属性打开。这是我自己试过的。*_*
//有一个变态的方法!~~ //通过Application.OnHint来实现~~ //鼠标移到菜单项会触发TApplication.OnHint事件~~ //参考如下代码~~procedure TForm1.ApplicationEvents1Hint(Sender: TObject); begin Caption := Application.Hint; end;procedure TForm1.FormCreate(Sender: TObject); var I: Integer; begin for I := 0 to ComponentCount - 1 do if Components[I] is TMenuItem then TMenuItem(Components[I]).Hint := '#' + Components[I].Name; end;
[email protected]
一会发给你
ARect: TRect; Selected: Boolean);
var
leftstr,itemcaption:String;
lp:Integer;
begin
lp:=5;
itemcaption:=TMenuItem(Sender).Caption;
leftstr:=Copy(itemcaption,1,pos('&',itemcaption)-1);
ACanvas.TextRect(ARect,lp,2,leftstr);
inc(lp,ACanvas.TextWidth(leftstr));
ACanvas.Font.Style :=ACanvas.Font.Style+[fsUnderline];
ACanvas.TextRect(ARect,lp,2,copy(itemcaption,pos('&',itemcaption)+1,length(itemcaption)));
if Selected then
begin
//调用要激发的事件。
ShowMessage('ok');
end;
end;
//通过Application.OnHint来实现~~
//鼠标移到菜单项会触发TApplication.OnHint事件~~
//参考如下代码~~procedure TForm1.ApplicationEvents1Hint(Sender: TObject);
begin
Caption := Application.Hint;
end;procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
begin
for I := 0 to ComponentCount - 1 do
if Components[I] is TMenuItem then
TMenuItem(Components[I]).Hint := '#' + Components[I].Name;
end;
你们两人的方法,我都试了,都要先点一下菜单,以后鼠标放在菜单上面,才能触发事件。我需要的是不点一次鼠标,就能触发事件的方法,不知你们能否帮忙?谢谢了。