How to create another window?

I have this code:

program testGUI;

uses
  System, Controls, GraphWPF;

begin
  Window.Title := 'GUI Mode with WPF';
  
  var edit1 := TextBoxWPF.Create(40,30,'Type your name:',100);
  var button1 := ButtonWPF.Create(40,80,'Click me!',100,12);
  edit1.FontSize := 13;
  button1.Height := 30;
  
  // I thought it would be like this, but this does not work obviously
  button1.Click := () -> begin
    // show another window
    var form1 := Form.Create;
    var button2 := ButtonWPF.Create(40,80,'Click me!',100,12);
    form1.AppendControl(button2);
    // or draw using GraphABC
    form1.Pen.Color := clNavy;
    form1.DrawCircle(100,100,40);
    form1.Show;
  end;
end.

Now, if I click on button1, I want to create and show another window (form) that has its own controls or draw upon itu using GraphABC methods. The new form definition could be in the same program file or in another file (unit).

Anybody have a hint how to do it? Is it possible?

Thank you.

Since nobody answered this, I could assume that it’s impossible to have multiple windows in pascalABC using procedural method.

Well i wanted for devs to give answer because i’m not entirely sure, but i think units like GraphWPF were only made for single-window programs.

You can create multiple windows by using WPF directly. Though you would probably also need to create another AppDomain for each window (except the first one).

Here is a bit of sample shitcode (you really shouldn’t use it in production like this):

uses System.Windows;
uses System.Windows.Controls;

{$reference PresentationFramework.dll}
{$reference PresentationCore.dll}
{$reference WindowsBase.dll}

{$apptype windows}

begin
  var w := new Window;
  
  var b := new Button;
  w.Content := b;
  b.Content := 'Click me';
  b.Click += (o,e)->
  begin
    var thr := new System.Threading.Thread(()->System.AppDomain.CreateDomain('Domain for second window').DoCallBack(()->
    begin
      var w2 := new Window;
      
      Application.Create.Run(w2);
    end));
    thr.ApartmentState := System.Threading.ApartmentState.STA;
    thr.Start;
  end;
  
  Application.Create.Run(w);
end.

In GraphWPF it is impossible.

How about multiple windows using GraphABC? Is it impossible too?

Thank you. I’ll try it later. I’m not on Windows right now. Let’s see whether your code would work or not.

In WinForms it’s a bit easier (though there are many more problems that don’t exist in WPF), but GraphABC is not supported.

Is there a way to embed GraphABC into a WinForm? Say a panel that can be used just like a GraphABC window. So we could do something like this:

with PanelABC1 do
begin
  Pen.Color := clNavy;
  DrawCircle(100,100,20);
end;

which will draw a circle inside the panel.

Is it possible?

Well, it works. But each window act as two different applications that run independently, not as part of a single application. The boiler code to do it is also too much for newbies. I think it’s not a suitable solution for newbies.

Thank you anyway.

And how do you imagine it working?

Like other multi window application which the main form acts as the main app. If you close it, all the other subforms are closed too. But if we close a subform, the main form still exists.

This behavior is usually added manually. Event handler for close button saves everything and shut’s down whole application, so other windows would die too.

No, I’ve tested it with Delphi. Only the main form acts as the application. If I close the main form, all the subforms are closed as well. It’s the other way around. If you close the main form but want to keep the application running, you have to give the main form handle to the subform which you must do it manually.

 Институт математики, механики и компьютерных наук ЮФУ, 2005–2018
Администрация форума: В.Н. Брагилевский, С.С. Михалкович, А.М. Пеленицын
Yandex.Metrica