Solved

How to determine window size?

Posted on 2009-05-07
9
343 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
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
 
LVL 85

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 85

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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

19 Experts available now in Live!

Get 1:1 Help Now