Link to home
Start Free TrialLog in
Avatar of lucroels
lucroels

asked on

How to open a file by doubleclicking when program is running

Hi,
I'm new to this list, so please bear with me.
I've been lurking for some time and am really impressed by the level of expertise of the anwers.

The problem:
I can start up the app and show the file doubleclicked by the user in the explorer by using the Paramcount and ParamStr functions.

I prohibit the running of a second instance of the app with the following code (in the program source):
(BTW: Quantum is the name of the app)

var
  hMutex : THandle;
  runningApp : THandle;
begin
  hMutex := CreateMutex (nil, false, 'Quantum');
  if (GetLastError = ERROR_ALREADY_EXISTS) Or (hMutex = 0) then
    begin
      SetWindowText(Application.Handle,'');
      runningApp := FindWindow (nil, 'Quantum');
      if not isWindowVisible (runningApp) then
        PostMessage (runningApp, wm_User, 0, 0);
      SetForegroundWindow (runningApp);
    end
  else
    begin
      {standard program init}
    end;

In response to the (Post)Message I do a simple 'Application.Restore;'

This works, whenever the user tries to startup another instance or doubleclicks a 'Quantum' file the running app comes to the front.

What eludes me completely is how to open the file that was doubleclicked when my app is running.

I hope someone can point me in the right direction.

TIA,

luc roels
ASKER CERTIFIED SOLUTION
Avatar of robert_marquardt
robert_marquardt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of geobul
geobul

Hi,

Another way:

1. Add one TEdit named edtExternal and one TButton named btnExternal. Place them on the form. Set their TabStop to false. Then move them over a Panel for example and using popup menu select 'Send to back'. Doing so these two controls will became hidden. In OnClick event of btnExternal add your code for opening a new file which name is in edtExternal.Text. Then add the following in your code above:

var
  runningApp, extEdt, extBtn : THandle;
  FileName: string;
begin
  ...
  FileName := // get it from ParamStr
  runningApp := FindWindow (nil, 'Quantum');
  // set edtExternal.Text = FileName
  extEdt := FindWindowEx(runningApp, 0, nil, 'edtExternal');
  if IsWindow(extEdt) then begin
    ShowMessage('aaa');
    SendMessage(extEdt,WM_SetText,0,Integer(FileName));
    // press btnExternal
    extBtn := FindWindowEx(runningApp, 0, nil, 'btnExternal');
    if IsWindow(extBtn) then begin
      ShowMessage('bbb');
      PostMessage(extBtn, BM_CLICK, 0, 0);
    end;
  end;
  ...
 
Regards, Geo
Remove these two ShowMessage calls from the code above, please. I'm sorry.
Avatar of lucroels

ASKER

WM_COPYDATA was definitely the way to go. It took me a while but I got it working. I first thought to rate you a B but then reconsidered because your comment was right up on the mark and by forcing me to go out looking myself for the details I did learn a few things... and I did not want to spoil your perfect track record. You once again thanks very much.