关于禁止程序重复启动的另一种需要与实现
2013-09-30 11:13
260 查看
关于禁止程序重复启动的另一种需要与实现
手头的程序需要禁止重复启动, 但需要保留新的、关闭旧的.我想还是从主窗口的类名下手吧; 写了一个函数, 在 OnCreate 中调用即可:
{ 函数 } procedure CloseSameClassNameWindow(ACurrentWindow: HWND; const AClassName: string); var h: HWND; buf: array[0..255] of Char; begin h := ACurrentWindow; while h > 0 do begin h := GetWindow(h, GW_HWNDNEXT); GetClassName(h, buf, Length(buf)); if buf = AClassName then begin SendMessage(h, WM_CLOSE, 0, 0); Break; end; end; end; { 调用 } procedure TForm1.FormCreate(Sender: TObject); begin CloseSameClassNameWindow(Handle, ClassName); end;
从程序文件中控制更简单, 一句话:
program Project1; uses Forms, Windows, Messages, Unit1 in 'Unit1.pas' {Form1}; {$R *.res} begin Application.Initialize; SendMessage(FindWindow('TForm1', nil), WM_CLOSE, 0, 0); //假如主窗体的类名是 TForm1 Application.MainFormOnTaskbar := True; Application.CreateForm(TForm1, Form1); Application.Run; end.
相关文章推荐
- 正确理解ContentPresenter
- 填报表用ajax实现关联单元格自动填充
- unity IEnumerator 协同程序
- 教你如何用路由器连接网页登陆式校园网(by liukun321 咕唧咕唧)
- ubuntu之Awk使用详解
- 用户卡的发展变革(二)
- Delphi XE2 新增 System.Zip 单元, 可用一句话压缩整个文件夹了
- leetcode:Palindrome Partitioning (字符串分割成回文串的所有方式) 【面试算法题】
- perl写日志
- 【算法刷题】poj 2192 Zipper
- Win7 windows凭证无法保存密码的解决方法
- Singleton设计模式的一种见解
- Oracle 11G函数整理(日期函数)
- 代码段(.text段)太长的解决办法
- NSValue包装自定义结构体
- System.exit(0)作用
- Singleton设计模式的一种见解
- Delphi XE2 之 FireMonkey 入门(45Finally) - 结题与问题
- firefox 的插件配置注意事项
- IDL与Matlab的比较