Have Dialog dissapear when minimizing application

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
delta1
Junior Coder
Posts: 26
Joined: Wed Dec 16, 2009 11:23 pm

Have Dialog dissapear when minimizing application

Post by delta1 » Wed Jun 02, 2010 10:15 pm

I have made a dialog for a specific application. I wish to have it dissapear when the application is minimized or closed and vice versa. Can someone point me in the right direction or supply a sample. Thanks.

User avatar
JRL
Automation Wizard
Posts: 3518
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 03, 2010 4:48 am

You can probably use the size of the application window to determine if it is maximized or minimized. This example uses Notepad as the application. If I was doing this for real I'd probably acquire the application window handle and use it rather than the name as I've done in the sample.

Hope this is helpful.

Code: Select all

Let>ShowFlag=0

Dialog>Dialog1
object Dialog1: TForm
  Left = 100
  Top = 10
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'Window is showing'
  ClientHeight = 200
  ClientWidth = 300
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
end
EndDialog>Dialog1

Label>Loop
  IfWindowOpen>Notepad*
    GetWindowSize>NotePad*,wide,tall
  EndIf
  If>Tall>50
      GoSub>ShowDialog
  EndIf
  If>ShowFlag>0
    Add>ShowFlag,1
    If>ShowFlag>10
      Let>ShowFlag=0
      CloseDialog>Dialog1
    EndIf
  EndIf
  Wait>0.01
Goto>Loop

SRT>ShowDialog
  If>ShowFlag=0
    Show>dialog1
  EndIf
  Let>ShowFlag=1
END>ShowDialog

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Thu Jun 03, 2010 1:19 pm

I don't have MS version 12 yet with the new dialog stuff, or the GetWindowSize> command so I created a similar script which works with my version 10.1.21


[code]

Dialog>Dialog1
Caption=
Width=445
Height=250
Top=222
Left=522
Label=Notepad window is showing,48,56,true
EndDialog>Dialog1

Let>WIN_USEHANDLE=1
Let>ShowFlag=0
Let>HideFlag=0

Label>Loop
GetWindowHandle>Notepad*,notepad_hwnd
Assigned>notepad_hwnd,notepad_hwnd_defined
If>notepad_hwnd_defined=TRUE
If>notepad_hwnd>0
GetWindowPos>notepad_hwnd,npX,npY
If>npY>0
Gosub>ShowDialog
Else
Gosub>HideDialog
Endif
Endif
Else
Gosub>HideDialog
Endif
Wait>1
Goto>Loop

SRT>ShowDialog
If>ShowFlag=0
Show>dialog1
Let>ShowFlag=1
Let>HideFlag=0
Endif
END>ShowDialog

SRT>HideDialog
If>HideFlag=0
CloseDialog>Dialog1
Let>HideFlag=1
Let>ShowFlag=0
Endif
END>HideDialog

[/code]

delta1
Junior Coder
Posts: 26
Joined: Wed Dec 16, 2009 11:23 pm

Post by delta1 » Thu Jun 03, 2010 1:58 pm

That's perfect. Thank you both.
Rob

delta1
Junior Coder
Posts: 26
Joined: Wed Dec 16, 2009 11:23 pm

Post by delta1 » Thu Jun 03, 2010 2:34 pm

One more question. Why is using the window handle more desirable than using the name?

User avatar
JRL
Automation Wizard
Posts: 3518
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 03, 2010 2:52 pm

A window handle is absolutely unique to the window. You could have several Notepad windows open all with identical names. Programmatically it could be difficult or impossible to work with the exact window that you desire to work with using a window name. Using the handle you are able to work with a precise match every time.

delta1
Junior Coder
Posts: 26
Joined: Wed Dec 16, 2009 11:23 pm

Post by delta1 » Thu Jun 03, 2010 3:23 pm

Thanks again.

User avatar
JRL
Automation Wizard
Posts: 3518
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 03, 2010 10:38 pm

Looking at adroege's solution; I think it is probably a more reliable concept to check for the position of a minimized window rather than checking the size which was the concept I proposed. I did a little experimenting this afternoon and discovered that on both my XP machine and a friend's Windows 7 machine, a minimized window's location is -32000,-32000. Armed with that information, I'd guess that it might be safe to assume that if a Window's position is -32000 on the x and -32000 on the y... it is minimized.

Also, I'm not sure about the reliability of adroege's test for a minimized window. Checking to be sure the y position is greater than 0 is possibly not good because its conceivable that a user could move a window to a negative y position on any normal computer. And a multi-monitor setup could have a monitor or two in a negative y position. That said I'm sure there is some reasonable number that could be used. Maybe If>npY>-10000... then show dialog.

Just a thought,
Dick

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

IsZoomed and IsIconic

Post by gdyvig » Fri Jun 04, 2010 2:06 am

Here is another approach to check whether a window is minimized:


http://www.mjtnet.com/usergroup/viewtop ... 8407#18407

If IsIconic returns TRUE the window is minimized. That would be when rZ=1 near the end of the code example.

Gale

User avatar
JRL
Automation Wizard
Posts: 3518
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Jun 04, 2010 3:29 am

Thanks Gale. I was sure there was a libfunc but I couldn't find it. Way better to have a function to tell you a window is minimized.

Oddly, I responded to the OP of the thread you found and I now have a better solution for his original question.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts