?
Solved

How to tell if a window is within screen boundaries?

Posted on 2002-03-22
5
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 3

Accepted Solution

by:
jbil earned 300 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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