Solved

Loading a second file in MDI-App

Posted on 1998-10-16
8
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…

696 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