Branko
Branko

Reputation: 1458

How to create form modal to the console window

Inside console app - GetOpenFileName() with Handle := FindWindow(Nil, Pchar(ConsoleTitle)) show OpenFile dialog modal to console window. Is it possible to create and show my own form modal to console window?

Upvotes: 1

Views: 2350

Answers (1)

Sertac Akyuz
Sertac Akyuz

Reputation: 54772

Set the console window the window owner of your form and disable it when showing your form. Something like the following:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  windows,
  forms,
  Unit1 in 'Unit1.pas' {Form1};

const
  ConsoleClass = 'ConsoleWindowClass';

var
  console: HWND;
  len: DWORD;
  title: array [0 .. MAX_PATH] of Char;

begin
  try
    len := GetConsoleTitle(title, SizeOf(title));
    Win32Check(Bool(len));
    console := FindWindow(ConsoleClass, title);
    Win32Check(Bool(console));
    Form1 := TForm1.Create(nil);
    try
      EnableWindow(console, False);
      try
        Form1.HandleNeeded;
        SetWindowLongPtr(Form1.Handle, GWLP_HWNDPARENT, console);
        Form1.ShowModal;
      finally
        EnableWindow(console, True);
      end;
    finally
      Form1.Free;
    end;
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
end.

Upvotes: 4

Related Questions