Solved

Open / bind / embed Notepad within your application??

Posted on 2003-11-16
6
1,419 Views
Last Modified: 2007-12-19
Ok heres what i want but am stuggling to explain it.

I have a form and button1

I want to be able to click button 1 and for it to open Notepad.exe from its usual location.

Simple....

Not exactly

i want to open notepad (the application and not some kind of .txt file) within a frame / panel within form 1 (like its embedded)

Query Binding object?????

So, embedd Notepad as an application in the form so the user can type etc... in notepad. I am not bothered that the user wont be able to see notepads toolbar.

Would you use an OleContainer for this or some how load it in a panel either way please provide the full source (its probbably better if you spell it out to me!!!)

Hope this makes sense.


Thanks in advance

Harry



0
Comment
Question by:harry_bela
[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
6 Comments
 
LVL 4

Expert Comment

by:nestorua
ID: 9759156
HI,
If you want to use OLEContainer then it is simple but not for notepad.exe. You could do it for wordpad.exe for example.
Place OLEContainer on your form, press right button and choose
'Insert Object...'. Then set the property Iconic to False.
That's all.
Sincerely,
Nestorua.
0
 

Author Comment

by:harry_bela
ID: 9759298
Hi,

Ok i have tried what you sugested and it works but i only really suggested Notepad because it was simple.... i really want to embed another Delphi project within my project. So i need the code for "any" type of .exe applcation.

Currently all i get is a file download box when the OLEContainer loads.

Thanks in advance.
0
 
LVL 3

Expert Comment

by:Mamouri
ID: 9759787
Hi Harry!

use this unit:
unit LecsExec;

interface

uses
 Windows;

 
FUNCTION WinExecAndGetHWnd(const fn: String; var hNewWnd : THandle) : Boolean;

implementation

VAR
 ProcessInformation : TProcessInformation;
 WndHandle  : hWnd;
 Identifier : LongInt;

function EnumFunc(H : HWnd; Ignored : LongInt) : Bool; StdCall;
VAR TheID : LongInt;
BEGIN
 {Find the window matching the process ID}
 GetWindowThreadProcessID(H, @TheID);
 IF TheID = Identifier THEN BEGIN
   WndHandle := H;
   Result    := False;
   END
 ELSE
   Result := True;
END;

FUNCTION WinExecAndGetHWnd(const fn: String; var hNewWnd : THandle) : Boolean;
VAR
 StartupInfo : TStartupInfo;
begin
 WndHandle := 0;

  FillChar(StartupInfo, SizeOf(StartupInfo), 0);
 StartupInfo.CB := SizeOf(StartupInfo);

  IF CreateProcess(
   NIL,                  
   PChar(fn),        
   NIL,
   NIL,
   False,
   DETACHED_PROCESS OR NORMAL_PRIORITY_CLASS,
   NIL,
   NIL,
   StartupInfo,
   ProcessInformation) THEN BEGIN

    Identifier := ProcessInformation.dwProcessID;
   CloseHandle(ProcessInformation.hThread);
   WaitForInputIdle(ProcessInformation.hProcess, 10000); {let process start!}
   EnumWindows(@EnumFunc, Identifier);

    END;

  if WndHandle = 0 then
   Result := False
 else begin
   Result := True;
   hNewWnd := WndHandle;
   end;

end;

end.





now include the above unit in your programme...



uses
  LecsExec;

procedure TForm1.Button1Click(Sender: TObject);
var
  WindowHandle: THandle;
begin
  if not WinExecAndGetHWnd(Calc.exe', WindowHandle) then
    raise Exception.Create('Unable to execute programme!');
  Windows.SetParent(WindowHandle, Panel1.Handle);
end;


This code didnt write by me.
I take it from:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20682510.html

let me know if you want to do more thing with this program!


Regards
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!

 
LVL 3

Expert Comment

by:Mamouri
ID: 9759793
Harray, Hi again!

If you want to embed an exe in your main executable see this question:

http://www-tcsn.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20751984.html

Regards
0
 
LVL 17

Accepted Solution

by:
geobul earned 300 total points
ID: 9762382
Hi,

The following is a complete project that does what you need:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls;

type

  PProcessWindow = ^TProcessWindow;
  TProcessWindow = record
    TargetProcessID: Cardinal;
    FoundWindow: hWnd;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    Panel1: TPanel;
    OpenD: TOpenDialog;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  hWin: HWND = 0;

implementation

{$R *.DFM}

function EnumWindowsProc(Wnd: hWnd; ProcWndInfo: PProcessWindow): BOOL; stdcall;
var
  WndProcessID: Cardinal;
begin
  GetWindowThreadProcessId(Wnd, @WndProcessID);
  if WndProcessID = ProcWndInfo^.TargetProcessID then begin
      ProcWndInfo^.FoundWindow := Wnd;
      Result := False; // This tells EnumWindows to stop enumerating since we've already found a window.
  end else Result := True; // Keep searching
end;

function GetProcessWindow(TargetProcessID: Cardinal): hWnd;
var
  ProcWndInfo: TProcessWindow;
begin
  ProcWndInfo.TargetProcessID := TargetProcessID;
  ProcWndInfo.FoundWindow := 0;
  EnumWindows(@EnumWindowsProc, Integer(@ProcWndInfo));
  Result := ProcWndInfo.FoundWindow;
end;

function RunAppInPanel(const AppName: string; PanelHandle: HWND): boolean;
var
  si: STARTUPINFO;
  pi: TProcessInformation;
begin
  FillChar(si, SizeOf(si), 0);
  si.cb := SizeOf(si);
  si.wShowWindow := SW_SHOW;
  result := CreateProcess(nil, PChar(AppName), nil,
      nil, true, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, si, pi);

  if not result then exit;

  WaitForInputIdle(pi.hProcess, 10000); // let process start!

  hWin:=GetProcessWindow(pi.dwProcessID);
  if hWin > 0 then begin
    Windows.SetParent(hWin, PanelHandle);
    SetWindowPos(hWin, 0, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOZORDER);
    result := true;
  end;

  // we don't need the handles so we close them
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenD.Execute then begin
    if hWin > 0 then PostMessage(hWin, WM_CLOSE, 0, 0); // close any app currently opened
    if not RunAppInPanel(OpenD.FileName, Panel1.Handle) then ShowMessage('App not found');
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if hWin > 0 then
    PostMessage(hWin, WM_CLOSE, 0, 0);
end;

end.

Regards, Geo
0
 

Author Comment

by:harry_bela
ID: 9766823
Ok,

Here what iv'e found....


All the code works and i can load notepad word etc... in my form. However

1) Is there any way to stop the icon of the app + name appearing in the taksbar (it appears just like it would if you have just opened any normal app from your desktop)
2) (And this is my big problem) the program i really want to open within my form is also another delphi app. But for some reason when you open it as you woul notepad etc... by clicking button 1. It opens but not in the form it opens just like another application in a sperate window infornt of form1 anyone know wy this happens?????

Any other demo delphi will open within panel 1 but what can be making my app open in this way.

Thnaks.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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