Delphi中的Windows API调用

为了能在Windows下快速开发应用程序,Delphi对Windows的控件都进行了很好的封装,这样,我们就可以直接使用Delphi的控件来编写程序,而不用使用格式、调用控制复杂的API来进行开发,为我们节省了大量的开发时间。

如何在Delphi中调用数据类型不兼容的API呢

我们在Delphi中最常用到的一个数据类型是String类型,可API并不支持,那么当我们需要从API中得到一个string类型的值时该如何去做呢?看看下面的程序:

function GetSysPath: string;
var
 sTmp: string;
begin
 result := ’’; 
 //调用WindowsApi得到Windows的系统路径
 SetLength(sTmp,256);
 GetSystemDirectory(PChar(sTmp),256); //这是一个API函数
 SetLength(sTmp,StrLen(PChar(sTmp)));
 result := sTmp + ’\’;
end;

在该函数中,调用了API函数GetSystemDirectory,它的第一个参数是一个指向以NULL结尾字符串的指针,在Delphi中,这种数据类型是PCHAR,所以这里要进行数据类型转换,要写成PChar(sTmp)。 我们再看另外一个例子:

procedure TForm1.ChangeAppIcon;
var
 icon: TIcon ;
begin
 icon := TIcon.Create;
 try
  icon.Handle := ExtractIcon(application.Handle,pchar(’D:\WINNT\regedit.exe’),0); //这是一个API函数
  if icon.Handle <> 0 then
   application.Icon := icon;
  finally
   icon.Free;
 end;
end;
该过程通过API函数得到一个应用程序的图标,来动态改变当前程序的图标。API函数ExtractIcon返回的是一个HICON类型,是一个指向图标的指针,如果写成application.Icon = ExtractIcon(application.Handle, pchar(’D:\WINNT\regedit.exe’), 0); 编译肯定不通过,数据类型不一致
application.Icon属性是Ticon类型的,我们可以创建一个 Ticon,把API的返回值赋给Ticon.Handle,最后将ICON赋值给application.Icon,就解决了我们的问题。

Delphi并不直接支持Windows中的数据类型,只能通过封装的形式来支持。要熟练地调用API函数,我们一定要熟悉Delphi的封装机制,多看、多写,多去实践,那么,我们就能够随心所欲的调用API函数了。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Creating Windows CreateMDIWindow CreateWindow CreateWindowEx RegisterClass RegisterClassEx UnregisterClass Message Processing BroadcastSystemMessage CallNextHookEx CallWindowProc DefFrameProc DefMDIChildProc DefWindowProc DispatchMessage GetMessage GetMessageExtraInfo GetMessagePos GetMessageTime GetQueueStatus InSendMessage PeekMessage PostMessage PostQuitMessage PostThreadMessage RegisterWindowMessage ReplyMessage SendMessage SendMessageCallback SendMessageTimeout SendNotifyMessage SetMessageExtraInfo SetWindowsHookEx TranslateMessage UnhookWindowsHookEx WaitMessage Window Information AnyPopup ChildWindowFromPoint ChildWindowFromPointEx EnableWindow EnumChildWindows EnumPropsEx EnumThreadWindows EnumWindows FindWindow FindWindowEx GetClassInfoEx GetClassLong GetClassName GetClientRect GetDesktopWindow GetFocus GetForegroundWindow GetNextWindow GetParent GetProp GetTopWindow GetWindow GetWindowLong GetWindowRect GetWindowText GetWindowTextLength IsChild IsIconic IsWindow IsWindowEnabled IsWindowUnicode IsWindowVisible IsZoomed RemoveProp SetActiveWindow SetClassLong SetFocus SetForegroundWindow SetParent SetProp SetWindowLong SetWindowText WindowFromPoint Processes and Threads CreateEvent CreateMutex CreateProcess CreateSemaphore CreateThread DeleteCriticalSection DuplicateHandle EnterCriticalSection ExitProcess ExitThread GetCurrentProcess GetCurrentProcessId GetCurrentThread GetCurrentThreadId GetExitCodeProcess GetExitCodeThread GetPriorityClass GetThreadPriority GetWindowThreadProcessId InitializeCriticalSection InterlockedDecrement InterlockedExchange InterlockedIncrement LeaveCriticalSection OpenEvent OpenMutex OpenProcess OpenSemaphore PulseEvent ReleaseMutex ReleaseSemaphore ResetEvent ResumeThread SetEvent SetPr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值