Delphi 7中ActionMainMenuBar、ActionManager的问题!(100分)

  • 主题发起人 主题发起人 chengbin
  • 开始时间 开始时间
C

chengbin

Unregistered / Unconfirmed
GUEST, unregistred user!
<br>在Delphi 7中,如何用ActionMainMenuBar、ActionManager动态的创建一个二级菜单?<br>我想在菜单中创建打开过文件的历史,就像Delphi 7中的file下的Reopen,不知如何做!!<br><br>还有如何创建和ActionMainMenuBar样式一样的popupmenu,如Delphi 7中的右键菜单。<br><br>请大家帮忙,谢谢各位了!!!
 
打开文件的历史记录,应该在属性中可以设置的。你在仔细瞧瞧。
 
不太好做,我用的是:COMBOBOX控件来代替这种效果<br>http://www.bwkj.net/Bios/Webmanagern.ace
 
请高手帮忙!!!!!
 
有一个这样的例子。<br>&nbsp;unit main;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, ActnList, StdActns, ToolWin, ActnMan, ActnCtrls, ActnMenus,<br>&nbsp; StdCtrls, ImgList, ComCtrls, ExtActns, XPStyleActnCtrls, BandActn,<br>&nbsp; StdStyleActnCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; ActionManager1: TActionManager;<br>&nbsp; &nbsp; ActionMainMenuBar1: TActionMainMenuBar;<br>&nbsp; &nbsp; Action1: TAction;<br>&nbsp; &nbsp; Action2: TAction;<br>&nbsp; &nbsp; Action3: TAction;<br>&nbsp; &nbsp; ReopenActionList1: TActionList;<br>&nbsp; &nbsp; Action4: TAction;<br>&nbsp; &nbsp; Action5: TAction;<br>&nbsp; &nbsp; Action6: TAction;<br>&nbsp; &nbsp; Action7: TAction;<br>&nbsp; &nbsp; Action8: TAction;<br>&nbsp; &nbsp; ActionToolBar1: TActionToolBar;<br>&nbsp; &nbsp; ImageList1: TImageList;<br>&nbsp; &nbsp; FileOpen1: TFileOpen;<br>&nbsp; &nbsp; EditCut1: TEditCut;<br>&nbsp; &nbsp; EditCopy1: TEditCopy;<br>&nbsp; &nbsp; EditPaste1: TEditPaste;<br>&nbsp; &nbsp; EditSelectAll1: TEditSelectAll;<br>&nbsp; &nbsp; EditUndo1: TEditUndo;<br>&nbsp; &nbsp; EditDelete1: TEditDelete;<br>&nbsp; &nbsp; RichEditBold1: TRichEditBold;<br>&nbsp; &nbsp; RichEditItalic1: TRichEditItalic;<br>&nbsp; &nbsp; RichEditUnderline1: TRichEditUnderline;<br>&nbsp; &nbsp; RichEditStrikeOut1: TRichEditStrikeOut;<br>&nbsp; &nbsp; RichEditBullets1: TRichEditBullets;<br>&nbsp; &nbsp; RichEditAlignLeft1: TRichEditAlignLeft;<br>&nbsp; &nbsp; RichEditAlignRight1: TRichEditAlignRight;<br>&nbsp; &nbsp; RichEditAlignCenter1: TRichEditAlignCenter;<br>&nbsp; &nbsp; SearchFind1: TSearchFind;<br>&nbsp; &nbsp; SearchFindNext1: TSearchFindNext;<br>&nbsp; &nbsp; SearchReplace1: TSearchReplace;<br>&nbsp; &nbsp; SearchFindFirst1: TSearchFindFirst;<br>&nbsp; &nbsp; FileSaveAs1: TFileSaveAs;<br>&nbsp; &nbsp; FilePrintSetup1: TFilePrintSetup;<br>&nbsp; &nbsp; FileRun1: TFileRun;<br>&nbsp; &nbsp; FileExit1: TFileExit;<br>&nbsp; &nbsp; RichEdit1: TRichEdit;<br>&nbsp; &nbsp; CustomizeActionBars1: TCustomizeActionBars;<br>&nbsp; &nbsp; Action9: TAction;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FileOpen1Accept(Sender: TObject);<br>&nbsp; &nbsp; procedure ReopenActionExecute(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; ReopenMenuItem: TActionClientItem;<br>&nbsp; &nbsp; OpenToolItem: TActionClientItem;<br>&nbsp; &nbsp; procedure FindReopenMenuItem(AClient: TActionClient);<br>&nbsp; &nbsp; procedure FindOpenToolItem(AClient: TActionClient);<br>&nbsp; &nbsp; procedure UpdateReopenItem(ReopenItem: TActionClientItem);<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FindReopenMenuItem(AClient: TActionClient);<br>begin<br>&nbsp; // Find the Reopen item by looking at the item caption<br>&nbsp; if AClient is TActionClientItem then<br>&nbsp; &nbsp; if Pos('Reopen...', TActionClientItem(AClient).Caption) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; ReopenMenuItem := AClient as TActionClientItem<br>end;<br><br>procedure TForm1.FindOpenToolItem(AClient: TActionClient);<br>begin<br>&nbsp; // Find the Open item by looking at the item caption<br>&nbsp; if AClient is TActionClientItem then<br>&nbsp; &nbsp; if Pos('Open', TActionClientItem(AClient).Caption) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; OpenToolItem := AClient as TActionClientItem;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br><br>&nbsp; procedure SetupItemCaptions(AnItem: TActionClientItem);<br>&nbsp; var<br>&nbsp; &nbsp; I: Integer;<br>&nbsp; begin<br>&nbsp; &nbsp; if Assigned(AnItem) then<br>&nbsp; &nbsp; &nbsp; for I := 0 to AnItem.Items.Count - 1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; TCustomAction(ReopenActionList1.Actions).Caption :=<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Copy(AnItem.Items.Caption, 5, MaxInt);<br>&nbsp; end;<br><br>begin<br>&nbsp; RichEdit1.Align := alClient;<br>&nbsp; // Find the Reopen... menu item on the ActionMainMenu<br>&nbsp; ActionManager1.ActionBars.IterateClients(ActionManager1.ActionBars[0].Items,<br>&nbsp; &nbsp; FindReopenMenuItem);<br>&nbsp; // Find the Reopen... menu item on the ActionToolBar<br>&nbsp; ActionManager1.ActionBars.IterateClients(ActionManager1.ActionBars[1].Items,<br>&nbsp; &nbsp; FindOpenToolItem);<br>&nbsp; // Set the captions of the actions since they are used to open the file<br>&nbsp; SetupItemCaptions(ReopenMenuItem);<br>&nbsp; SetupItemCaptions(OpenToolItem);<br>end;<br><br>procedure TForm1.FileOpen1Accept(Sender: TObject);<br>var<br>&nbsp; I: Integer;<br>&nbsp; Found: Boolean;<br>begin<br>&nbsp; Found := False;<br>&nbsp; // If the filename is already in the list then do not add it again<br>&nbsp; for I := 0 to ReopenActionList1.ActionCount - 1 do<br>&nbsp; &nbsp; if CompareText(TCustomAction(ReopenActionList1.Actions).Caption,FileOpen1.Dialog.FileName) = 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Found := True;<br>&nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; end;<br>&nbsp; if not Found then<br>&nbsp; begin<br>&nbsp; &nbsp; // Update the Reopen menu...<br>&nbsp; &nbsp; UpdateReopenItem(ReopenMenuItem);<br>&nbsp; &nbsp; UpdateReopenItem(OpenToolItem);<br>&nbsp; end;<br>&nbsp; // ...then actually open the file<br>&nbsp; RichEdit1.Lines.LoadFromFile(FileOpen1.Dialog.FileName);<br>end;<br><br>procedure TForm1.UpdateReopenItem(ReopenItem: TActionClientItem);<br>var<br>&nbsp; I: Integer;<br>begin<br>&nbsp; if ReopenItem = nil then<br>&nbsp; &nbsp; &nbsp;exit;<br>&nbsp; // Add thew new filename to the beginning of the list and move other items down<br>&nbsp; for I := ReopenActionList1.ActionCount - 1 downto 0 do<br>&nbsp; &nbsp; if I = 0 then<br>&nbsp; &nbsp; &nbsp; TCustomAction(ReopenActionList1.Actions).Caption := FileOpen1.Dialog.FileName<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; TCustomAction(ReopenActionList1.Actions).Caption :=<br>&nbsp; &nbsp; &nbsp; &nbsp; TCustomAction(ReopenActionList1.Actions[I - 1]).Caption;<br>&nbsp; // Add new items to the reopen item if necessary<br>&nbsp; if ReopenItem.Items.Count &lt; ReopenActionList1.ActionCount then<br>&nbsp; &nbsp; ReopenItem.Items.Add;<br>&nbsp; // Set the item captions by appending a number for use as the shortcut<br>&nbsp; // This change will cause them to be streamed which allows us to store the<br>&nbsp; // filenames when the application is shutdown<br>&nbsp; for I := 0 to ReopenItem.Items.Count - 1 do<br>&nbsp; begin<br>&nbsp; &nbsp; ReopenItem.Items.Action := ReopenActionList1.Actions;<br>&nbsp; &nbsp; ReopenItem.Items.Caption := Format('&amp;%d: %s', [I,<br>&nbsp; &nbsp; &nbsp; TCustomAction(ReopenActionList1.Actions).Caption]);<br>&nbsp; end;<br>end;<br><br>procedure TForm1.ReopenActionExecute(Sender: TObject);<br>begin<br>&nbsp; // Set the reopened filename into the FileOpen action and call OnAccept to open the file normally<br>&nbsp; FileOpen1.Dialog.FileName := (Sender as TCustomAction).Caption;<br>&nbsp; // Execute the action's OnAccept logic<br>&nbsp; FileOpen1.OnAccept(nil);<br>end;<br><br>end.
 
接受答案了.
 
后退
顶部