delphi中,让程序只运行一次的方法(2)

文章作者 100test 发表时间 2008:03:31 12:08:35
来源 100Test.Com百考试题网


program MyThreadTest.
  uses
   Windows,
  Forms,
  SysUtils,
  Messages,
   Dialogs,
   Unit1 in Unit1.pas {Form1},
  {$R *.res}
  var
  myMutex,
  FindHid: HWND.
  MoudleName: string.
  function EnumWndProc(hwnd: Thandle. param: Cardinal): bool. stdcall.
  //由于用于api回调函数,请使用windows传统的参数传递方式stdcall
  var
   ClassName, WinMoudleName: string.
   WinInstance: THandle.
  begin
   result := true.
  SetLength(ClassName, 100).
   GetClassName(hwnd, pchar(ClassName), length(ClassName)). //获得当前遍历窗口的类名
   ClassName := pchar(ClassName). //在字符串后加结束符,确定字符串结束
   if UpperCase(ClassName) = UpperCase(TForm1.ClassName) then //比较类名
   begin
   WinInstance := GetWindowLong(hwnd, GWL_HINSTANCE). //获得当前遍历窗口的实例
  setlength(WinMoudleName, 100).
  //获得当前遍历窗口的程序文件名
  GetModuleFileName(WinInstance, pchar(WinMoudleName), length(WinMoudleName)).
  WinMoudleName := pchar(WinMoudleName).
  WinMoudleName :=ExtractFileName(WinMoudleName).
  //MoudleName为工程全局变量,自身程序的文件名
  if UpperCase(WinMoudleName) = UpperCase(MoudleName) then
   begin
   FindHid := hwnd.//FindHid为工程全局变量保存找到的句炳
   result := false. //找到以后就结束遍历
   end.
  end.
  end.
  begin
  // CreateMutex建立互斥对象,并且给互斥对象起一个唯一的名字
   myMutex := CreateMutex(nil, false, hkOneCopy).
   if WaitForSingleObject(myMutex, 0) <> wait_TimeOut then
  //程序没有被运行过
   begin
   Application.Initialize.
   Application.CreateForm(TForm1, Form1).
   Application.Run.
  end else
   begin
   SetLength(MoudleName, 100).
  //获得自己程序文件名
   GetModuleFileName(HInstance, pchar(MoudleName), length(MoudleName)).
   MoudleName := pchar(MoudleName).
  MoudleName := ExtractFileName(MoudleName).
   EnumWindows(@EnumWndProc, 0). //调用枚举函数
   if FindHid <> 0 then
   begin
   ShowWindow(FindHid,SW_RESTORE).
   SetForegroundWindow(FindHid).
   end.
  end.
  end.
  [EnumWindows函数使用]:
  EnumWindows 用来列举屏幕上所有顶层窗口。
  MSDN:
  The EnumWindows function enumerates all top-level windows on the screen by passing the handle to each window。 var arrBaiduCproConfig=new Array(). arrBaiduCproConfig[uid] =65193. arrBaiduCproConfig[n] =exam8cpr. arrBaiduCproConfig[tm] =30. arrBaiduCproConfig[cm] =35. arrBaiduCproConfig[um] =34. arrBaiduCproConfig[w] =575. arrBaiduCproConfig[h] =60. arrBaiduCproConfig[wn] =2. arrBaiduCproConfig[hn] =1. arrBaiduCproConfig[ta] =right. arrBaiduCproConfig[tl] =bottom. arrBaiduCproConfig[bu] =0. arrBaiduCproConfig[bd] =#ffffff. arrBaiduCproConfig[bg] =#ffffff. arrBaiduCproConfig[tt] =#0000ff. arrBaiduCproConfig[ct] =#333333. arrBaiduCproConfig[url] =#666666. arrBaiduCproConfig[bdl] =#ffffff. arrBaiduCproConfig[rad] =1. document.write(baiduCproIFrame()).


相关文章


Java版本和C 版本简单Stack程序
高手讲解:探索C 的秘密之详解extern
在VisualC 应用程序中彻底清除进程
delphi中,让程序只运行一次的方法(3)
delphi中,让程序只运行一次的方法(2)
delphi中,让程序只运行一次的方法(1)
关于Exchange循环日志和备份
让你的Win2008更安全限制匿名访问
让Windows自带的磁盘清理工具功能更强
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛