Solved

How to tell if a window is within screen boundaries?

Posted on 2002-03-22
5
230 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

785 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