[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

control position of other windows

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
jsm11482
Asked:
jsm11482
1 Solution
 
zzzzzoocCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now