Solved

MDI,create child window when command line execution.

Posted on 1998-09-08
5
224 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

16 Experts available now in Live!

Get 1:1 Help Now