Solved

control position of other windows

Posted on 2003-11-02
4
154 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we 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…
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…

708 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

14 Experts available now in Live!

Get 1:1 Help Now