delphi 中混合使用脚本语言。

本文探讨了如何在Delphi应用程序中利用脚本语言增强交互性。脚本语言,如VBScript,能够弥补Web页面交互性的不足,并提供动态生成和解释执行的能力。通过引用一篇关于在Delphi中混合使用脚本语言的文章,揭示了脚本在自动化处理和现场调整等场景中的实用性。
摘要由CSDN通过智能技术生成

在 web 开发中,离不开脚本语言支持。web页面由于其本身交互性较弱的原因, 需要脚本语言来弥补缺陷。脚本语言有可动态生成,解释执行的特点,这是硬编码语言如delphi所不具备的。有时候应用程序需要能自动化处理一些过程(例如,客户现场调整)硬编码不合适, 而脚本语言就发挥出作用了。

下面就转载了一篇文章,包含了delphi 中混合使用脚本语言的例子和工作原理:

原文: http://www.malcolmgroves.com/stories/2004/09/23/scriptingYourDelphiApplications.html

Scripting Your Delphi Applications
 

 

Scripting languages are nothing new. From the DOS batch file you wrote years ago, through to the ASP code that drives your website, scripting languages are a good solution to certain problems. However, the Delphi community in general seems to have been slow to latch on to scripting languages. Maybe it's the Visual Basic connotations in VBScript, maybe it's the fact that these languages are interpreted, or maybe it's just that a lot of developers associate scripting with web applications and don't look any further.

However, once you realise how easy it is to use scripting with your Delphi application. The ability to run external scripts from within your application, and give those scripts access to whichever parts of your application you wish, starts to suggest some interesting possibilities. The fact that these scripts can be changed with nothing more than a text editor, by end-users already familiar with writing macros in Microsoft Word, without requiring you to recompile your application is an extremely powerful (and possibly a little scary) proposition.

In this paper I'll show you how to use the Microsoft ActiveScripting technologies to achieve everything we've just mentioned. While there is more to these technologies than can be covered by a single paper, by the end of this paper you'll know more than enough to start planning how you can use this technology in your application.

Setting Up

While there are a number of methods available to run scripts from Delphi applications, we're going to be using the Microsoft Scripting Control, which is available for download from http://www.msdn.microsoft.com/scripting/

Once you've downloaded and run the installation, start Delphi and go to the Component | Import ActiveX Control... menu.  Scroll down to the Microsoft Script Control as shown below, then click Install to install the component into Delphi's palette.

What you should end up with now is a TScriptControl component on your ActiveX tab.  That's it, you're setup and ready to go!

Running a simple script

Before we talk about the ActiveScript architecture, let's get our feet wet by running an extremely simple demo. Don't worry, we'll cover more complex issues later on.

Start a new application, and drop a TButton, a TEdit and a TScriptControl onto the main form. Put the following text into the Text property of Edit1:

MsgBox "Hello World"

Then, in the OnClick event of Button1, put the following code:

ScriptControl1.ExecuteStatement(Edit1.Text);

What this code does is pass whatever script command is typed into Edit1 to the ScriptControl to be executed. Lastly, in the OnShow event of the form, put the following code:

ScriptControl1.SitehWnd := Self.Handle;
ScriptControl1.Language := 'VBScript';

What this code does is firstly, pass the ScriptControl a reference to our Form's Window handle. It needs this so that any dialogs, etc that the script might display will be "parented" to our window. For example, if we left this line out, we coudl show a Modal dialog box (like MsgBox) but still switch away from it back to our main form.

The second line of code tells the ScriptControl to use the VBScript engine when evaluating this script statement. More about this later.

That's it! Run your application and press the button. You should see something like this:

Your Delphi application has executed an external VBScript in a few easy steps. There are plenty more cool things we can do, but for the moment, just to silence the cynics out there, let's take it further. Don't close your app, but try changing the contents of Edit1 at runtime, then press the button again. Provided you've entered valid VBScript, you should see different behaviour. If you don't know much VBScript, try entering the following text:

InputBox "Enter some text"

So, before we push on, let's have a look at what's happening under the covers.

How It All Works

Microsoft ActiveScript is basically 2 related COM objects: the scripting engine and the scripting host. The scripting engine is the COM object that actually processes the scripts, ie. VBScript, JScript, etc. This is usually provided by a third party, in the case of VBScript, it's provided by Microsoft.


Thank
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值