unit U_openexe;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls,ShellAPI,jpeg, cxControls, cxContainer,
  cxEdit, cxGroupBox;

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

  Tfrmopenexe = class(TForm)
    Button1: TButton;
    Splitter1: TSplitter;
    Panel1: TPanel;
    Button2: TButton;
    Button3: TButton;
    ODPat: TOpenDialog;
    ScrollBox1: TScrollBox;
    Image1: TImage;
    Button4: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Image1DblClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmopenexe: Tfrmopenexe;
  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);
  ShowMessage(IntToStr(hWin));
  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 Tfrmopenexe.FormDestroy(Sender: TObject);
begin
  frmopenexe:=nil;
end;



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

procedure Tfrmopenexe.Button2Click(Sender: TObject);
var  t : TResourceStream;
 
 begin
 
   if FileExists('float.exe') then
 
   WinExec('float.exe',Sw_normal)
 
 else
 
   begin
 
     try
 
       t := TResourceStream.Create(HInstance,'AEXE','EXEFILE'); //其中HInstance为一个句柄常量；rwww为资源名；exefile为资源类型
 
      t.SaveToFile('float.exe');
 
     finally
 
       t.free;
 
     end;
 
     WinExec('float.exe',Sw_normal);
 
   end;
 
 end;

procedure Tfrmopenexe.Button3Click(Sender: TObject);
var
  AJpeg:TJPEGImage;
  TPWidth,TPHeight:Integer;
  Bitmap: TBitmap;
  Ratio: Double;
  ARect: TRect;
  AHeight, AHeightOffset: Integer;
  AWidth, AWidthOffset: Integer;
  FReal:Double;
begin

  if ODPat.Execute=False  then Exit;
  AJpeg:=TJpegImage.Create();
  AJpeg.LoadFromFile(ExtractFileName(ODPat.FileName));
  TPWidth:=AJpeg.Width;
  TPHeight:=AJpeg.Height;
  Bitmap := TBitmap.Create;
  Bitmap.Width :=Image1.Width;
  Bitmap.Height :=Round(Image1.Width/TPWidth*TPHeight);
  if Bitmap.Height>400 then
  begin
    Bitmap.Height:=400;
    Bitmap.Width :=Round(400/TPHeight*TPWidth);
  end;
  Bitmap.Canvas.FillRect(Rect(0, 0, Bitmap.Width, Bitmap.Height));
  ARect := Rect(0, 0, Bitmap.Width, Bitmap.Height);
  Bitmap.Canvas.StretchDraw(ARect, AJPeg);
  Image1.Height:=Bitmap.Height;
  Image1.Width:=Bitmap.Width;
  Image1.Picture.Assign(BitMap);
end;


procedure Tfrmopenexe.Image1DblClick(Sender: TObject);
var
  AJpeg:TJPEGImage;
  TPWidth,TPHeight:Integer;
  Bitmap: TBitmap;
  Ratio: Double;
  ARect: TRect;
  AHeight, AHeightOffset: Integer;
  AWidth, AWidthOffset: Integer;
  FReal:Double;
begin
  AJpeg:=TJpegImage.Create();
  AJpeg.Assign(Image1.Picture.Graphic);
  Bitmap := TBitmap.Create;
  Bitmap.Width :=Round(Image1.Width*1.5);
  Bitmap.Height :=Round(Image1.Height*1.5);
  Bitmap.Canvas.FillRect(Rect(0, 0, Bitmap.Width, Bitmap.Height));
  ARect := Rect(0, 0, Bitmap.Width, Bitmap.Height);
  Bitmap.Canvas.StretchDraw(ARect, AJPeg);
  Image1.Height:=Bitmap.Height;
  Image1.Width:=Bitmap.Width;
  Image1.Picture.Assign(BitMap);
end;

procedure Tfrmopenexe.Button4Click(Sender: TObject);

var
  AJpeg:TJPEGImage;
  TPWidth,TPHeight:Integer;
  Bitmap: TBitmap;
  Ratio: Double;
  ARect: TRect;
  AHeight, AHeightOffset: Integer;
  AWidth, AWidthOffset: Integer;
  FReal:Double;
begin
  AJpeg:=TJpegImage.Create();
  AJpeg.Assign(Image1.Picture.Graphic);
  Bitmap := TBitmap.Create;
  Bitmap.Width :=Round(Image1.Width/1.5);
  Bitmap.Height :=Round(Image1.Height/1.5);
  Bitmap.Canvas.FillRect(Rect(0, 0, Bitmap.Width, Bitmap.Height));
  ARect := Rect(0, 0, Bitmap.Width, Bitmap.Height);
  Bitmap.Canvas.StretchDraw(ARect, AJPeg);
  Image1.Height:=Bitmap.Height;
  Image1.Width:=Bitmap.Width;
  Image1.Picture.Assign(BitMap);
end;


end.
