delphi调用windows api

在Delphi中调用Windows API 函数十分方便,只需在单元的uses段加入Windows 单元名即可(对于由Delphi自动创建的单元,该项工作已经完成)。单元Windows.pas 已经由Delphi编写并直接提供开发者引用。笔者在开发Windows 应用时对于几个特别的API 函数总结了一些经验供大家参考。以下开发经验均在Windows 9x中适用。 

Delphi已经封装了copyfile,在windows.pas中.(uses Windows就是引用windows.pas


一、关闭系统 
  对于某些特殊的应用程序,可能需要进行关闭系统的操作,可以调用函数ExitWindows 或者ExitWindowsEx 来实现。 

  在Delphi中函数ExitWindows 的语法如下: 

  function ExitWindows(dwReserved: Cardinal; Code: Word): LongBool; 

  如果调用该函数成功,则返回True,否则返回False ;成功调用该函数将执行“关闭所有程序并以其他身份登录(Windows 95)”或“注销(Windows 98)”操作。其中的参数dwReserved和参数Code都必须是0 。 

  例如,要实现注销操作,只需要在适当的位置写入如下语句: 

  ExitWindows(0, 0); 

  执行关闭计算机或重新启动计算机的操作,可以通过调用函数ExitWindowsEx 来实现。 

  在Delphi中函数ExitWindowsEx 的语法如下: 

  function ExitWindowsEx(uFlags, dwReserved: Cardinal): LongBool; 

  如果调用该函数成功,则返回True,否则返回False ;其中的参数uFlags的不同将决定执行不同的关闭操作: 

  EWX_FORCE 强制终止所有的进程。如果设置了此参数,Windows 将不会向正在运行的应用程序发送消息WM_QUERYENDSESSION和WM_ENDSESSION ,这将可能导致数据丢失。因此,除非特别紧急或特别的情况(如应用程序死锁或系统资源濒临崩溃等),不要使用该参数。 

  EWX_LOGOFF 关闭所有与当前调用函数ExitWindowsEx 相关的进程并注销用户。 

  EWX_POWEROFF关闭系统并关闭电源。其中,关闭电源要求系统应支持power-off 特性。 

  EWX_REBOOT 关闭系统并重新启动计算机。 

  EWX_SHUTDOWN在安全关闭电源后关闭系统。该操作将所有的缓存刷新到磁盘并停止所有正在运行的进程。 

  参数dwReserved被函数所忽略,可以是任意值。 

  例如,要强制注销,可以通过如下语句实现: 

  ExitWindowsEx(EWX_FORCE, 0); 

  再如,要重新启动计算机,可以通过如下语句实现: 

  ExitWindowsEx(EWX_REBOOT, 0); 

  需要提醒大家注意的是,在很多情况下,只有参数EWX_SHUTDOWN能够成功关闭计算机电源。 

二、弹出模式化对话框 
  在Delphi中,提供模式化对话框的方法很多,现对几种比较常用的方法做如下比较: 

  1) 直接调用Windows API 函数MessageBox 

  该方法一般情况下使用正常,而且根据不同的操作系统将显示相应的中文或英文按钮标题。而在笔者曾经开发的系统中,偶尔会出现该函数没有锁定Handle所指的窗口的情况。通过观察发现,当入口参数的窗口句柄是Application.MainForm的窗口句柄时,没有出现类似情况。因此,建议大家甚用。 

  2) 调用Delphi提供的函数MessageDlg 

  该方法的不足之处是,对话框的按钮标题是英文。 

  3) 调用Delphi提供的过程ShowMessage 

  该方法默认将应用程序的标题作为对话框的标题,并且仅仅是信息提示框,不太灵活。 

  4) 调用函数Application.MessageBox 

  该方法是笔者强力推荐使用的方法。该方法的使用效果与Windows API 函数MessageBox完全相同,并且不会出现有时没有锁定当前活动窗口的情况。 

  例如: 

  Aplication.MessageBox(Handle, '这是一条警告信息。', '警告', MB_OK + MB_ICONEXCLAMATION); 

三、防止多次载入应用程序实例 
  某些应用程序需要禁止用户载入多次实例。比如,控制面板中的应用程序,不管用户打开多少次,同一应用程序只有一个实例,而且每一次试图重复打开都会自动激活已经存在的实例。 

  Windows API 提供了函数FindWindow,可以是应用程序在启动时检查自己是否已经存在。 

  该函数在Delphi中的语法是: 

  function FindWindow(lpClassName: PChar, lpWindowName: PChar): HWND; 

  其中,参数lpCalssName 是要查找的窗口的类的名称,参数lpWindowName是要查找的窗口的标题(Caption)。 如果找到了相应的窗口实例,将返回一个非0 的该窗口句柄的整型值,否则返回0 。因此,只要判断应用程序的主窗口(或者伴随着应用程序存在而存在的窗口)是否存在就可以判断是否已经有实例存在了。 

  例如: 

  H := FindWindow('TForm1', nil); 

  if H = 0 then begin 

   ShowMessage('没有发现相同的应用程序实例。'); 

   //加入加载应用程序的语句 

   //... 

  end else begin 

   ShowMessage('应用程序已经加载。'); 

   SetActiveWindow(H); 

  end; 

  其中,参数lpWindowName的位置以Delphi保留字nil 代替,是因为窗口的标题可能在应用程序中是变化的。Windows API 函数SetActiveWindow 用于指定活动窗口。


实际测试

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin

//两种方式都可以
//Windows.CopyFile('C:\1.txt','C:\2.txt',false);
CopyFile('C:\1.txt','C:\2.txt',false);

end;

end.


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值