Results 1 to 3 of 3

Thread: Run een willekeurig programma in een Panel

  1. #1

    Run een willekeurig programma in een Panel

    Hallo allemaal,

    Onderstaande code heeft als resultaat dat NotePad.exe wordt weergegeven op Panel1. Dit werkt dus correct. Vervangen we nu notepad.exe door een willekeurig andere toepassing dan wordt deze toepassing niet in Panel1 geplaatst maar vrij op het scherm. Het is gewenst om ook dit programma op Panel1 weer te geven. Hoe is dit op te lossen?
    Een tweede probleem is dat bij het sluiten van NotePad.Exe het programma niet meer reageert.

    Alvast bedankt, Frans

    Code:
    procedure TForm1.RunExeOpPanel(sExeName, sCaption: string; hParent: THandle);
    var
      wHandle: THandle;
    begin
      wHandle := 0;
      ShellExecute(Application.Handle, 'Open', PChar(sExeName), nil, nil, SW_NORMAL);
      while wHandle = 0 do
        wHandle := FindWindow(PChar(sCaption), nil);
      Windows.SetParent(wHandle, hParent);
      Windows.ShowWindow(wHandle, SW_SHOWMAXIMIZED);
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      RunExeOpPanel('Notepad.exe', 'Notepad', panel1.Handle);
    end;

  2. #2
    Hier een voorbeeld hoe je dergelijk kan doen, op basis van de naam van de executable en niet op window caption:

    Unit.pas
    Delphi Code:
    1. unit Unit30;
    2.  
    3. interface
    4.  
    5. uses
    6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
    8.  
    9. type
    10.   TForm30 = class(TForm)
    11.     Panel1: TPanel;
    12.     Bevel1: TBevel;
    13.     Button1: TButton;
    14.     procedure Button1Click(Sender: TObject);
    15.     procedure Panel1Resize(Sender: TObject);
    16.   private
    17.     { Private declarations }
    18.     NotepadHandle : THandle;
    19.   public
    20.     { Public declarations }
    21.   end;
    22.  
    23. var
    24.   Form30: TForm30;
    25.  
    26. implementation
    27.  
    28. {$R *.dfm}
    29.  
    30. uses TLHelp32, ShellAPI;
    31.  
    32. function GetPIDbyProcessName(processName:String):integer;
    33. var
    34.   GotProcess: Boolean;
    35.   tempHandle: tHandle;
    36.   procE: tProcessEntry32;
    37. begin
    38.   tempHandle:=CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
    39.   procE.dwSize:=SizeOf(procE);
    40.   GotProcess:=Process32First(tempHandle, procE);
    41.   {$B-}
    42.     if GotProcess and (procE.szExeFile <> processName) then
    43.       repeat GotProcess := Process32Next(tempHandle, procE);
    44.       until (not GotProcess) or (procE.szExeFile = processName);
    45.   {$B+}
    46.  
    47.   if GotProcess then
    48.     result := procE.th32ProcessID
    49.   else
    50.     result := 0; // process not found in running process list
    51.  
    52.   CloseHandle(tempHandle);
    53. end;
    54.  
    55. function GetHWndByPID(const hPID: THandle): THandle;
    56. type
    57.   PEnumInfo = ^TEnumInfo;
    58.   TEnumInfo = record
    59.   ProcessID: DWORD;
    60.   HWND: THandle;
    61.   end;
    62.  
    63.   function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
    64.   var
    65.     PID: DWORD;
    66.   begin
    67.     GetWindowThreadProcessID(Wnd, @PID);
    68.     Result := (PID <> EI.ProcessID) or
    69.     (not IsWindowVisible(WND)) or
    70.     (not IsWindowEnabled(WND));
    71.  
    72.     if not Result then EI.HWND := WND;
    73.   end;
    74.  
    75.   function FindMainWindow(PID: DWORD): DWORD;
    76.   var
    77.     EI: TEnumInfo;
    78.   begin
    79.     EI.ProcessID := PID;
    80.     EI.HWND := 0;
    81.     EnumWindows(@EnumWindowsProc, Integer(@EI));
    82.     Result := EI.HWND;
    83.   end;
    84.  
    85. begin
    86.   if hPID <> 0 then
    87.     Result := FindMainWindow(hPID)
    88.   else
    89.     Result :=0;
    90. end;
    91.  
    92. procedure TForm30.Button1Click(Sender: TObject);
    93. var
    94.   appPID : THandle;
    95. begin
    96.   appPID    := GetPIDbyProcessName('notepad.exe');
    97.   NotepadHandle := GetHWndByPID(appPID);
    98.   if (NotepadHandle <> 0) then
    99.   begin
    100.     Winapi.Windows.SetParent(NotepadHandle, Panel1.Handle);
    101.     ShowWindow(NotepadHandle, SW_SHOWMAXIMIZED);
    102.   end;
    103. end;
    104.  
    105. procedure TForm30.Panel1Resize(Sender: TObject);
    106. begin
    107.   if (NotepadHandle <> 0) then
    108.   MoveWindow(NotepadHandle, 0, 0, (Sender as TPanel).ClientWidth, (Sender as TPanel).ClientHeight, True);
    109. end;
    110.  
    111. end.

    Form DFM
    Delphi Code:
    1. object Form30: TForm30
    2.   Left = 0
    3.   Top = 0
    4.   Caption = 'Form30'
    5.   ClientHeight = 524
    6.   ClientWidth = 765
    7.   Color = clBtnFace
    8.   Font.Charset = DEFAULT_CHARSET
    9.   Font.Color = clWindowText
    10.   Font.Height = -11
    11.   Font.Name = 'Tahoma'
    12.   Font.Style = []
    13.   OldCreateOrder = False
    14.   DesignSize = (
    15.     765
    16.     524)
    17.   PixelsPerInch = 96
    18.   TextHeight = 13
    19.   object Bevel1: TBevel
    20.     AlignWithMargins = True
    21.     Left = 603
    22.     Top = 8
    23.     Width = 8
    24.     Height = 508
    25.     Margins.Left = 0
    26.     Margins.Top = 8
    27.     Margins.Right = 0
    28.     Margins.Bottom = 8
    29.     Align = alLeft
    30.     Shape = bsLeftLine
    31.     ExplicitLeft = 609
    32.   end
    33.   object Panel1: TPanel
    34.     AlignWithMargins = True
    35.     Left = 8
    36.     Top = 8
    37.     Width = 587
    38.     Height = 508
    39.     Margins.Left = 8
    40.     Margins.Top = 8
    41.     Margins.Right = 8
    42.     Margins.Bottom = 8
    43.     Align = alLeft
    44.     Anchors = [akLeft, akTop, akRight, akBottom]
    45.     BevelOuter = bvLowered
    46.     Caption = 'Panel1'
    47.     ShowCaption = False
    48.     TabOrder = 0
    49.     OnResize = Panel1Resize
    50.     ExplicitWidth = 593
    51.   end
    52.   object Button1: TButton
    53.     Left = 614
    54.     Top = 8
    55.     Width = 143
    56.     Height = 25
    57.     Anchors = [akTop, akRight]
    58.     Caption = 'Open op Panel'
    59.     TabOrder = 1
    60.     OnClick = Button1Click
    61.     ExplicitLeft = 620
    62.   end
    63. end

    Ik heb ook de code voor de resize erin gezet, zodat als je de panel vergroot/verkleind dat het venster in de panel ook wordt geresized. Je moet enkel zelf zien dat de applicatie al gestart is; of deze starten.

  3. #3
    Ik maak gebruik van Lazarus versie 2.0.12.Hoe wordt het dan?

    H.G.Frans

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •