Board index » delphi » Console App using Open File dialog.

Console App using Open File dialog.

OS: Windows 95/98
Delphi 3

I am in the process of "converting" 50 legacy Turbo PASCAL apps to Delphi.
Due to schedule demands, I am converting the non graphics programs to
Delphi Console Applications, and use the Windows Open File dialog to prompt
for file pathnames.

Everything works fine when MS-DOS is configured to use a Window (instead of
full screen).  The app starts, the file dialog pops up over the app (not
"in" the app window, but as another process) and the program responds fine.

Some of my users have their MS-DOS configured to use Full Screen.  The app
starts, the file dialog causes the app to minimize the console app, and when
the dialog is finished, (here's the problem) the console app remains
minimized.  This leads to problem reports that the app "aborted".  Not good.

I've browsed through the help, and have not found a solution.  I've tried
calling Application.Restore (restores app after minimize) after the dialog
box returns but it does not restore the console app.  I wouldn't mind if
Windows would undo what it did.

Does anyone have a solution (applicable to Delphi 3) ?

Thanks in advance,
John.

 

Re:Console App using Open File dialog.


Quote
"John Bowes" <jbowe...@comcast.nospam.net> wrote in message

news:4Q%n8.94161$2q2.8207289@bin4.nnrp.aus1.giganews.com...

Quote
> OS: Windows 95/98
> Delphi 3

> I am in the process of "converting" 50 legacy Turbo PASCAL apps to Delphi.
> Due to schedule demands, I am converting the non graphics programs to
> Delphi Console Applications, and use the Windows Open File dialog to
prompt
> for file pathnames.

> Everything works fine when MS-DOS is configured to use a Window (instead
of
> full screen).  The app starts, the file dialog pops up over the app (not
> "in" the app window, but as another process) and the program responds
fine.

> Some of my users have their MS-DOS configured to use Full Screen.  The app
> starts, the file dialog causes the app to minimize the console app, and
when
> the dialog is finished, (here's the problem) the console app remains
> minimized.  This leads to problem reports that the app "aborted".  Not
good.

> I've browsed through the help, and have not found a solution.  I've tried
> calling Application.Restore (restores app after minimize) after the dialog
> box returns but it does not restore the console app.  I wouldn't mind if
> Windows would undo what it did.

> Does anyone have a solution (applicable to Delphi 3) ?

> Thanks in advance,
> John.

_A_ solution
I think I once saw a better way, I cannot find it or maybe
was another OS.

operation co-ords etc not checked on all WOS versions

a fullscreen console has a remote window , off screen
on dialog return the taskbar tab should be focused
why bother to check?
...if you enter a return in windowed mode AND a manager like
nortons is running it will interpret the return and relaunch the app

function recttostr(r:trect):string;
begin
 result:='('+inttostr(r.left)
 +','+inttostr(r.top)
  +','+inttostr(r.right)
   +','+inttostr(r.bottom)+')';
end;

var d: TOpenDialog;
var res:boolean;
var w:thandle;
var r1:trect;
var fullscreen:boolean;

  w:=getforegroundwindow;
  getwindowrect(w,r1);
  writeln(recttostr(r1));
  fullscreen:= (r1.Left = 3000) and  (r1.top = 3000);
  d:=topendialog.create(nil);
  res:= d.Execute;
  if getforegroundwindow <> w then  setforegroundwindow(w);
  if fullscreen then
  begin
    keybd_event(VK_RETURN,MapVirtualKey(VK_RETURN,0),0,0);
    keybd_event(VK_RETURN,MapVirtualKey(VK_RETURN,0),KEYEVENTF_KEYUP,0);
  end;
 if res then
 ...
 d.free;

Re:Console App using Open File dialog.


Thanks,

This seems to work just fine.

Just to make sure I understand what's going on, the key is to save the
console window handle and it's Window/Full Screen state before using
the Open File dialog and afterwards see if the foreground window is
different from before.  Then if it was full screen, fake an
Alt-Enter to toggle between Window/Full Screen.

Is that the general idea ?

John.

p.s. Here's my test program to illustrate:

{$APPTYPE CONSOLE}
program Test;
uses
  Windows, Dialogs;
var
  OpenDialog: tOpenDialog;
  WasFullScreen: boolean;
  OriginalWindow: tHandle;

  function FullScreen: boolean;
  { suggested by Terry Russell }
  var r1:trect;
  begin
    getwindowrect(OriginalWindow,r1);
    result := (r1.Left = 3000) and  (r1.top = 3000);
  end;

  procedure SimulateAltEnter;
  begin
    { suggested by Terry Russell }
    keybd_event( VK_RETURN, MapVirtualKey( VK_RETURN, 0 ), 0, 0 );
    keybd_event( VK_RETURN, MapVirtualKey( VK_RETURN, 0 ), KEYEVENTF_KEYUP,
0 );
  end;

begin { Test }
  OriginalWindow := getforegroundwindow;
  WasFullScreen := FullScreen;
  writeln('About to open windows OpenFile dialog');

  OpenDialog := tOpenDialog.Create(nil);
  with OpenDialog do begin
    Title := 'Test File Open';
    if Execute then begin
      { use the file }
    end;
  end;
  OpenDialog.Free;

  if getforegroundwindow <> OriginalWindow then
    setforegroundwindow(OriginalWindow);
  if WasFullScreen then
    SimulateAltEnter;

  write('Do you see this ? (hit Enter)');
  readln;
end.  { Test }

Quote
----- Original Message -----
From: Terry Russell <trochi...@bigpond.com>

Newsgroups: comp.lang.pascal.delphi.misc
Sent: Wednesday, March 27, 2002 12:27 AM
Subject: Re: Console App using Open File dialog.

> "John Bowes" <jbowe...@comcast.nospam.net> wrote in message
> news:4Q%n8.94161$2q2.8207289@bin4.nnrp.aus1.giganews.com...
> > OS: Windows 95/98
> > Delphi 3

> > Some of my users have their MS-DOS configured to use Full Screen.  The
app
> > starts, the file dialog causes the app to minimize the console app, and
> when
> > the dialog is finished, (here's the problem) the console app remains
> > minimized.  This leads to problem reports that the app "aborted".  Not
> good.

> _A_ solution
> I think I once saw a better way, I cannot find it or maybe
> was another OS.

> operation co-ords etc not checked on all WOS versions

> a fullscreen console has a remote window , off screen
> on dialog return the taskbar tab should be focused
> why bother to check?
> ...if you enter a return in windowed mode AND a manager like
> nortons is running it will interpret the return and relaunch the app

> function recttostr(r:trect):string;
> begin
>  result:='('+inttostr(r.left)
>  +','+inttostr(r.top)
>   +','+inttostr(r.right)
>    +','+inttostr(r.bottom)+')';
> end;

> var d: TOpenDialog;
> var res:boolean;
> var w:thandle;
> var r1:trect;
> var fullscreen:boolean;

>   w:=getforegroundwindow;
>   getwindowrect(w,r1);
>   writeln(recttostr(r1));
>   fullscreen:= (r1.Left = 3000) and  (r1.top = 3000);
>   d:=topendialog.create(nil);
>   res:= d.Execute;
>   if getforegroundwindow <> w then  setforegroundwindow(w);
>   if fullscreen then
>   begin
>     keybd_event(VK_RETURN,MapVirtualKey(VK_RETURN,0),0,0);
>     keybd_event(VK_RETURN,MapVirtualKey(VK_RETURN,0),KEYEVENTF_KEYUP,0);
>   end;
>  if res then
>  ...
>  d.free;

Re:Console App using Open File dialog.


Quote
"John Bowes" <jbowe...@comcast.nospam.net> wrote in message

news:cBSo8.109703$7b.10125654@bin7.nnrp.aus1.giganews.com...

Quote
> Thanks,

> This seems to work just fine.

> Just to make sure I understand what's going on, the key is to save the
> console window handle and it's Window/Full Screen state before using
> the Open File dialog and afterwards see if the foreground window is
> different from before.  Then if it was full screen, fake an
> Alt-Enter to toggle between Window/Full Screen.

That  was the idea, nothing wrong with a bit of manual control.

I used this when in a hurry unable to find which API calls would work plus
winsight kept crashing so I found a quick and dirty  way ;-(

Looking at the code again I remembered there was a problem in that
normally it worked but if you moved or sized the dialog or closed without a
selection
it sometimes didn't restore to fullscreen correctly
despite indicating that the status and window were correct

Now, playing with it a little more showed pounding the window with restores
worked
so assuming it was a timing thing tried this , which seems  to work
without the arm waving
maybe a wait for something would be better but this is simpler ;-)

  w:=getforegroundwindow;
  wasfullscreen:=isiconic(w);
  d:=topendialog.create(nil);
  res:= d.Execute;

  if wasfullscreen then
  begin
    setforegroundwindow(w);
    //sleep(50);
    sleep(250);  // just being conservative didn't see errors with 50
    showwindow(w,SW_RESTORE);
    //showwindow(w,SW_RESTORE); // two usually worked three always
    //showwindow(w,SW_RESTORE);
  end;

 if res then
  begin
   writeln(d.filename);
   exitcode:=0;
  end
  else
  begin
   writeln('dialog aborted');
   exitcode:=1;
  end;
  d.free;

unfortunately you still have to test for fullscreen
sending a restore to a windowed dosbox makes it toggle the zoom

( NOW I fancy I saw something with isiconic not always working, but that
could be faulty memory..or dreaming about the problem after collapsing into
bed..
I didn't see any just now, just a  note if you see problems)

a windowed box  is
 not iconic (-6,-6,x,y)
 zoomed  ( zoomed means normal size)
a GUIwindowed box  is
 not iconic  (66,66,x,y);
 not zoomed
a fullscreen DOSbox  is
 iconic  (3000,3000,x,y)
 not zoomed

@echo off
CONSOL~1 EXE
if errorlevel 1 goto aborted
echo okay
goto endit
:aborted
echo  aborted
:endit

Other Threads