Solved

How to determine window size?

Posted on 2009-05-07
9
356 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

705 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