求TActionManager,TActionlist怎么使用,在编程中有何优势

LeeYu 2011-11-07 05:10:18
Delphi高手:
请问 TactionManager ,Tactionlist 怎么用啊,在大型程序中这两个控件有何优势啊,
最好是举个详细实用的例子。
小弟谢谢了。积分都给你了
...全文
191 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jfhyn 2011-11-08
  • 打赏
  • 举报
回复
Using TActionList
使用tactionlist
Start a new application, and drop a TActionList component on the Form.
Right-click with the mouse on the ActionList1 component to get the
Action List Editor. The Action List Editor shows the action categories,
and for each action category the individual actions that are available
for the current application.
建立一个新程序, 在form上放一个tactionlist构件. 右键点ACTIONLIST1会弹出
Action List Editor(动作编辑器). 动作编辑器显示了动作种类列表和在此程序中
可以使用的每个动作种类中的每个动作.
We can define new actions, or select a number of existing (standard)
actions. The latter can be done by right-clicking with the mouse in
the Action List Editor, and selecting the New Standard Action menu:
我们可以定义新动作, 或者选择选择一系列的已有的(标准)动作. 后面的可以通过
右键点动作编辑器, 然后选择新标准动作菜单得到.


This will give us a new dialog with the (current) list of standard
actions. Note that we can select more than one action at the same
time by holding down the Ctrl key and clicking on each individual
item (see left figure below).
这会给我们一个现有标准动作列表的对话框. 注意我们可以通过按住CTRL键不放
点击需要的项目来一次选择多个动作.


After we click on OK, we return to the Action List Editor which now
shows two categories (Edit and Window) and four actions in total
(Copy, Cut and Paste for the Edit categories, and MinimizeAll for
Windows) - see right figure above.
我们点击OK后, 就回到了动作编辑器, 现在就有了两个类别(编辑和窗口)和4个动作
(编辑类的Copy, Cut 和 Paste和窗口类的MinimizeAll ).

It's now time to add some controls to the Form and connect them to
these actions.
现在我们在FORM上添加一些控件并把他们连到动作列表上.

Control Action
控件动作
Go to the Win32 tab of the component palette, and drop a ToolBar
component on the form. Right-click on it and select "New Button"
to add a new button. Repeat this until you have four buttons next
to each other on the toolbar (tip: you may want to insert a
separator between the third and fourth button).
放一个TOOLBAR构件, 右键点选择新按钮来加一个按钮. 重复以上动作添加4个按钮.
(也许你想在3/4中间加个分割按钮)

Now, click the left button, and set its Action property to EditCopy1.
Set the Action property of the second button to EditCut1, and the
third button to EditPaste1. Finally, set the Action property of the
last button to WindowMinimizeAll1.
现在, 把第一个按钮的ACTION属性设成EDITCOPY1, 2设成EDITCUT1, 3-EDITPASTE1,
最后一个设成WindowMinimizeAll1.

Before we compile and run this application, let me remind you that
we now have four fully functional toolbar buttons without having
written a single line of code. That's because we're using standard
behavior that's already been provided to us as "standard" re-usable
Actions. But it gets even better, because we can share these actions
with multiple components.
在我们编译和运行这个程序前, 我提醒你注意我们现在有了4个全功能的工具栏按钮
而不需要编写一行代码. 这就是为什么我们用标准动作, 因为它已经提供我们这些
"标准的"可以重用的动作. 不过现在更好了, 因为我们可以把这些动作共享给许多
构件.

Go back to the Standard tab of the component palette, and drop a
TMainMenu component on the Form. Double-click on it (to get into
the Menu Designer). Right-click on the Menu Designer select "Insert
from Template" and pick the Edit menu template. This gives us a
list of 10 menu items, while we only need three (Cut, Copy and
Paste), but at least it saves us some typing. You can easily remove
the menu items that you don't need, by the way.
现在让我们在FORM上放一个TMAINMENU构件. 双击它(弹出菜单设计器). 右键点菜单
设计器选"从模版中插入", 选择编辑菜单模版. 这样会提供10个菜单项, 不过我们只
需要其中的3个(Cut, Copy 和Paste), 至少节省了我们输入时间. 你可以很简单的
去掉你不需要的菜单项.

Go to the Cut1 menu item, and set its Action property to EditCut1.
Set the Action property of the Copy1 menu item to EditCopy1, and
let Paste1 point to EditPaste1. We're now almost ready to compile
and run our application, but we need one more thing. Let's drop
three normal buttons on the form (again from the Standard tab),
and connect them to EditCut1, EditCopy1 and EditPaste1 like we've
done twice before alre
选CUT1菜单项, 把它的ACTION属性设成EDITCUT1, COPY1为EDITCOPY1, PASTE1
为EDITPASTE1. 我们现在几乎可以编译运行这个程序了, 不过还需要几个步骤.
在FORM上放3个标准按钮, 把他们的ACTION属性分别设成EDITCUT1, EDITCOPY1,
和EDITPASTE1, 就象我们已经做过两次的那样.

后面就没有了:-(

我猜应该是这样:

放一个TMEMO构件, 然后就可以运行了.

在MEMO1中输入任何东西, 选中一些, 点击工具栏的COPY按钮, 然后点击CUT, 呵呵,
MEMO1中选中的内容消失了, 不要紧, 点PASTE按钮, 呵呵, 又回来了. 选择菜单和
标准按钮也会得到同样的结果.

这就是ACTION的好处:-)

翻译的不好, 如果不明白只能这样中英文对照着看了

一个友好的用户界面,必须具有下拉菜单,弹出菜单,工具条和快捷键。同样一个功能,程序员可能要提供几种操作方式,如文本拷贝,菜单命令©,快捷键Ctrl+C,工具条上的拷贝按钮,都是程序员提供给用户的操作,可以大大方便了不同层次的用户。但是,多增加一种操作方式,就意味着增加响应事件的代码,还有,实现统一功能的多个操作必须一致,如剪切板上不为空的时候,菜单命令,快捷键,按钮,都是可以选择的Enabled状态,而剪切板上没有内容时,这些构件的Enabled状态都为假。当然,如果不集中控制这些细节的话,实现相同功能的构件要一致,必须多增加代码,控制较为复杂。
Action就是将实现某个功能的细节统一管理起来,如Caption, Checked, Enabled, HelpContext, Hint, ImageIndex, ShortCt, Visible, onExecute等属性和事件。程序员设置Action属性的同时,还要实现执行Action的代码onExecute。定义了Action后,将其连接到一个菜单项,或者按钮上时,Action定义的属性和OnExecute事件,马上取代了原来的设置,并且建立了连接。通过这个连接,当Action的属性发生变化时,如Enabled发生变化,那么与之连接的构件属性都会自动更新;反过来,当构件对应的事件被执行时,Action的onExecute也被调用。
8.1.2 Action 和ActionList
Action是定义了最小的功能单位,使用它来在不同构件间,达到代码集中管理。表4-12定义了Action的一些属性:
ActionList是将多个Action统一管理的构件,在窗体设计阶段,使用它的弹出菜单,可以增减Action和定义Action的属性。它本身的属性Image连接了一个TImageList,指出它所包含的Action使用那个TImageList来定义ImageIndex。ActionLink是一个看不见的构件,但是它是负责构件和Action之间建立连接的。
表 4-12 Action属性和事件
属性和事件 具体功能
Caption 标题,可用于菜单项,ToolButton等。
Category 分类
Checked 设置选择状态,用于菜单项
Enabled 设置可用状态,用于与之连接的所有构件
HelpContext 帮助的索引项,用于与之相连的所有构件
Hint 智能感知,设置提示内容
ImageIndex 设置菜单项和按钮上的图片
Visible 指示与之连接的构件可否显示
ShortCut 设置菜单项的快捷键。
OnExecute 执行此Action的代码
OnHint 当被感知的构件显示Hint时调用的代码
OnUpdate 当Action更新构件的属性时调用的代码
////////////
我们先来认识TActionList。TActionList 是从TCustomAction中继承来的,由它管理定义好的Action,是程序员在对Action编程的接口。
新建一个工程文件,在主窗体上增加一个TActionList,一个TMemo,一个TImageList,几个按钮,几个菜单项。双击TActionlist,会出现它的编辑界面。再单击右键选择New standard action,显示出预定义的Action类别,属性Category是用来表明Action所属类别,我们可以根据需要选择具体的类别,如图4-10所示,我们引入了文件操作的一些标准Action。
这里所列的Action,大部分属性已经定义好了,就是说只要把他们添加到ActionList中,就可以使用。同样的,也可以新建Action,在Category属性中加上自己定义的名字,之后再添加新的Action 。
8.2.2 连接Action
定义了Action后,不与菜单项和按钮连接,是不能使用的,也没有达到程序功能集中实现和管理。使用对象观察器,看看我们刚刚选择的菜单项和按钮上是否有属性Action。
在这些构件的Action属性内,选择一个Action之后,构件本身的很多属性都随着Action定义的属性发生了变化,例如菜单项的Caption与Action的属性Caption就一致了,构件的属性Name和Tag是不随着改变的。连接后,Action的属性值被拷贝到所连接构件的相应属性上,这种连接是动态地,当Action的属性值发生变化时,自动更新所联系的构件。
注意:如果使用的是一个TToolButton 或着是菜单项,必须自己设置与它相联系的Images属性,虽然构件的ImageIndex属性已经动态的同Action相连接,但是属性Images并未自动连接。
继续本节的例子,通过设置菜单项和按钮的Action属性,完成Action的连接。
8.2.3 处理Action
处理Action的代码可放置在Action的onExecute事件,也可以集中放置在ActionList的onExecuteAction中,还可以更集中的放置在Application的onExecuteAction事件中。改变了以往每个构件都必须对应事件的做法,程序设计上更系统化。
在本节的例子中,编写某几个Action的onExecute方法。如FileOpenCmd的onExecute事件如下:
if OpenDialog.Execute then
Memo1.LoadFromFile(OpenDialog.FileName);
8.2.3 更新Action属性
Action的事件有OnExecute和OnUpdate,OnExecute事件在控制被触发时响应,比如说按钮被按下,菜单被按下,而OnUpdate事件是在应用程序空闲时被调用,用来更新Action的属性。ActionList 中也有OnUpdate, 可以用作设置菜单或者按钮的可选等属性使用。如下例,这个例子来自于$Delphi\Demos\RichEdit\Richedit.Dpr程序,有兴趣的读者可查看一下,以便更了解Action。
1 procedure TMainForm.ActionList2Update(Action: TBasicAction; var Handled: Boolean);
2 begin
3 EditCutCmd.Enabled := Editor.SelLength > 0;
4 EditCopyCmd.Enabled := EditCutCmd.Enabled;
5 if Editor.HandleAllocated then
6 begin
7 EditUndoCmd.Enabled := Editor.Perform(EM_CANUNDO, 0, 0) <> 0;
8 EditPasteCmd.Enabled := Editor.Perform(EM_CANPASTE, 0, 0) <> 0;
9 end;
10 end;
当应用程序空闲时,就执行这段代码,首先检查是否有被选定的文本,如果有则将Cut和Copy的Enabled属性设为True;当前控制是Editor时,设置Undo和Paste属性,Perfom是构件向自身发送消息的方法。
Delphi已经定义了一些标准的Action,这些Action实现的功能和VCL代码已经结合起来。所以,程序员可以直接使用,有些Action甚至不需要定义onExecute事件。如Cut,Copy,Paste等剪切板操作,Cancel,Delete,Edit,First,Insert,Last,Next,Prior,Post,Refresh等数据导航功能,Arrange,Cascade,Close,MinimizeAll,TileHorizontal等窗口排布功能。

三界 2011-11-08
  • 打赏
  • 举报
回复
窗体中如果用到TactionManager ,窗体就不能被继承使用。
要留意,开发基类模块最好不要用到它。
OO_is_just_P 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 onlyou13 的回复:]
引用 2 楼 sz_haitao 的回复:
集中管理控制各个功能(菜单、按钮的事件)

同意
[/Quote]
+1
onlyou13 2011-11-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sz_haitao 的回复:]
集中管理控制各个功能(菜单、按钮的事件)
[/Quote]
同意
haitao 2011-11-07
  • 打赏
  • 举报
回复
集中管理控制各个功能(菜单、按钮的事件)
我看见佛 2011-11-07
  • 打赏
  • 举报
回复
我一般用Tactionlist,管理一些通用的函数比较方便吧,,
比如TOOLBAR里面一些按扭和POPMENU执行的功能一致时……

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧