Solved

MDI,create child window when command line execution.

Posted on 1998-09-08
5
225 Views
Last Modified: 2010-04-06
Hi!

I'm making a simple MDI text editor.It based on text editor
sample program in '..\delphi 3\demos\doc'.

How can I create child window when parent window is already
running?
My question is like this.
1.running my 'test.exe sample.txt'
2.at this time my test.exe has one child window about
  sample.txt.
3.at windows explore,I click right mouse button at some txt
  file and send to my test.exe.
4.My test.exe running already and how can I create just
  only a child window? I don't want to execute another
  test.exe.

Sorry about my poor english...
0
Comment
Question by:rnxotj
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Jacco earned 60 total points
ID: 1338992
You need to prevent multiple applications to start. I use the PrevMultiple unit for that.

Change the UniqueAppStr to the desired value, any string will do.
When the app is called for the second time a globalatom is added with the filename. The first instantiated finds the global back by its number, and reads it.

I tried this with the standard Delphi 2 demo of TextEdit and added the following things:

in  MDIFrame.Pas:

procedure TFrameForm.Open(const s : String);
begin
  with TEditForm.Create(Self) do
    Open(s);
end;

in TextEdit.Dpr

begin
  Application.CreateForm(TFrameForm, FrameForm);
  if ParamCount<>0 then
    FrameForm.Open(ParamStr(1));
  Application.Run;
end.

This is the PrevMultiple unit. Pay attention that it uses the MDIFrame unit!! And MDIFrame has an extra method Open!!!

*** start of code ***
unit UPrevMultiple;

interface

uses
  Forms, Windows, Dialogs, SysUtils;

const
  MI_NO_ERROR          = 0;
  MI_FAIL_SUBCLASS     = 1;
  MI_FAIL_CREATE_MUTEX = 2;

function GetMIError : Integer;

implementation

uses
  MDIFrame;

const
  UniqueAppStr : PChar = 'My Text Editor';

var
  MessageId : Integer;
  WProc     : TFNWndProc = nil;
  MutHandle : THandle = 0;
  MIError   : Integer = 0;

function GetMIError: Integer;
begin
  Result := MIError;
end;

function NewWndProc(Handle : HWND; Msg : Integer; wParam, lParam : LongInt) : LongInt; StdCall;
var
  s : PChar;
begin
  // Initialize result
  Result := 0;
  // if this is the registered message ...
  if Msg = MessageID then begin
    // if main form is minimized, normalize it
    if Application.MainForm.WindowState = wsMinimized then
      Application.MainForm.WindowState := wsNormal;
      // set the focus to the application
      SetForegroundWindow(Application.MainForm.Handle);
      // load the text!
      GetMem(s,100);
      try
        GlobalGetAtomName(Atom(lParam),s,100);
        GlobalDeleteAtom(Atom(lParam));
        TFrameForm(Application.MainForm).Open(s);
      finally
        FreeMem(s,100);
      end;
  end else
    // Otherwise, pass message on to old window proc
    Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam);
end;

procedure SubClassApplication;
begin
  {we subclass application window so that
  application.onmessage remains available for user}
  WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC,
                        LongInt(@NewWndProc)));
  {set appropriate error flag}
  if WProc = nil then
    MIError := MIError or MI_FAIL_SUBCLASS;
end;

procedure DoFirstInstance;
begin
  SubClassApplication;
  MutHandle := CreateMutex(nil, False, UniqueAppStr);
  if MutHandle = 0 then
    MIError := MIError or MI_FAIL_CREATE_MUTEX;
end;

var
  s: PChar;

procedure BroadCastFocusMessage;
{ this is called when there is already an instance running}
var
  BSMRecipients : DWORD;
  AAtom : Atom;
begin
  { don't flash main form }
  Application.ShowMainForm := false;
  { post message and inform other instace to focus itself }
  BSMRecipients := BSM_APPLICATIONS;
  s:=PChar(ParamStr(1));
  AAtom:=GlobalAddAtom(s);
  BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
    @BSMRecipients, MessageID, 0, LongInt(AAtom));
  Application.Terminate;
end;

procedure InitInstance;
begin
  MutHandle :=OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr);
  if MutHandle = 0 then
    { mutex object has not yet been created, meaning that no previous
      instance has been created }
    DoFirstInstance
  else
    BroadCastFocusMessage;
end;

initialization
  MessageID := RegisterWindowMessage(UniqueAppStr);
  InitInstance;
finalization
  if WProc <> nil then
    // restore the old window procedure
    SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc));
end.

*** end of code ***

P.S. A far better apraoch would be using DDE in stead of globalatoms. But I have litle experience with that.

B.T.W. Your link in the explorer should be something like:

".../textedit.exe" "%1"

This because the of long file that include spaces.

Let me know if it works! I had fun trying this out. It works!

Regards Jacco
0
 
LVL 10

Expert Comment

by:Jacco
ID: 1338993
It might be a good idea to add a small 4 character prefix to the name of the Atom. This to prevent doubles.
0
 

Author Comment

by:rnxotj
ID: 1338994
Hi! Jacco.

When UPrevMultiple.pas used?
On create event or on show event at MDI parent Form ?

Would you explain relationship between MDI parent and UPrevMultiple.pas ?

0
 
LVL 10

Expert Comment

by:Jacco
ID: 1338995
Hi rnxotj,

The unit does everything itself.

The initialisation/finalization part of the unit is responsible for it.

The initialisation part is executed when the application starts

It checks if there is a previous instance running. If not it registers a special message. If so it broadcasts this special message to the previous instance. If they is is add a global atom and broadcast a message that the atom is there.

The finalisation part is executed when the application ends

This unregisters the special message.

Try it!

Regards Jacco
0
 

Author Comment

by:rnxotj
ID: 1338996
Thank you,Jacco!
It woks very well.
Have a nice day!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

862 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now