Solved

Open / bind / embed Notepad within your application??

Posted on 2003-11-16
6
1,382 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

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

18 Experts available now in Live!

Get 1:1 Help Now