Solved

Open / bind / embed Notepad within your application??

Posted on 2003-11-16
6
1,438 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
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.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

631 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