求调用外部程序及判断其完成的方法 !急(10分)

  • 主题发起人 主题发起人 admini
  • 开始时间 开始时间
A

admini

Unregistered / Unconfirmed
GUEST, unregistred user!
我想调用外部程序dat2mpg.exe自动把.DAT转为mpg格式,要等dat2mpg.exe转换完了这个文件后才执行下一条指令。<br><br>请教各位大哥 &nbsp;<br>
 
外部程序向你的主程序发送消息。<br>你可以在主程序自定义消息。<br>----------------------------------------<br>DELPHIVCL中消息的传递<br>&nbsp; &nbsp;Delphi是Borland公司提供的一种全新的WINDOWS编程开发工具.由于它采用了具有弹性的和可重用的面向对象Pascal(object-orientedpascal)语言,并有强大的数据库引擎(BDE),快速的代码编译器,同时又提供了众多出色的构件.受到广大编程人员的青睐.在众多的编程语言(VB,PowerBuilder,Powerpoint等)中脱颖而出.<br>&nbsp; &nbsp;其中一个DELPHI强于其他编程语言(如VB4.0)的地方就是在DELPHI中可自定义消息,并可直接处理消息.这对于那些希望编写自己的构件(Component),或者希望截获.过滤消息的用户来说是必不可少的.因为编写构件一般要对相应的消息进行处理.下面就对Delphi中消息处理机制进行一下介绍。<br><br>一.DELPHIVCL中消息的传递<br>&nbsp; &nbsp;Delphi中每一个VCL(VisualComponentLibrary)构件(如Tbutton,Tedit等)都有一内在的消息处理机制,其基本点就是构件类接收到某些消息并把它们发送给适当的处理方法,如果没有特定的处理方法,则调用缺省的消息处理句柄。<br>&nbsp; &nbsp;其中mainwndproc是定义在Twincontrol类中的一个静态方法,不能被重载(Override)。它不直接处理消息,而是交由wndproc方法处理,并为wndproc方法提供一个异常处理模块。Mainwndproc方法声明如下:<br>&nbsp;procedureMainWndProc(varMessage:TMessage);<br>&nbsp; &nbsp;Wndproc是在Tcontrol类中定义的一个虚拟方法,由它调用dispatch方法来进行消息的分配,wndproc方法声明如下:<br>&nbsp;procedureWndProc(varMessage:TMessage);virtual;<br>&nbsp; &nbsp;dispatch方法是在Tobject根类中定义的,其声明如下:<br>&nbsp;procedureTobject.dispatch(varMessage);传递给dispatch的消息参数必须是一个记录类型,且这个记录中第一个入点必须是一个cardinal类型的域(field),它包含了要分配的消息的消息号码.例如:<br>type<br>&nbsp;Tmessage=record<br>&nbsp;Msg:cardinal;<br>&nbsp;wparam:word;<br>&nbsp;lparam:longint;.<br>&nbsp;result:longint;<br>end;<br>&nbsp; &nbsp;而Dispatch方法会根据消息号码调用构件的最后代类中处理此消息的句柄方法.如果此构件和它的祖先类中都没有对应此消息的处理句柄,Dispatch方法便会调用Defaulthandler方法.Defaulthandler方法是定义于Tobject中的虚拟方法,其声明如下:<br>&nbsp;procedureDefaulthandler(varMessage);virtual;<br>&nbsp; &nbsp;Tobject类中的Defaulthandler方法只是实现简单的返回而不对消息进行任何的处理.我们可以通过对此虚拟方法的重载,在子类中实现对消息的缺省处理.对于VCL中的构件而言,其Defaulthandler方法会启动windowsAPI函数Defwindowproc对消息进行处理.<br><br>二.DELPHI中的消息处理句柄<br>&nbsp; &nbsp;在DELPHI中用户可以自定义消息及消息处理句柄.消息处理句柄的定义有如下几个原则:<br>&nbsp; &nbsp;1.消息处理句柄方法必须是一个过程,且只能传递一个Tmessage型变量参数.<br>&nbsp; &nbsp;2.方法声明后要有一个message命令,后接一个在0到32767之间的消息标号(整型常数).<br>&nbsp; &nbsp;3.消息处理句柄方法不需要用override命令来显式指明重载祖先的一个消息处理句柄,另外它一般声明在构件的protected或private区.<br>&nbsp; &nbsp;4.在消息处理句柄中一般先是用户自己对消息的处理,最后用inherited命令调用祖先类中对应此消息的处理句柄(有些情况下可能正相反).由于可能对祖先类中对此消息的处理句柄的名字和参数类型不清楚,而调用命令inherited可以避免此麻烦,同样如果祖先类中没有对应此消息的处理句柄,inherited就会自动调用Defaulthandler方法.(当然如果要屏蔽掉此消息,就不用inherited命令了)。<br>&nbsp; &nbsp;消息处理句柄方法声明为:<br>&nbsp;procedureMymsgmethod(varmessage:Tmessage);messageMsgtype;<br>同样用户也可以定义自己的消息,用户自定义消息应从WM_USER开始.<br>&nbsp; &nbsp;自定义消息及消息处理句柄举例如下:<br>&nbsp;constmy_paint=Wm_user+1;<br>&nbsp;type<br>&nbsp; &nbsp;Tmypaint=record<br>&nbsp; &nbsp;msgid:cardinal;<br>&nbsp; &nbsp;msize:word;<br>&nbsp; &nbsp;mcolor:longint;<br>&nbsp; &nbsp;msgresult:longint;<br>&nbsp;end;<br>&nbsp;type<br>&nbsp; &nbsp;Tmycontrol=class(TCustomControl)<br>&nbsp;protected<br>&nbsp; &nbsp;procedurechange(varmessage:Tmypaint);messagemy_paint;<br>&nbsp;.....<br>&nbsp;end;<br>&nbsp;......<br>&nbsp;procedureTmycontrol.change(varmessage:Tmypaint);<br>&nbsp;begin<br>&nbsp; &nbsp;size:=message.msize;{设置Tmybutton尺寸属性}<br>&nbsp; &nbsp;color:=message.mcolor;{设置Tmybutton颜色属性}<br>&nbsp;{dosomethingelse}<br>&nbsp; &nbsp;inherited;{交由Tcustomcontrol处理}<br>&nbsp;end;<br><br>三.过滤消息<br>&nbsp; &nbsp;过滤消息又称消息陷阱。在一定情况下,用户可能需要屏蔽某些消息.或者截获某些消息进行处理。由以上介绍可以看出过滤消息一般有三种途径:<br>&nbsp;(1).重载构件继承的虚拟方法wndproc.<br>&nbsp;(2).针对某消息编写消息处理句柄.<br>&nbsp;(3).重载构件继承的虚拟方法Defhandler,在其中对消息进行处理。其中常用的方法是方法(2),在上节中已介绍过了,方法(1)与方法(3)相似,这里只简单介绍一下方法(1)。<br>&nbsp; &nbsp;重载虚拟方法wndproc的一般过程如下:<br>&nbsp; &nbsp;procedureTmyobject.wndproc(varmessage:Tmessage);<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; {...判断此消息是否该处理..}<br>&nbsp; &nbsp; inheritedwndproc(message);<br>&nbsp; &nbsp; {未处理的消息交由父辈wndproc方法处理}<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;由此可以看出在wndproc方法中处理消息的优势是可以过滤整个范围内的消息,而不必为每个消息指定一个处理句柄,事实上Tcontrol构件中就是利用它来过滤并处理所有的鼠标消息的(<br>从WM_mousefirst到WM_mouselast,如下代码示).同样利用它也可以阻止某些消息被发送给处理句柄。<br>&nbsp;procedureTControl.WndProc(varMessage:TMessage);<br>&nbsp;begin<br>&nbsp; &nbsp;if(Message.Msg&gt;=WM_MOUSEFIRST)and(Message.Msg&lt;=WM_MOUSELAST)<br>&nbsp; &nbsp;then<br>&nbsp; &nbsp; &nbsp;ifDraggingthen{处理拖曳事件}<br>&nbsp; &nbsp; &nbsp; &nbsp; DragMouseMsg(TWMMouse(Message))<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; ...{处理其他鼠标消息}<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;Dispatch(Message);<br>&nbsp; &nbsp;{否则正常发送消息}<br>&nbsp;end;<br>&nbsp;下例为一简单的自定义构件例子:<br>&nbsp;Tmyedit类是从Tedit类派生出的一个新类,它的特点是在运行中不能获得焦点,不能由键盘输入(有点类似Tlabel构件).我们可在其wndproc方法中过滤出WM_setfocus,WM_mousemove消息并进行处理来达到上述要求,源程序如下:<br>unitmyedit;<br>interface<br>uses<br>&nbsp;Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls;<br>type<br>&nbsp;Tmyedit=class(TEdit)<br>private<br>&nbsp;{Privatedeclarations}<br>protected<br>&nbsp;{Protecteddeclarations}<br>&nbsp;{otherfieldsandmethods}<br>&nbsp;procedurewndproc(varmessage:Tmessage);override;<br>public<br>&nbsp;{Publicdeclarations}<br>published<br>&nbsp;{Publisheddeclarations}<br>end;<br><br>procedureRegister;<br>implementation<br>procedureRegister;<br>begin<br>&nbsp;RegisterComponents('Samples',[Tmyedit]);<br>end;<br>procedureTmyedit.wndproc(varmessage:tmessage);<br>begin<br>&nbsp;if message.msg=wm_mousemove then<br>&nbsp;begin<br>&nbsp; &nbsp;cursor:=crarrow;<br>&nbsp; &nbsp;{设置光标为crarrow,而不是缺省的crBeam光标}<br>&nbsp; &nbsp;exit;<br>&nbsp;end;<br>&nbsp;if message.msg=wm_SetFocus then exit;<br>&nbsp; &nbsp;{屏蔽掉WM_setfocus消息,不让Tmyedit控件获得输入焦点}<br>&nbsp;inheritedwndproc(message);<br>&nbsp;{其他消息交父辈wndproc处理}<br>&nbsp;end;<br>end.<br>&nbsp; &nbsp;您可以将Tmyedit加到ComponentPalette中检验其性能。<br>&nbsp; &nbsp;由以上介绍可以看出,只有清楚了DelphiVCL中的消息处理机制,掌握好处理各种消息的方法和时机(必要时要借助各种工具,如winsight32,spy等),并结合OOP语言的特点,我们才可能编出高质量的构件。这当然要靠读者在实践中不断摸索,积累经验. <br>
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部