Solved

Open / bind / embed Notepad within your application??

Posted on 2003-11-16
6
1,403 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
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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