Solved

How to tell if a window is within screen boundaries?

Posted on 2002-03-22
5
224 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
Comment Utility
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
Comment Utility
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
Comment Utility
jbil:
Never mind the second part.
I figured it out.
Thanks.
Perfect.
0
 

Author Comment

by:rbend
Comment Utility
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
Comment Utility
jbil:
Ok...I took care of that too.
Thanks.
b
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now