?
Solved

control position of other windows

Posted on 2003-11-02
4
Medium Priority
?
165 Views
Last Modified: 2010-05-01
Hi, how can i control the position of windows and dialogs that open on a certain portion of the screen? For instance, if a window opens onthe right hand side of the screen, I want to move it to the left hand side.  This needs to work with all windows, not windows that belong to this application, but windows from any application.
-josh
0
Comment
Question by:jsm11482
[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 Comments
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 500 total points
ID: 9670297
Well this is a rough example. You'll need to adjust the Max Left/Top and the new Left/Top (based on Bottom/Right for width/height respectively) but you may get the general idea:


Form1:

Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowRect Lib "User32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)

Private Const GW_HWNDNEXT = 2

Private Const SWP_NOACTIVATE = &H10
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4

Dim iMaxLeft As Integer, iMaxTop As Integer
Private Sub Form_Load()
    iMaxLeft = CInt((Screen.Width / Screen.TwipsPerPixelX) / 2)
    iMaxTop = CInt((Screen.Height / Screen.TwipsPerPixelY) / 2)
    Timer1.Interval = 1000
    Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
    Dim lHandle As Long, lpRect As RECT
    lHandle = FindWindow(vbNullString, vbNullString)
    Do Until lHandle = 0
        If GetText(lHandle) <> "" Then
            Call GetWindowRect(lHandle, lpRect)
            'Tested on 1024*768 with these positions
            With lpRect
                If .Left > iMaxLeft Then .Left = iMaxLeft
                If .Top > iMaxTop Then .Top = iMaxTop
                Call SetWindowPos(lHandle, 0, .Left, .Top, 0, 0, SWP_NOSIZE Or SWP_NOACTIVATE Or SWP_NOZORDER)
            End With
            bSet = False
        End If
        lHandle = GetWindow(lHandle, GW_HWNDNEXT)
        DoEvents
    Loop
End Sub
Private Function GetText(ByVal lHandle As Long) As String
    Dim lTemp As String
    GetText = String(256, Chr(0))
    Call GetWindowText(lHandle, GetText, Len(GetText))
    GetText = Left(GetText, InStr(GetText, Chr(0)) - 1)
End Function
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 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