program Project1;
uses
Forms,
Windows,
Messages,
IniFiles,
SysUtils,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
const
CM_RESTORE = WM_USER + $1000;{自定义''恢复消息''}
MYAPPNAME = 'Project1';
var
hExeWnd:THandle;
str: string;
IniFile: TIniFile;
begin
try
IniFile := TIniFile.Create(ExtractFilepath(Application.exename) + 'inifile.ini');
str := IniFile.ReadString('name', 'caption', '');
finally
hExeWnd:=FindWindow(MYAPPNAME,pchar(str));
if hExeWnd > 0 then
begin
PostMessage(hExeWnd,CM_RESTORE,0,0);
Application.Terminate;
end
else
begin
Application.Initialize;
Application.Title := '测试';
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
end;
end.
1、对主窗口程序的改动:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IniFiles;
const
CM_RESTORE = WM_USER + $1000;{自定义''恢复消息''}
MYAPPNAME = 'Project1';
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure CreateParams(var Param:TCreateParams); Override;
Procedure RestoreRequest(Var Message:TMessage); Message CM_RESTORE;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
str: string;
IniFile: TIniFile;
begin
IniFile := TIniFile.Create(ExtractFilepath(Application.exename) + 'inifile.ini');
//读入连接字符串
str := IniFile.ReadString('name', 'caption', '');
Form1.Caption := str;
end;
procedure TForm1.CreateParams(var Param: TCreateParams);
begin
inherited CreateParams(Param);
Param.WinClassName := MYAPPNAME;
end;
procedure TForm1.RestoreRequest(var Message: TMessage);
begin
if IsIconic(Application.Handle) = True then
Application.Restore
else
Application.BringToFront;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
str: string;
IniFile: TIniFile;
begin
IniFile := TIniFile.Create(ExtractFilepath(Application.exename) + 'inifile.ini');
//读入连接字符串
str := IniFile.ReadString('name', 'caption', '');
Form1.Caption := str;
end;
end.
参考出处: http://topic.csdn.net/t/20020411/10/637733.html

浙公网安备 33010602011771号