Solved

control position of other windows

Posted on 2003-11-02
4
159 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
4 Comments
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 125 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

776 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