Solved

control position of other windows

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 116
clicking a shape in a frame array vb6 3 48
Help me. 3 70
Macro problems with Excel file 6 27
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 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