转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Windows API函数使用技巧         ★★★★

Windows API函数使用技巧

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1808 更新时间:2009/4/23 18:43:54

Windows API函数使用技巧
得到WINDOWS的SYSTEM路径:
    方法:
             var
                  MySysPath : PCHAR ;
             begin
                     GetMem(MySysPath,255);
                     GetSystemDirectory(MySysPath,255);
             end;
    注:MySysPath为SYSTEM路径

 
得到程序的路径
  ExtractFileDir(Application.Exename);


 
察看文件是否存在
  FileExists(FileName:String):Boolean;


 
改变文件扩展名
  ChangeFileExt(FileName:String)


 
得到文件的扩展名
  ExtractFileExt(FileName:String):String;


 
如何取得Windows的临时文件目录?
适合版本:Delphi 3,2.0,1.0

Windows 95 & NT都指定了放置临时文件的目录,然而,用户能改变临时目录的位置而不使用缺省的目录。这篇文章的目的是告诉你如何得到Windows 95 & NT当前的临时目录位置。这个Windows API函数 GetTempPath就是解决这个问题的。其函数原形为:

DWORD GetTempPath(DWORD nBufferLength, LPTSTR lpBuffer);

下面的例子示范如何使用:

function GetTempDirectory: String;

var

TempDir: array[0..255] of Char;

begin

GetTempPath(255, @TempDir);

Result := StrPas(TempDir);

end;


备注:临时目录的确定原则:

1,如果有TMP环境变量则临时目录为TMP指定的目录

2,如果没有TMP环境变量而有TEMP环境变量,则为TEMP变量指定的目录

3,如果TMP和TEMP都没有定义,则取当前目录为临时目录


 
程序不出现在任务栏

  一般Windows 95运行程序时都会在任务栏上出现按钮,如果你的程序是一个监视程序,那么出现按钮就不是明智之举了。要实现该功能就要在OnCreate事件里利用到API函数SetWindowLong
procedure TForm1.FormCreate(sender:TObject);
begin
SetWindowLong(Application,Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
end;


 
改计算机名

改变计算机在网络中的名字,重新启动后才生效
SetComputerName(''''Hello World'''');

 
控制热启动

要使系统的热启动键(Ctrl+Alt+Del)失效,使用以下语句
SystemParametersInfo(SPI_SCREENSAVERRUNNING, 1, 0, 0);
要恢复系统的热启动键(Ctrl+Alt+Del),使用以下语句
SystemParametersInfo(SPI_SCREENSAVERRUNNING, 0, 0, 0);


 
临时路径

有时需要Windows的临时路径来做备份等工作,那么就要知道路径在哪,下面的程序帮你忙:
var aa:pchar;
begin
GetTempPath(20,aa); file://返回路径名
edit1.text:=aa;
end;


 
返回程序执行参数

  有关 Delphi 传入应用程式的命令列参数, 请参考以下的说明:
用ParamCount函数取得命令参数的个数:
呼叫 ParamStr(0), 传回执行档的档名(含路径)
呼叫 ParamStr(n), 传回第n个参数的内容
procedure TForm1.FormCreate(Sender: TObject);
var
sFileName: string;
begin
if ParamCount > 0 then begin (* 有执行参数传入 *)
sFileName := ParamStr(1); (* 取得参数内容 *)
if FileExists(sFileName) then
Memo1.Lines.LoadFromFile(sFileName)
else
Application.MessageBox(''''找不到指定的档案'''', ''''讯息'''', 48);
end;
end;


 
关闭Windows

控制WINDOWS的开关:如关闭WINDOWS,重新启动WINDOWS等, ExitWindowsEx(UINT uFlags,DWORD dwReserved);是实现这一功能的API函数
首先定义常数
const
EWX_FORCE=4; file://关闭所有程序并以其他用户身份登录
EWX_LOGOFF=0; file://重新启动计算机并切换到MS-DOS方式
EWX_REBOOT=2; file://重新启动计算机
EWX_SHUTDOWN=1;//关闭计算机
运行时给How赋值,让他等于EWX_SHUTDOWN或其他,调用以下语句
ExitWindowsEx(How,0);


 
关闭外部应用程序

如何在 Delphi 应用程序中, 去关闭外部已开启的应用程序?
下面给出一段在 Delphi 中关闭“计算器”程序为例:
var
HWndCalculator : HWnd;
begin
// find the exist calculator window
HWndCalculator := Winprocs.FindWindow(nil, ''''计算器''''); // close the exist Calculator
if HWndCalculator <> 0 then
SendMessage(HWndCalculator, WM_CLOSE, 0, 0);
end;


 
得到执行程序的目录

  SysUtils 单元中有 ExtractFileDir 与 ExtractFilePath两个类似的函数, 用哪一个?没有太大的关系。
  不过有以下的差别: ExtractFilePath 传回值的最後一个字元是反斜杠“/”。
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(ExtractFileDir(Application.Exename));
// ie: c:\temp
ShowMessage(ExtractFilePath(Application.Exename));
// ie: c:\temp\
end;
相同点: 如果执行文件在根目录下(如:C:\SAMPLE.EXE)的话, 两者的传回值相同, 且最后一个字符都是“/”。


 
使用GetFileVersionInfo 得到版本信息的例子 
Samples Using GetFileVersionInfo?

回答1:
  procedure GetBuildInfo(var V1, V2, V3, V4: Word);
  var
    VerInfoSize: DWORD;
    VerInfo: Pointer;
    VerValueSize: DWORD;
    VerValue: PVSFixedFileInfo;
    Dummy: DWORD;
  begin
  VerInfoSize := GetFileVersionInfoSize(PChar(ParamStr(0)), Dummy);
  GetMem(VerInfo, VerInfoSize);
  GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo);
  VerQueryValue(VerInfo, ''''\'''', Pointer(VerValue), VerValueSize);
  with VerValue^ do
    begin
    V1 := dwFileVersionMS shr 16;
    V2 := dwFileVersionMS and $FFFF;
    V3 := dwFileVersionLS shr 16;
    V4 := dwFileVersionLS and $FFFF;
    end;
  FreeMem(VerInfo, VerInfoSize);
  end;
------------------------------------------
回答2
If you want a component, check out TVersionInfoResource at
http://www.pobox.com/~bstowers/delphi/ in the My Stuff section. D1/D2/D3/C++B
compatible, freeware with full source code and a small demo.
And you can see the http://www.aye.net/~bstowers/delphi/
另一个component VersionInfo.zip


 
防止程序运行多个例程? 
More than one instance of program?

回答
 This is copied direct from my *.dpr file. You can work it for your own
use.

var
  hMutex : Thandle;
  WaitResult : word;
  BroadcastList : DWORD;
begin
     MessageID := RegisterWindowMessage(''''Check For Choice Previous Inst'''');
// register a message to use later on
     hMutex := createMutex(nil,false,pchar(''''App_Choice'''')); // grab a mutex
handle
     WaitResult := WaitForSingleObject(hMutex,10); // wait to see
if we can have exclusive use of the mutex
     if ( waitResult = WAIT_TIMEOUT ) then // if we can''''t then broadcast
the message to make the owner of the mutex respond
     { request that the running application takes focus }
       begin
          BroadcastList := BSM_APPLICATIONS;
          BroadcastSystemMessage(
BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0); file://32 bit - broadcast the
message to all apps - only a prev inst will hear it.
       end
     else
      begin
      { do the normal stuff}
      Application.Title := ''''Choice Organics Purchase & Sales System'''';
      Application.CreateForm(TMainForm, MainForm);
      Application.Run;
      ReleaseMutex(hMutex); // release the mutex as a politeness
      end;
      CloseHandle(hMutex); // close the mutex handle
end.

This goes in the MainForm

procedure Tmainform.OnAppMessage(var Msg : TMsg ; Var Handled : Boolean);
begin
{ If it''''s the special message then focus on this window}
if Msg.Message = MessageID then // if we get the broadcast message from an
another instance of this app that is trying to start up
   begin
      show;
      WindowState := wsMaximized;
      BringToFront;
      SetFocus;
      Handled := true;
   end;
end;

file://And this goes in the TMainForm.FormCreate ;-

Application.OnMessage:= OnAppMessage;


 
4.得到Win 95 的计算机名字? 

问 How can I learn Windows''''95 Machine Name?

答function ComputerName : String;

var
   CNameBuffer : PChar;
  fl_loaded : Boolean;
  CLen : ^DWord;

begin

    GetMem(CNameBuffer,255);
    New(CLen);
    CLen^:= 255;

    fl_loaded := GetComputerName(CNameBuffer,CLen^);

    if fl_loaded then
      ComputerName := StrPas(CNameBuffer)
    else
      ComputerName := ''''Unkown'''';

    FreeMem(CNameBuffer,255);
    Dispose(CLen);

end;


 
7. 停止一个线程? 
问 Stop A Thread?
回答
You can Terminate your thread in two ways:
1) Assign ThreadDone to OnTerminate when you create it.
   In the Execute method, exit when the terminated property is True.
   At the point where you want to stop, issue the Terminate method.
2) Just call the Suspend method.
After one of these steps you may free the thread.
I hope the following snippets will help.

// -------------------------------------------------------------- //
interface

type
  Txyz = class(TThread)
  published
    procedure Execute; override;
  end;

var
  XYZThread: Txyz;

implementation

procedure Txyz.Execute;
begin
  while True do Application.ProcessMessages;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  XYZThread := Txyz.Create(False);
end;

[1] [2]  下一页


[系统软件]windows下Apache+php+mysql的安装与配置图解  [操作系统]在Windows中玩转Linux操作系统
[操作系统]死马还当活马医:6种方法挽救Windows系统  [聊天工具]四大更新 Windows Live Msn 8.1评测
[聊天工具]Windows Live Messenger最新0683版亮相_联络工具_…  [聊天工具]Windows Live Mail招人爱的N个理由_联络工具_Wind…
[聊天工具]Windows Live Mail Desktop多图欣赏_联络工具_Win…  [聊天工具]OE老了 微软开发新邮件客户端取而代之_联络工具
[聊天工具]Windows Live Messenger中文版试用报告(一)__天极…  [聊天工具]Windows Live Messenger 8 Beta1高清图赏__天极Ye…
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台