Solved

MDI,create child window when command line execution.

Posted on 1998-09-08
5
229 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
[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
  • 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 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