Solved

Loading a second file in MDI-App

Posted on 1998-10-16
8
159 Views
Last Modified: 2010-04-06
I registered a file-type XYZ with my MDI-application,
so that a file can be loaded from Windows Explorer with a double-click.
If my app is already running and a second file is opened
via double-click, a second app is being opened.
But I want a second MDI-child to be opened in the first app.
Can someone give me a source sample for this ??
I don't know if this is a hard question, but I need a
quick (and working) answer, so it's worth the points for me.
0
Comment
Question by:Tulip
  • 5
  • 3
8 Comments
 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1343143
Hello Tulip,

what you need is a three step procedure...

1. find out that another instance of your program is already running
2. send a message to the first instance of your program
3. process the message (i.e. open the file in another MDI window)

I'm preparing an example and will mail it in soon

Roadrunner
0
 
LVL 1

Accepted Solution

by:
Roadrunner100598 earned 200 total points
ID: 1343144
Back again

I always wanted to know how to share memory between applications...

so here it comes...
-----------------------------------------------------------
Your DPR should look similar to this...

program Demo;

uses
  Forms,
  Windows,
  Dialogs, SysUtils,
  ufMain in 'ufMain.pas' {fMyMDIMainForm};

{$R *.RES}

function CheckPreviousInstance:boolean;
{ Returns FALSE if a previous instance is running... }
var
  H         : HWND;
  st        : string;
  HFilename : THandle;
  Filename  : pointer;
begin
  H:=FindWindow('TfMyMDIMainForm',NIL);
  Result:=(H=0);
  if not Result then begin
    st:=GetCommandLine+#0;
    { Get a shared memory block }
    HFilename:=CreateFileMapping(-1, { use paging file }
                                 NIL,
                                 PAGE_READWRITE,
                                 0, length(st), 'SharedFilename');
    Filename:=MapViewOfFile(HFilename, FILE_MAP_WRITE, 0,0,0);
    move(st[1], Filename^, length(st));
    UnmapViewOfFile(Filename);

    MessageDlg(IntToStr(longint(HFilename)), mtInformation, [mbOK], 0);
    SendMessage(H, WM_USER+UM_OpenNewMDIChild, 0, longint(HFilename));
    MessageDlg(IntToStr(longint(Filename)), mtInformation, [mbOK], 0);
    CloseHandle(HFilename);
  end;
end;

begin
  Application.Initialize;
  if not CheckPreviousInstance then
    EXIT;

  Application.CreateForm(TfMyMDIMainForm, fMyMDIMainForm);
  Application.Run;
end.
-------------------------------------------------------------------------------
And the define a message processing method in your TfMyMDIMainForm

const
  UM_OpenNewMDIChild = 42;

type
  TfMyMDIMainForm = class(TForm)
    Label1: TLabel;
  private
    { Private-Deklarationen }
    procedure WMUserMsg(var Msg:TMessage); message WM_USER+UM_OpenNewMDIChild;
  public
    { Public-Deklarationen }
  end;

var
  fMyMDIMainForm: TfMyMDIMainForm;

implementation

{$R *.DFM}

procedure TfMyMDIMainForm.WMUserMsg(var Msg:TMessage);
var
  HFilename : THandle;
  Filename  : pChar;
begin
  { Get a handle to the shared memory }
  HFilename:=OpenFileMapping(FILE_MAP_READ, true, 'SharedFilename');
  { Get a pointer }
  Filename:=MapViewOfFile(HFilename, FILE_MAP_READ, 0,0,0);
  { Do something with the filename... }
  Label1.Caption:=Filename;
  { Release pointer }
  UnmapViewOfFile(Filename);
  { Release handle }
  CloseHandle(HFilename);
end;
--------------------------------------------------------------------

CheckPreviousInstance looks for a class with the name of your programs main window (so this has to be unique!). Then a shared memory block is created and the command line written in it. After that a message is posted to the first instance of your program.
The message is processed by your main form.

I did not include any code for creating mdi-childs, loading file etc. for I'm shure you already have written some.

If you want more information on memory mapped files there is
Don Taylor et. al., 'High performance delphi 3 programming', Coriolis Group Books 1997


Thanks for the question, I learned a lot answering it!


Roadrunnner
0
 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1343145
One thing I forgot... this was done under Windows NT 4, I could not test the code with 95/98 - please tell me if it worked on your OS!
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:Tulip
ID: 1343146
Hi Roadrunner!

Thanks for your detailed answer, it looks very good.
I'm gonna try it out tomorrow because for today I'm running out of time.


0
 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1343147
There is one additional hint that slipped my mind when answering:

The program searches for the classname of the main window. As long as your form defining this window is loaded in delphi there already is a form with this name. So, if you want to test this, close your main form's unit first (just closing the form is not enough - it is only invisible, but still created)!
0
 

Author Comment

by:Tulip
ID: 1343148
Thank you Roadrunner !
I tested the source and it works perfectly. Since the Delphi-made source code comments
are in German, I suppose you are living in a German speaking country, just like me.
So again:  danke schoen
0
 

Author Comment

by:Tulip
ID: 1343149
Oh, I forgot to tell you my OS:

Win NT 4 with SP3
0
 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1343150
Gern geschehen
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question