防止程序重复执行(100分)

  • 主题发起人 主题发起人 yaosi
  • 开始时间 开始时间
Y

yaosi

Unregistered / Unconfirmed
GUEST, unregistred user!
我是菜鸟,请教各位大侠一个简单的问题,那就是如何防止程序重复执行<br>能给点代码看看最好!能执行马上给分!
 
[blue][/blue]用下面的代码试试看<br>procedure TForm1.FormCreate(Sender: TObject); <br>var <br>ZAppName: array[0..127] of char; <br>Hold: String; <br>Found: HWND; <br>Found: HWND; <br>begin <br>Hold := Application.Title; <br>Application.Title := 'OnlyOne' <br>+ IntToStr(HInstance); <br>StrPCopy(ZAppName, Hold); <br>Found := FindWindow(nil, ZAppName); <br>Application.Title := Hold; <br>if Found&lt;&gt;0 then begin <br>ShowWindow(Found, SW_RESTORE); <br>end; <br>end;
 
我试了,还是报错啊就在倒数第二个end处
 
不好意思,在倒数第二个end前加上<br>Application.Terminate;<br>原理:在程序启动时将Application的Title特性字段的值暂时改变。 <br>利用Windows API函数FindWindows()查找窗口 <br>恢复Application的Title值
 
procedure TForm1.FormCreate(Sender: TObject); <br>var <br>&nbsp;ZAppName: array[0..127] of char; <br>&nbsp;Hold: String; <br>&nbsp;Found: HWND; <br>begin <br>&nbsp; Hold := Application.Title; <br>&nbsp; Application.Title := 'OnlyOne' + IntToStr(HInstance); <br>&nbsp; StrPCopy(ZAppName, Hold); <br>&nbsp; Found := FindWindow(nil, ZAppName); <br>&nbsp; Application.Title := Hold; <br>&nbsp;if Found&lt;&gt;0 then <br>&nbsp; &nbsp;begin <br>&nbsp; &nbsp; ShowWindow(Found, SW_RESTORE); <br>&nbsp; &nbsp; Application.Terminate;<br>&nbsp; &nbsp; end; <br>end;
 
多人接受答案了。
 
后退
顶部