?
Solved

How to determine window size?

Posted on 2009-05-07
9
Medium Priority
?
369 Views
Last Modified: 2013-11-26
Is there a free tool that can determine the size of any open window in MS Windows?  ...and  size it to particular resolution?
0
Comment
Question by:brettr
[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
9 Comments
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24332138
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 24332166
0
 

Author Comment

by:brettr
ID: 24332353
Guessing I didn't pose the question correctly.  I don't want to determine size programmatically.  I want a third party tool to tell me the size of any open window - MS Work, VS.NET, Windows Explorer, FF, iTunes.

I'd like the tool to also resize any window to a specific resolution.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24332610
How about YOU build your OWN tool?...
Public Class Form1
 
    Private prevHandle As IntPtr
    Private prevRC As Rect
 
    Public Structure PointAPI
 
        Public X As Integer
        Public Y As Integer
 
        Public Sub New(ByVal x As Integer, ByVal y As Integer)
            Me.X = x
            Me.Y = y
        End Sub
 
    End Structure
 
    Public Structure Rect
 
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
 
        Public Function Width() As Integer
            Return Me.Right - Me.Left
        End Function
 
        Public Function Height() As Integer
            Return Me.Bottom - Me.Top
        End Function
 
    End Structure
 
    Public Const GA_ROOT As Integer = 2
    Public Const SWP_NOMOVE As Integer = 2
 
    Public Declare Function GetAncestor Lib "user32.dll" (ByVal handle As IntPtr, ByVal gaFlags As Integer) As IntPtr
    Public Declare Function WindowFromPoint Lib "user32" (ByVal pt As PointAPI) As IntPtr
    Public Declare Function GetWindowRect Lib "user32" (ByVal handle As IntPtr, ByRef lpRect As Rect) As Integer
    Public Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal handle As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal handle As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
    Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal handle As IntPtr) As Integer
    Public Declare Function IsWindow Lib "user32" Alias "IsWindow" (ByVal handle As IntPtr) As Boolean
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Window Size Tool"
 
        PictureBox1.BorderStyle = BorderStyle.FixedSingle
        PictureBox1.BackColor = Color.Red
        PictureBox1.Cursor = Cursors.Cross
 
        Dim rc As Rectangle = Screen.GetBounds(Me)
        NumericUpDown1.Maximum = rc.Width * 2
        NumericUpDown2.Maximum = rc.Height * 2
    End Sub
 
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Dim msg As String = "Drag the Red PictureBox around the screen..." & vbCrLf & vbCrLf & "...then change the size and click the button."
        MessageBox.Show(msg, "Instructions", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
 
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            prevHandle = IntPtr.Zero
        End If
    End Sub
 
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim pt As New PointAPI(Cursor.Position.X, Cursor.Position.Y)
            Dim handle As IntPtr = WindowFromPoint(pt)
            handle = GetAncestor(handle, GA_ROOT)
 
            Label1.Text = "Handle: " & handle.ToString("X")
            Dim windowText As New String(Microsoft.VisualBasic.Chr(0), GetWindowTextLength(handle) + 1)
            GetWindowText(handle, windowText, Len(windowText))
            Label2.Text = "Title: " & windowText
 
            If Not handle.Equals(prevHandle) AndAlso Not prevHandle.Equals(IntPtr.Zero) Then
                ' erase previous rectanlge
                ControlPaint.DrawReversibleFrame(New Rectangle(prevRC.Left, prevRC.Top, prevRC.Right - prevRC.Left, prevRC.Bottom - prevRC.Top), Color.Black, FrameStyle.Thick)
            End If
 
            If Not handle.Equals(prevHandle) Then
                ' get new rectangle
                GetWindowRect(handle, prevRC)
 
                ' draw new rectangle
                ControlPaint.DrawReversibleFrame(New Rectangle(prevRC.Left, prevRC.Top, prevRC.Right - prevRC.Left, prevRC.Bottom - prevRC.Top), Color.Black, FrameStyle.Thick)
 
                ' increase max's if necessary and display width/height of window
                If NumericUpDown1.Maximum < prevRC.Width Then
                    NumericUpDown1.Maximum = prevRC.Width * 2
                End If
                NumericUpDown1.Value = prevRC.Width
                If NumericUpDown2.Maximum < prevRC.Height Then
                    NumericUpDown2.Maximum = prevRC.Height * 2
                End If
                NumericUpDown2.Value = prevRC.Height
            End If
 
            ' store new handle
            prevHandle = handle
        End If
    End Sub
 
    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Left Then
            ControlPaint.DrawReversibleFrame(New Rectangle(prevRC.Left, prevRC.Top, prevRC.Right - prevRC.Left, prevRC.Bottom - prevRC.Top), Color.Black, FrameStyle.Thick)
        End If
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not prevHandle.Equals(IntPtr.Zero) AndAlso IsWindow(prevHandle) Then
            SetWindowPos(prevHandle, IntPtr.Zero, prevRC.Left, prevRC.Top, NumericUpDown1.Value, NumericUpDown2.Value, SWP_NOMOVE)
        End If
    End Sub
 
End Class

Open in new window

WindowSizeTool.jpg
0
 

Author Comment

by:brettr
ID: 24332757
>>How about YOU build your OWN tool?...<<

eh...thanks but I'm not sure how much clearer to get on the question.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24332941
Ok...I just gave you FULL, COMPLETE, WORKING code to do what you asked for...the picture above is of it actually working on my system.

You did post in the .Net section, are you not a programmer?  VB.Net 2008 is FREE.  Just build the app and you have a FREE tool that does what you want...AND you can customize it further to suit your needs.

If not, good luck finding your dream tool...  =)
0
 

Author Comment

by:brettr
ID: 24332989
Not a dream tool.  I've used such a tool and it did both things I'm asking for.  I just don't know the name.  It was only used in a programming environment to make sure windows met target resolutions.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24346664
Did you build it?...or just stop getting responses and decide to close the question?
0
 

Author Comment

by:brettr
ID: 24346739
The latter.  Obviously no one knows what the particular tool is and a question can't be left open since you will get a message that your account will be suspended.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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