当前位置:主页>销售管理软件> 列表

怎样实现找到软件已运行,并且激活它,还要给它传递参数 找免费进销存管理软件

记账软件版1楼: 像PhotoShop,RealPlayer等大型软件都有个功能,比如我用RealPlayer放音乐时,如果再打开第二首歌时,它是激活正在播放歌曲的哪个窗口,并且把第二首歌的地址传给它播放,我知道可以用参数传过去,但是我传过去后,第一次是运行程序,第二次是重新又打开第二个程序窗口了

2楼: 我说一种方法,也是我自己实现了的

1.设置程序只能启动一个实例,并且在程序中可以截获处理WM_COPYDATA消息(将收到的字符串当成文件名处理)
2.在启动的时候判断是否已经存在实例,若没有则正常启动
若有则找到已经存在的实例的窗口句柄 发送WM_COPYDATA消息给此窗口,内容是ParamStr(1)(比如文件名) 如记账软件

3楼: 这是个老话题了,需要做的是用findwindow寻找前一实例,然后通过共享内存或者发送wm_copydata传入参数及信息.
相关代码可以自己去查

4楼: 小神通,请问检索什么关键字,可以找的到以前的代码

5楼: eg. 一个实例

http://www.delphibbs.com/delphibbs/dispq.asp?lid=2938412

6楼: 你好,谢谢你的回答,哪个帖子只讲了怎么判断一个实例的,我现在想知道如果已存在一个实例,怎么样激活前一个实例并传递参数给它,你前面讲到的用WM_COPYDATA消息,我觉得可以,是不是用SendMessage发送,WM_COPYDATA是什么消息,传递的参数写在那

记账软件版7楼: program PRichplayer;

uses
Windows,
Forms,
SysUtils,
Messages,
RichFunc,
Unit_RichPlayer in ''Unit_RichPlayer.pas'' {RichPlayer},
MPGRectfrm in ''MPGRectfrm.pas'' {frmMPGRect},
RichMixerControl in ''RichMixerControl.pas'' {frmRichMixerControl},
RealPlayerfrm in ''RealPlayerfrm.pas'' {frmRealPlayer},
PlayListfrm in ''PlayListfrm.pas'' {frmPlayList};

{$R *.res}
var
Rmoc3260: string; // ''rmoc3260.dll''; // real dll
FlashOCX: string; // ''Flash.ocx''; // real dll
AppPathName: string;

procedure SendWM_COPYDATA(const ClassName, WName: string);
var
S: pchar;
H: hwnd;
strTemp,
FilePath: string;
I: Integer;
Buf: tagCOPYDATASTRUCT;
begin
H := FindWindow(Pchar(ClassName), Pchar(WName));
if H <> 0 then
begin
GetMem(S, 255);
if ParamCount <> 0 then //读取命令行file list
begin
FilePath := ExtractFilePath(ParamStr(0));
for I := 0 to ParamCount - 1 do


begin
strTemp := ParamStr(I + 1);
if FileExists(strTemp) then
begin
if pos('':'', strTemp) = 0 then
StrPCopy(S, FilePath + strTemp)
else
StrPCopy(S, strTemp);
Buf.lpData := S;
Buf.cbData := 255;
Buf.dwData := 255;
SendMessage(H, WM_COPYDATA, 0, Integer(@Buf));
end; // if FileExists(strTmp)
end; // for i
end;
end;
end;

begin

if TRichFunc.ChkWindow then
begin
SendWM_COPYDATA(''TRichPlayer'', ''RichPlayer'');
Halt;
end;
Application.Initialize;
Application.CreateForm(TRichPlayer, RichPlayer);
Application.CreateForm(TfrmMPGRect, frmMPGRect);
Application.CreateForm(TfrmRichMixerControl, frmRichMixerControl);
Application.CreateForm(TfrmPlayList, frmPlayList);
Application.CreateForm(TfrmRealPlayer, frmRealPlayer);
Application.Run;
end.
传文件名到别的程序里去:
procedure SendWM_COPYDATA(const ClassName, WName: string);
var
S: pchar;
H: hwnd;
strTemp,
FilePath: string;
I: Integer;
Buf: tagCOPYDATASTRUCT;
begin
H := FindWindow(Pchar(ClassName), Pchar(WName));
if H <> 0 then
begin
GetMem(S, 255);
if ParamCount <> 0 then //读取命令行file list
begin
FilePath := ExtractFilePath(ParamStr(0));
for I := 0 to ParamCount - 1 do
begin
strTemp := ParamStr(I + 1);
if FileExists(strTemp) then
begin
if pos('':'', strTemp) = 0 then
StrPCopy(S, FilePath + strTemp)
else
StrPCopy(S, strTemp);
Buf.lpData := S;
Buf.cbData := 255;
Buf.dwData := 255;
SendMessage(H, WM_COPYDATA, 0, Integer(@Buf));
end; // if FileExists(strTmp)
end; // for i
end;
end;
end;

收文件名
procedure WMCopyData(var Message: TWMCopyData); message WM_COPYDATA;

procedure TfrmReal.WMCopyData(var Message: TWMCopyData);
var //接收另一实例发来的文件名并打开
sFileName: string;
begin
if Message.CopyDataStruct.cbData = 0 then
begin
if IsIconic(Application.Handle) then
Application.Restore;
end
else
begin
sFileName := StrPas(PChar(Message.CopyDataStruct.lpData));
if FileExists(sFileName) then
begin
if RichFunc1.ChkFileExt(sFileName, [''.rm'', ''.rmm'', ''.ram'',''.rmvb'']) then
begin
RealAudio1.SetSource(sFileName);
RealAudio1.DoPlay;
RealAudio1.Visible := true;
RealAudio1.Align := alClient;
//ShowMessage(sFileName)
end
else RichFunc1.MsgBox(''不支持此文件播放: '' + sFileName, MB_ICONWARNING);
SetForegroundWindow(Self.Handle);
Application.Restore;
end;
end;
Message.Result := 1;
end;

// ==========================================================================================

// Version : V1.0
// Author : JinFeng
// Create by : 2005-01-21
// Function : 运行一个程序实例, 如果存在原有的实例将其恢复为Windows 活动窗口
// Remark : IDE: Delphi 6.0 and Win2000
var
MyAppName,
MyClassName: array[0..255] of Char;
NumFound: Integer;
LastFound, MyPopup: HWND;
class function TRichFunc.ChkWindow: Boolean;
function ChkAllWindows(Handle: HWND; Temp: LongInt): BOOL; stdcall;
var
WindowName,
ClassName: array[0..255] of Char;
begin
if GetClassName(Handle, ClassName, SizeOf(ClassName)) > 0 then
if StrComp(ClassName, MyClassName) = 0 then
if GetWindowText(Handle, WindowName, SizeOf(WindowName)) > 0 then
if StrComp(WindowName, MyAppName) = 0 then
begin
Inc(NumFound);
if Handle <> Application.Handle then
LastFound := Handle;
end;
result := true;
end;
begin
NumFound := 0;
LastFound := 0;


Result := false;
GetWindowText(Application.Handle, MyAppName, SizeOf(MyAppName));
GetClassName(Application.Handle, MyClassName, SizeOf(MyClassName));
EnumWindows(@ChkAllWindows, 0);
if NumFound > 1 then //不是一个窗口
begin
Result := true;
MyPopup := GetLastActivePopup(LastFound);
BringWindowToTop(LastFound);
if IsIconic(MyPopup) then
ShowWindow(MyPopup, SW_RESTORE)
else
SetForegroundWindow(MyPopup);
end
end;
// end function 运行一个程序实例
//==========================================================================

8楼: 工程文件里:
procedure SendData;
var
lcdsFile: TCopyDataStruct;
lsFile: string;
h: THandle;
begin
h := 已经运行的实例的窗口句柄;

lsFile := ParamStr(1);
with lcdsFile do begin
dwData:= 自己定义一个整数(比如100);
cbData:= Length(lsFile) + 1;
lpData:= @lsFile[1];
end;
SendMessage(h, WM_COPYDATA, 0, Cardinal(@lcdsFile));
SendMessage(h, WM_SYSCOMMAND, SC_RESTORE, 0);

end;


if 已经存在实例了 then
SendData
else begin
Application.Initialize;
Application.Title := ''XXXXXX'';
Application.CreateForm(TFRMMain, FRMMain);
Application.Run;
end;


窗口中:
private
procedure WMCOPYDATA(var Msg: TWMCopyData); message WM_COPYDATA;



procedure TFRMMain.WMCOPYDATA(var Msg: TWMCopyData);
var
lsTemp: string;
begin
with Msg.CopyDataStruct^ do begin
lsTemp := StrPas(PChar(lpData)); //lsTemp就是得到的文件名
if (dwData = 自己定义一个整数(比如100)) then begin
自己的处理;
end;
end;
end;

9楼: smithcouple,非常感谢,可以了,dwData是干什么的

10楼: 好像第二次激活的窗口不能最小化呢

11楼: 我用上面哪个例子激活前一个窗口更好
Application.Restore;
就能最小化了[:)]

12楼: dwData相当于Tag,可以用这个值进行确认,比如我定义只有dwData=2006的时候,我才认为这个消息是我需要的。并不是必须的一个参数。 如免费进销存管理软件

13楼: 接受答案,谢谢