📄 menu.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>UDDF - MENU</TITLE>
<META NAME="Description" CONTENT="Menu section of the Delphi Developers FAQ" >
<META NAME="Keywords" CONTENT=" ">
</HEAD>
<BODY bgcolor="#FFFFFF">
<CENTER>
<IMG SRC="../images/uddf.jpg"> </CENTER>
<HR SIZE="6" color="#00FF00">
<CENTER><FONT SIZE="7" FACE="Arial Black" COLOR="RED">Menu</FONT></CENTER>
<P><H1><A NAME="menu0">Catch SHIFT key during menu item selection?</P></A></H1>
<P><I>From: "Rodney E Geraghty" <gerarod@ibm.net></I></P>
Try some thing like this:<P>
<HR><PRE>
procedure TForm1.Menu11Click(Sender: TObject);
begin
{Check if Shift key is down}
if HiWord(GetKeyState(VK_SHIFT)) <> 0 then
Label1.Caption := 'Shift'
else
{Check if Ctrl key is down}
if HiWord(GetKeyState(VK_CONTROL)) <> 0 then
Label1.Caption := 'Control'
else
{Check if Alt key is down}
if HiWord(GetKeyState(VK_MENU)) <> 0 then
Label1.Caption := 'Alt'
else
Label1.Caption := 'None';
end;
</PRE><HR>
<P><H1><A NAME="menu1">How to dynamically create popup menus inside other popups?</P></A></H1>
<P><I>From: Chris Jobson <chrisj@rcp.co.uk></I></P>
Try something like this
<HR><PRE>procedure TForm1.PopupMenu2Popup(Sender: TObject);
var
mi, msub: TmenuItem;
begin
with (Sender as TPopupMenu) do begin
// Delete all items
// while Items.Count > 0 do Items.delete(0);
// Old version had resource leak. Leak plugged by Andrew Stewart (astewart@Strobes.co.nz)
while Items.Count > 0 do Items[0].Free;
// Create ordinary item "First"
mi := TMenuItem.Create(self);
with mi do begin
Caption := 'First';
OnClick := MyClick;
end;
Items.Insert(0, mi);
// Create a submenu "Sub" with two items "Sub1" and
// "Sub2"
mi := TMenuItem.Create(self);
with mi do begin
Caption := 'Sub';
msub := TMenuItem.Create(self);
with msub do begin
Caption := 'Sub1';
OnClick := MyClick;
end;
Insert(0, msub);
msub := TMenuItem.Create(self);
with msub do begin
Caption := 'Sub2';
OnClick := MyClick;
end;
Insert(1, msub);
end;
Items.Insert(1, mi);
end;
end;
procedure TForm1.MyClick(Sender: TObject);
begin
beep;
end;
</PRE><HR>
<P><H1><A NAME="menu2">How do I add a bitmap to a MENU ...</P></A></H1>
<P><I>From: kclaeys@innet.be (Kurt Claeys)</I></P>
> How do I add bitmaps to a menu?
Maybe like this :
<HR><PRE>
var
Bmp1 : TPicture;
...
Bmp1 := TPicture.Create;
Bmp1.LoadFromFile('c:\where\b1.BMP');
SetMenuItemBitmaps( MenuItemTest.Handle,
0,
MF_BYPOSITION,
Bmp1.Bitmap.Handle,
Bmp1.Bitmap.Handle);
...
</PRE><HR>
Create a Picture.<P>
Load a .BMP from somewhere into the picture.<P>
Use the SetMenuItemBitmaps API call to connect the Picture to the Menu with
these parameters : <P>
<OL>
<LI> MenuItemTest is the name given to the horizontal Menuitem
<LI> 0,1 ... is the position of the item on which you want to place the
bitmap. (start counting with 0)
</OL>
<P>
The first of the two bitmap-handles is the one for the bitmap displayed
for the unchecked menuitem.<P>
The second is the one for the checked menuitem. These can be the same or not.<P>
All this can by coded in the .Create of a form. <P>
Result : It works, but only the right-top of the bitmap is displayed. Rest
us to change the height and/or width of the menuitem according to the bitmap<P>
<P><H1><A NAME="menu3">How do I dynamically add a MenuItem to a Menu?</P></A></H1>
<h2> Solution 1 </H2>
<P><I>From: Jeff Lawton <jlawton@groupz.net></I></P>
Now I'm not sure what your trying to do, add it to the top menu, or as a
sub menu item, so I'll include code for both.. You can work it out from
there. <p>
<U>New Top Level Item:</U>
<HR><PRE>
procedure tform1.addmainitem(s:string);
var
newitem : Tmenuitem;
begin
newitem:=tmenuitem.create(Mainmenu1);
newitem.caption:=s;
{if you want to assign an onclick
newitem.onclick:=Dynamenuclick; }
{add it to the top level menu}
mainmenu1.items.insert(mainmenu1.items.count,newitem);
removemenu1.enabled:=true;
addmenuitem1.enabled:=true;
end;
</PRE><HR>
<U>Sub Menu Item:</U>
<HR><PRE>
procedure tform1.addsubitem(s:string; to : integer);
var
newitem, toitem : Tmenuitem;
begin
{to = top level menu to add item to}
toitem:=mainmenu1.items[to];
newitem:=tmenuitem.create(toitem);
newitem.caption:=s;
{if you want to assign an onclick
newitem.onclick:=Dynamenuclick; }
toitem.onclick:=nil;
toitem.insert(toitem.count,newitem);
removemenuitem1.enabled:=true;
end;
</PRE><HR>
<H2>Solution 2 </H2>
<P><I>From: janij@dystopia.fi (Jani Järvinen)</I></P>
You should use the ready made menu functions defined in the Menus
unit. The routines in D2 are as follows:
<HR><PRE>function NewMenu(Owner: TComponent; const AName: string; Items: array
of TMenuItem): TMainMenu;
function NewPopupMenu(Owner: TComponent; const AName: string;
Alignment: TPopupAlignment; AutoPopup: Boolean; Items: array of
TMenuitem): TPopupMenu;
function NewSubMenu(const ACaption: string; hCtx: Word; const AName:
string; Items: array of TMenuItem): TMenuItem;
function NewItem(const ACaption: string; AShortCut: TShortCut;
AChecked, AEnabled: Boolean; AOnClick: TNotifyEvent; hCtx: Word;
const AName: string): TMenuItem;
function NewLine: TMenuItem;
</PRE><HR>
This is quite messy, but using the functions is easy.<P>
<P><H1><A NAME="menu4">Hooking a procedure on to a dynamically created popup menu<IMG SRC="../images/new.gif" WIDTH=28 HEIGHT=11 BORDER=0 ALT=" [NEW]"></P></A></H1>
Can any body enlighten me on "How to Hooking a procedure on to a popup
menu which I create dynamically?"<p>
This is what I've done fo far
<HR><PRE>
Procedure TTypeOfFrame.CreateAPopUpMenu;
var
NewItem: TMenuItem;
FDragEnabledPopUpMenu : TPopUpMenu;
begin
NewItem := TMenuItem.Create(Self);
NewItem.Caption := 'Drag';
FDragEnabledPopUpMenu:= TPopUpmenu.Create(Self);
with FDragEnabledPopUpMenu do
begin
Items.Add(NewItem);
end;
end;</PRE><HR>
<I>[Bruno Sonnino, sonnino@netmogi.com.br]<I><P>
To hook a procedure to this new item, you must create a procedure inside
an object (define a private method for the form), like this:
<HR><PRE>
procedure MyClick(Sender : TObject);
</PRE><HR>
and then when you create the item you must set its OnClick event like
this:
<HR><PRE>
NewItem := TMenuItem.Create(Self);
NewItem.Caption := 'Drag';
NewItem.OnClick := MyClick;
</PRE><HR>
<HR SIZE="6" COLOR="#00FF00">
<FONT SIZE="2">
<a href="mailto:rdb@ktibv.nl">Please email me</a> and tell me if you liked this page.<BR>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.write("Last modified " + document.lastModified);
// -->
</SCRIPT><P>
<TABLE BORDER=0 ALIGN="CENTER">
<TR>
<TD>This page has been created with </TD>
<TD> <A HREF="http://www.dexnet.com./homesite.html"><IMG SRC="../images/hs25ani.gif" WIDTH=88 HEIGHT=31 BORDER=0 ALT="HomeSite 2.5b">
</A></TD>
</TR>
</TABLE>
</FONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -