Delphi开发中Windows API函数的应用

Delphi作为快速开发Windows95/NT下应用程序的工具,已经为越来越多的开发者采用。但是,如果要开发出专业的Windows应用软件,还需要使用大量的Windows API函数,以下是笔者开发管理软件中的几个应用实例。

一、判定Windows版本

众所周知,Windows95/
NT某些地方有些差别,为了使应用程序避免出现因为系统不符合而导致的错误,有必要自动判定系统版本。这可以通过API函数getversion实现。

TForm1.FormCreate(Sender:Tobject);

var ver:integer;//版本号  major:integer;//主版本号  minor:integer;//次版本号

begin

ver:=getversion();//获得版本号

major:=ver and 255;//获得主版本号

minor:=(ver and 255*256)div 256;

//获得次版本号

showmessage(‘系统版本号是:’+inttostr(ver));

//显示版本号

showmessage(‘系统主版本号是:'+inttostr(major));//显示主版本号

showmessage(‘系统次版本号是:'+inttostr(minor));//显示次版本号

end;

二、判定Windows安装目录

一般Delphi开发出来的程序需要知道系统的安装目录,以便将帮助文件、动态链接库等系统部分拷贝到系统目录下,为了使安装程序能够在用户任意设定安装目录的情况下均能正常运行,需要使用API函数GetWindowsDirectory、GetSystemDirectory来实现。

procedure TForm1.FormCreate(Sender:Tobject);

var windir:pchar;

sysdir:pchar;

begin

getmem(windir,256);//获得内存

getmem(sysdir,256);//获得内存

GetWindowsDirectory(Windir,128);//获得安装目录

GetSystemDirectory(sysdir,128);//获得系统目录

showmessage(‘Windows目录是:'+windir);

//显示安装目录

showmessage(‘System目录是:'+sysdir);

//显示系统目录

freemem(windir,256);//释放内存

freemem(sysdir,256);//释放内存

end;

三、在程序中重新启动Windows系统

通常在程序中需要将系统重新启动,这时可以使用ExitWindowsEx函数来实现。

procedure TForm1.Button1Click(Sender:Tobject);

begin

ExitWindowsEx(EWX_FORCE or EWX_LOGOFF,0);

//强行结束当前登录的用户进程

ExitWindowsEx(EWX_FORCE or EWX_POWEROFF,0);

//强行关机

ExitWindowsEx(EWX_FORCE or EWX_REBOOT,0);

//强行重新启动机器

end;

四、调用其他程序

通常,用户希望能够在一个应用程序中直接调用外部程序,这可以通过winexec函数来实现。

procedure TForm1.Button1Click(Sender.Tobject);

begin

winexec(‘c:/pwin97/notepad.exe c:/config.dos',9);

end

 

<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
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、付费专栏及课程。

余额充值