Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Open / bind / embed Notepad within your application??

Posted on 2003-11-16
6
Medium Priority
?
1,464 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

926 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