How to run applications in Delphi, say, a calculator in the form of an application, I searched in nete, but there is only C, but I need Delphi. Does anyone know how to do?

    2 answers 2

    As I understand from the example, the main form of the running application becomes a child of the form of the launching program and is placed in its main window. It can be seen that when the program is started, its form appears as usual, and only then it shifts to the window of the program that launched it. Apparently, the standard functions of the Win32 or User32 libraries are used.

    I want to see myself

    What for? To view the source, just a notebook.

    Here is the source code in which everything happens:

    void PictureBox1Click(object sender, EventArgs e) //ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΊΠ»ΠΈΠΊΠ° ΠΏΠΎ Ρ„ΠΎΡ€ΠΌΠ΅ { System.Diagnostics.Process bob = new System.Diagnostics.Process(); bob.StartInfo.UseShellExecute = true; bob.StartInfo.Arguments += " /K TITLE Command Prompt"; bob.StartInfo.FileName = "CMD"; //имя ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ bob.Start(); //запускаСм ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡƒ this.timer1.Enabled = true; //запускаСм Ρ‚Π°ΠΉΠΌΠ΅Ρ€ } void Timer1Tick(object sender, EventArgs e) //ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° Ρ‚ΠΈΠΊΠ° Ρ‚Π°ΠΉΠΌΠ΅Ρ€Π° { System.IntPtr winParent; System.IntPtr x; System.IntPtr winHandle = Usr32.FindWin("ConsoleWindowClass", "Command Prompt"); //Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ ΠΎΠΊΠ½ΠΎ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ запустили if(winHandle != System.IntPtr.Zero) { winParent = Usr32.GetParent(winHandle); x = Usr32.SetParent(winHandle, this.pictureBox1.Handle); //присваиваСм Ρ„ΠΎΡ€ΠΌΠ΅ прилоТСния Π² качСствС родитСля Ρ„ΠΎΡ€ΠΌΡƒ нашСй ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ x = Usr32.SetWindowPos(winHandle, 1, 10, 10, 500, 300, 0); //устанавливаСм Π½ΠΎΠ²ΡƒΡŽ ΠΏΠΎΠ·ΠΈΡ†ΠΈΡŽ этой Ρ„ΠΎΡ€ΠΌΡ‹, которая ΡƒΠΆΠ΅ считаСтся Π΄ΠΎΡ‡Π΅Ρ€Π½Π΅ΠΉ this.timer1.Enabled = false; //Π²Ρ‹Ρ€ΡƒΠ±Π°Π΅ΠΌ Ρ‚Π°ΠΉΠΌΠ΅Ρ€ } 

    Now I will try to rewrite to Delphi ... So, it came out very simply and compactly:

     procedure TForm1.FormClick(Sender: TObject); //ΠΏΡ€ΠΈ ΠΊΠ»ΠΈΠΊΠ΅ Π½Π° Ρ„ΠΎΡ€ΠΌΠ΅ var h:hwnd; //ΡƒΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒ Π½Π° ΠΎΠΊΠ½ΠΎ begin //запускаСм ΡΡ‚Π°Π½Π΄Π°Ρ€Ρ‚Π½ΡƒΡŽ "ΠΊΠΎΠΌΠ°Π½Π΄Π½ΡƒΡŽ строку" ShellExecute(form1.Handle,'open','cmd.exe',nil,'c:\\windows\system32\',SW_SHOW); sleep(10); //ΠΆΠ΄Ρ‘ΠΌ, ΠΏΠΎΠΊΠ° ΠΎΠΊΠ½ΠΎ Π½Π΅ появится //Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ ΠΎΠΊΠ½ΠΎ ΠΏΠΎ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΡƒ h:=findwindow(nil, 'c:\\windows\system32\cmd.exe'); //Ссли нашли, присваиваСм Π΅ΠΌΡƒ статус Π΄ΠΎΡ‡Π΅Ρ€Π½Π΅Π³ΠΎ ΠΎΡ‚ нашСго окошка if h<>0 then windows.SetParent(h,form1.Handle); end; 

    For ShellExecute (shell32.dll), you need to connect the ShellAPI module, but the functions findwindow and setparent are declared in the windows module (win32.dll and user32.dll). Download my example. By the way, it turned out to be cool: C is written up to the fig code, and Delphi only contains a couple of lines, and the result is the same, maybe even better. : D

    • The application does not start in the form, but in its window (((( - evgeniy
    • So, too little pause. You can write in sleep, for example, 100 milliseconds, or even 1000. It can be seen that your cmd.exe program runs too slowly, and findwindow works ahead of time, therefore it does not detect the window ... Ok, you can try it as done in the example on C - start the timer, and it will already detect the window. Here, the fix: in the procedure for clicking on the form we insert the activation code for the timer, transfer the lines "h: = f ..." and "if h ..." to the timer handler, and add a shutdown to it if h <> 0 .. Here is the new link: rghost.ru/23666461 - TimKruz
    • For greater clarity, here is a slightly different example. Enter the path of the program and its title in the field - the application is launched and transferred to the window. Only instead of shellexecute is used winexec, by the way, it is described in the windows module, so there is no need to connect additional modules. Download: rghost.ru/23670601 - TimKruz
    • ... Only then it is necessary to kill open applications, otherwise their processes (without windows) will hang in the system all the time ... - TimKruz
    • Voooo what I wanted)) now it remains to figure out how it works .... And how now to change the properties of the form in the form)))? - evgeniy

    This phrase is completely incomprehensible:

    calculator in the application form

    And launching an external application from a Delphi program with ShellExecute ();

    • Here, for example, there is a program written on Delphi, there is a button, you press it and the application starts in the form of Delphi. - evgeniy
    • I have not met, and why? - SoftR
    • Yes, I met and it became interesting) - evgeniy
    • If it is not difficult to throw a link for example on C, I will see, perhaps it will be possible to fasten delphi, although I don’t see practical application of this subject yet. - SoftR
    • There is no link, but here’s one example on the site I’ve seen. I’m putting on the visa myself. I want to look at narod.yandex.ru/disk/26793186001/picBoxApp.7z β€” evgeniy