不可以第二次运行程序,只运行一次。
解决方案 »
- PChar转String
- 寻求解决问题的方法,谢谢了!!!!
- 请问TWebBrowser的哪个属性...?
- 请问如何保存和创建数据库?
- 服务器(Win2000)上有一个共享目录,目录中有一个Access2000数据库,如何访问该数据库?请给出详细例子!
- Zlib 可以压缩文件夹吗?
- 关于access 使用的一些问题
- 我用DBGridEh1做排序,写如下代码,还要在DBGridEh1中设置什么属性,才能使他排序?
- ActiveForm中OnStartPage为何没有反应??请帮忙
- 如何取得datamodule 对象表名?
- 怎么不能提问?
- 关于MDIChild窗体释放的问题,请大家赐教。。。俺洗耳恭听。
begin
Hwnd:=FindWindow(nil,'frmmain');
If Hwnd=0 then
begin
.................
Application.Run;
end;
end;
*.dpr文件:program Wine;uses
Forms,
windows,
messages,
MainUnit in 'MainUnit.pas' {MainForm},{$R *.RES}
const
CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
MYAPPNAME = 'My Delphi Program';var
RvHandle : hWnd;
begin
RvHandle := FindWindow(MYAPPNAME, NIL);
if RvHandle > 0 then begin
PostMessage(RvHandle, CM_RESTORE, 0, 0);
Exit;
end;
Application.Initialize;
Application.Title := '酒水管理系统';
Application.CreateForm(TMainForm, MainForm);
Application.CreateForm(TDM, DM);
Application.Run;
end.
implementation
var hnd: THandle;initialization
hnd := CreateMutex(nil, True, 'irgendwaseinmaliges');
if GetLastError = ERROR_ALREADY_EXISTS then Halt;finalization
if hnd <> 0 then CloseHandle(hnd);
end.
const
CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
MYAPPNAME = "My Delphi Program";
并在Form的定义的public节中加入
procedure CreateParams(var Params: TCreateParams); override;
Procedure RestoreRequest(var message: Tmessage); message CM_RESTORE;
在implementation节中加入
{指定窗口名称} procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.WinClassName := MYAPPNAME;
end;{处理“恢复”消息}procedure TForm1.RestoreRequest(var message: Tmessage);
begin
if IsIconic(Application.Handle) = TRUE then
Application.Restore
else
Application.BringToFront;
end;
经过以上修改,程序的主窗口的类名已经被指定了,这是进行判断的基础。一般在程序刚开始运行的时候进行判断,所以还要对DPR文件进行修改。2、对DPR文件的改动在 uses 节中添加 windows、messages这两个单元加入下列语句,注意两个文件中常量CM_RESTORE和MYAPPNAME的定义必须一致
const
CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
MYAPPNAME = "My Delphi Program";
var
RvHandle : hWnd;
将下列语句插到程序最前部(在Application.Initialize之前)
RvHandle := FindWindow(MYAPPNAME, NIL);
if RvHandle > 0 then
begin
PostMessage(RvHandle, CM_RESTORE, 0, 0);
Exit;
end;这段程序的意思是如果找到一个类名相同的窗口,则向该窗口发送一个消息,并退出,而本例中原窗口收到该消息后会自动激活或从图标还原,从而达到了避免二次运行且能自动调出前一例程的目的。
摘帖,
findwindow,createmutex,globaladdatom都可以在程序启动时将Application的Title特性字段的值暂时改变。 利用Windows API函数FindWindows()查找窗口
恢复Application的Title值
上述步骤一般在主Form的OnCreate事件中实现,示例如下: procedure TForm1.FormCreate(Sender: TObject);
var
ZAppName: array[0..127] of char;
Hold: String;
Found: HWND;
begin
Hold := Application.Title;
Application.Title := 'OnlyOne'
+ IntToStr(HInstance); // 暂时修改窗口标题
StrPCopy(ZAppName, Hold); // 原窗口标题
Found := FindWindow(nil, ZAppName); // 查找窗口
Application.Title := Hold; // 恢复窗口标题
if Found<>0 then begin
// 若找到则激活已运行的程序并结束自身 ShowWindow(Found, SW_RESTORE);
Application.Terminate;
end;
end;
{***************************************************************}
{ XJustOne Component for Delphi 32 }
{ Version: 2.6 }
{ Author: Gennady Makeev ([email protected]) }
{ Improvement: Aleksey Kuznetsov ([email protected]) }
{ WWW: http://www.utilmind.com }
{***************************************************************}
{ This component let start only one copy of your application. }
{***************************************************************}
{ PROPERTIES: }
{ SwitchToPrevious: Boolean - Activate the previous instance }
{ if exist }
{ EVENTS: }
{ AlreadyExist - occurs if first instance of this application }
{ is already loaded }
{***************************************************************}unit JustOne;interfaceuses
Windows, SysUtils, Classes, Controls, Forms;type
TXJustOne = class(TComponent)
private
FSwitchToPrevious: Boolean;
FAlreadyExist: TNotifyEvent;
Mutex: hWnd;
IsCreated: Boolean;
public
procedure Loaded; override;
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
published
property SwitchToPrevious: Boolean read FSwitchToPrevious write FSwitchToPrevious;
property AlreadyExist: TNotifyEvent read FAlreadyExist write FAlreadyExist;
end;procedure Register;implementationprocedure TXJustOne.Loaded;
var
PrevInstHandle: THandle;
AppTitle: Array[0..$100] of Char;
begin
inherited Loaded;
StrPCopy(AppTitle, Application.Title);
IsCreated := False;
Mutex := CreateMutex(nil, False, AppTitle);
if (GetLastError = ERROR_ALREADY_EXISTS) or
(Mutex = 0) then
begin
if Assigned(FAlreadyExist) then FAlreadyExist(Self); if FSwitchToPrevious then
begin
SetWindowText(Application.Handle, ''); PrevInstHandle := FindWindow(nil, AppTitle);
if PrevInstHandle <> 0 then
if IsIconic(PrevInstHandle) then
ShowWindow(PrevInstHandle, SW_RESTORE)
else
BringWindowToTop(PrevInstHandle);
SetForegroundWindow(PrevInstHandle);
end; Application.ShowMainForm := False;
Application.Terminate;
end;
IsCreated := True;
end;constructor TXJustOne.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FSwitchToPrevious := True;
end;destructor TXJustOne.Destroy;
begin
if IsCreated then
CloseHandle(Mutex);
inherited Destroy;
end;procedure Register;
begin
RegisterComponents('UtilMind', [TXJustOne]);
end;end.