Solved

How to tell if a window is within screen boundaries?

Posted on 2002-03-22
5
232 Views
Last Modified: 2010-05-02
Scenerio:
My app is running.
If the user opens a window on a program and moves it anywhere outside the screen boundaries, my program detects it and puts up a msg box.
Basically, this app would know if the window that has focus is within or without the screen boundaries.
0
Comment
Question by:rbend
  • 4
5 Comments
 
LVL 3

Accepted Solution

by:
jbil earned 75 total points
ID: 6888677
Here's what Ive got so far..
form with text1 and timer1


Option Explicit
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Const SW_MINIMIZE = 6
Private Type POINTAPI
        x As Long
        y As Long
End Type
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Private Type WINDOWPLACEMENT
        Length As Long
        flags As Long
        showCmd As Long
        ptMinPosition As POINTAPI
        ptMaxPosition As POINTAPI
        rcNormalPosition As RECT
End Type

Dim myrect As RECT
Dim busywin As Long

Private Sub Form_Click()
Text1.Text = ""
End Sub


Private Sub Timer1_Timer()
Dim rside As Long
Dim bside As Long
Dim winpos As WINDOWPLACEMENT
Dim rtn As Long
 
   busywin = GetForegroundWindow
   winpos.Length = Len(winpos)
    rtn = GetWindowPlacement(busywin, winpos)
    myrect = winpos.rcNormalPosition
    rside = (myrect.Right) * Screen.TwipsPerPixelX
    bside = (myrect.Bottom) * Screen.TwipsPerPixelY
    If myrect.Top < 0 Or myrect.Left < 0 Or bside > Screen.Height Or rside > Screen.Width Then
        Text1.Text = "foul!"
        Beep
    End If

End Sub
0
 

Author Comment

by:rbend
ID: 6888845
jbil:
Just one more thing and I think this will work.
Can you add :
To close the window that "foul'd" ?
When it goes off the screen, close it?
More points when you do.
Thanks
0
 

Author Comment

by:rbend
ID: 6888893
jbil:
Never mind the second part.
I figured it out.
Thanks.
Perfect.
0
 

Author Comment

by:rbend
ID: 6888951
jbil:
I just noticed something...
If I click on the desktop (not on any icon or anything)
the "foul" message appears.
Can you make it so that "focus" on the desktop does NOT acitvate the program?
More Points.
0
 

Author Comment

by:rbend
ID: 6889032
jbil:
Ok...I took care of that too.
Thanks.
b
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question