Delphi Win32 API 使用的特殊情况

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 iriscat 的 blog

    我个人认为,delphi 是当今最好的 windows 可视化开发工具。
其种种特点令开发如虎添翼。但要想发挥出 delphi 真正的内含性能
比如开发控件,实现一些特殊的功能,就必须直接调用 win32 api。
win32 api 主要包含在 windows95/98/nt/2k 的系统动态连接库中
如 kernel32.dll、user32.dll、gdi32.dll、shell32.dll 等等
    通常情况下 我们只要在代码的 uses 部分加入 windows 等单元
的声明即可像使用 delphi 内置函数一样的使用 win32 api 函数,十
分方便。
    但是,这样使用有时候会带来一些意想不到的麻烦。具体如下:
众所周知,windows 的版本十分多,仅 win95 就有 win95a,win95b
等等,而它们对 win32 api 的实现是有细微差别的,尽管它们都是
win32 平台。有一些 win32 api 函数在特定的 windows 版本中名称有
些许不同,或者根本就是是不存在的。
    这样就带来了问题: delphi 的 windows 等单元是与当时最新的
win32 api 全集对应的,delphi 在编译的时候总是动态连接 windows
函数库(所有的 windows 编译型开发工具都是这样的)。编译时毫无
问题的代码,其可执行文件在特定的 windows 平台上就无法使用。
    由于 windows 的可执行文件加载机制,在 delphi 集成环境中是
无法跟踪这样的潜在问题的。下面举两个例子:
    例一:
   
win32 api 声明:
    function broadcastsystemmessage; external user32
       name 'broadcastsystemmessagea';
    (来自 delphi 5 enterprise windows.pas :29408)
注意,使用这个函数编译后,程序在 win95 的早期版本中无法使用(
好像是 win95a)
    将函数声明改为如下后,问题解决:
    function broadcastsystemmessage; external user32
       name 'broadcastsystemmessage'; //注意这里!!
    例二:
    win32 api 声明:
    function shgetspecialfolderpath; external shell32
       name 'shgetspecialfolderpatha'
    (来自 delphi 5 enterprise shlobj.pas :3333)
注意,使用这个函数编译后,程序在 win95 某版本中无法使用
(shell32.dll 版本:4.00.1111),因为这个函数根本就不存在!!
目前我尚无解决方案
    要避免这样的问题的出现,有两个方法:
    一:不直接使用 win32 api,找第三方控件(这个方法好像是废话)
    二:动态加在函数。方法如下:以 win2k 中的 animatewindow 为例
(关于 animatewindow 函数的详细讨论,请到 www.csdn.net 文档,vb
查找关键字 animatewindow,感谢: iprogram)

unit xxxx;
.....
type
   fanimatewindow = function(
                       const hwnd : hwnd;       //仅对窗口有效
                       const dwtime : dword;    //动画持续时间,默认200ms
                       const dwflags : dword): dword; stdcall;
  
function animatewindow(const hwnd : hwnd; const dwtime : dword;
                       const dwflags : dword): dword;

implementation

function animatewindow(const hwnd : hwnd; const dwtime : dword;
                       const dwflags : dword): dword;
var
   dllhandle : thandle;
   animatewindow : fanimatewindow;
begin
   result := 0;
   dllhandle := loadlibrary('user32.dll');
   @animatewindow := getprocaddress(dllhandle,'animatewindow');
   result := animatewindow(hwnd, dwtime, dwflags);
end;

.....
end.

怎么样,是有些麻烦吧,很值的。
    如果你不想让自己的程序挑三拣四,
    如果你不想让自己被称为废物程序员,呵呵,试一下吧。

本文关键:Delphi Win32 API 使用的特殊情况
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top