windows api
api(application programming interface)应用编程接口,所有的计算机语言都要用到它。 api是什么?api是程序使用操作系统提供的服务的一个途径,我们大多数的编程都不直接操作硬件,而是调用这些api,再由操作系统直接操作硬件,这样的好处是我们编程时不必考滤与硬件兼容的问题,更重要的是从操作系统层实现了代码共享。所以,在编程时如果能用api实现的功能,我们尽量用它。
delphi如何使用windows api
在日常开发工作中,我们经常要使用windows api函数,那么api函存在在哪里呢?我们可以这样理解,api函数就是封装在windows系统提供的dll系统文件中。如我们经常使用的beep过程(响铃),就是调用windwos系统目录下的user32.dll中的messagebeep实现的;sendmessage(发消息)就是直接调用user32.dll的sendmessagea。delphi使用频率最多的dll是:advapi32.dll、kernel32.dll、mpr.dll、version.dll、comctl32.dll、gdi32.dll、opengl32.dll、user32.dll、wintrust.dll、msimg32.dll。
那么delphi是如何使用这api函数的呢?既然api函数存在于系统的dll中,那么我们就可以像调用自己写的dll一样调用api函数啦。调用dll函数有两种方式,一种是静态方式,一种是动态方式。调用windows的api都是采用静态方式,为什么呢?这是因为这些dll是操作系统提供的最基本服务,操作系统在启动时就已经加载到内存中了,并且操作系统也大量的使用他们。
api与日常编程
delphi封装了windows的大部分api函数(主要在windows.pas单元中),应该说可以完成我们的大部分工作,我们一般不直接调用api函数。但有时有特别要求时,我们可能就要调用一些delphi没封装过的api,有时甚至会调用windows没公布的api函数。那么如何调用这些api函数呢?就像前面说的,使用静态方法调用就好了。更详细的调用请参看相关的资料。
调用这些delphi没封装的api函数关键是要知道参数。可以查阅看最新的msdn或相关的资料。
api与vcl
microsoft的mfc大量的封装地windows api,vcl也不例外。vcl功能的实现大部分都离不开windows api,要么是直接调用,要么是经过简单的封装再调用。如tcontrol的repaint的实现(control单元中):
procedure tcontrol.repaint;
var
dc: hdc;
begin
if (visible or (csdesigning in componentstate) and not (
csnodesignvisible in controlstyle)) and (parent <> nil) and
parent.handleallocated then
if csopaque in controlstyle then
begin
//直接调用user32.dll的getdc
dc := getdc(parent.handle);
try
//直接调用gdi32.dll的intersectcliprect
intersectcliprect(dc, left, top, left + width, top +
height);
// parent.paintcontrols调用大量的api
parent.paintcontrols(dc, self);
finally
// 直接调用user32.dll的releasedc
releasedc(parent.handle, dc);
end;
end else
begin
//以下两个经过封装调用
invalidate;
update;
end;
end;
tcustomadodataset = class(tdataset, iunknown,
recordseteventsvt)
private
frecordsetobject: _recordset;
ffindcursor: _recordset;
flookupcursor: _recordset;
flockcursor: _recordset;
frowset: irowset;
faccessor: iaccessor;
frowsetfind: irowsetfind;
fhaccessor: haccessor;
folerecbufsize: integer;
…
end;
tadoquery.first->tdataset.first->tdataset.internalfirst-> tcustomadodataset. internalfirst ->recordset15. movefirst
tadoquery从tcustomadodataset继承,而tcustomadodataset从tdataset继承,tdataset.internalfirst定义的是虚方法,而子类tcustomadodataset. internalfirst覆盖了它。tcustomadodataset. internalfirs调用接口recordset15的 movefirst方法。
不难发现,tadoquery.first最终是通过接口调用com对象实现的。
delphi与windows 外壳
什么是windows外壳呢?windows外壳是windows界面操作的环境,它还为我们编程提供了强大的可扩展性。我们利用windows外壳实现某些功能的编程,称为外壳扩展。例如,如果你的机器上已经装了winrar,在文件夹的右键菜单就会看到winrar的压缩菜单。这些功能就是通过windows的外壳扩展实现的。
windows外壳是基于com的,所以所有的外壳扩展都要通过接口实现。delphi也定义了很多外壳扩展接口,在delphi7的安装目录下的source\rtl\win\shlobj.pas单元中。
在delphi的demo目录下的有一个virtual listview的例子,就是通过windows外壳扩展实现磁盘浏览的,有兴趣的读者可以研究一下。
嵌入式汇编语言
在代码中嵌入汇编语言也是delphi的一大特色,如vcl的根类tobjce的实现就大量嵌入了汇编语句。
汇编语言是一种比较低级的计算机语言,与硬件的关系比较密切。所以我们在平时编程中尽量不要使用它,但是在某些特殊场合(如性能要求比较高、需要直接操纵硬件)下,使用它还是能发挥很大的作用的。
相关链接:delphi初学者参考(一)
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器