Getting the size of another applications window..

I am having two issues here. The first one involve the data returned by GetWindowRect, it is flat out wrong. I have my desktop at 1280x1024 and the window I tried to get the info from takes up the full size top to bottom and 3/4 of the width of the entire available space, but it returns Top = 0, Bottom = 0, Left = 3, Right = 0, of which only the Left coordinate is right according to information about the window retrieved by Spy++, or for that matter, just basic common sense. The other issue is that when I close the small test program, it causes an exception in Kernel32. :p The following is the code, sans all the form layout etc.:

EnumDeclares.bas>>

Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean

Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal ccs As Long) As Long

Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Type RECT
    Left As Integer
    Top As Integer
    Right As Integer
    Bottom As Integer
End Type

Public Myhandle As Long
Public WinName As String

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
  Dim sSave As String, Ret As Long
  Dim TestWin As String
  TestWin = "MUSHclient" 'Change to whichever window is being looked for.
  Ret = GetWindowTextLength(hwnd)
  sSave = Space(Ret)
  GetWindowText hwnd, sSave, Ret + 1
  TestEnumWin.RichTextBox1.Text = TestEnumWin.RichTextBox1.Text + vbCrLf + Hex(hwnd) + " " + sSave + " " + Str$(InStr(sSave, TestWin))
  If InStr(sSave, TestWin) Then 'Replace with key word to look for.
    Myhandle = hwnd
    WinName = sSave
  End If
  EnumWindowsProc = True
End Function

Code from the form>>

Private Sub Form_Load()
  Dim WinRect As RECT
  EnumWindows AddressOf EnumWindowsProc, ByVal 0&
  GetWindowRect Myhandle, WinRect
  'GetWindowRect TestEnumWin.hwnd, WinRect
  TestEnumWin.TopText = WinRect.Top
  TestEnumWin.BottomText = WinRect.Bottom
  TestEnumWin.LeftText = WinRect.Left
  TestEnumWin.RightText = WinRect.Right
  TestEnumWin.NameText = WinName
End Sub

This is annoyingly the last critical piece in a small animated firework gadget I want to 'dock' to a program called MUSHclient. The idea is to get the windows current size and position, then when ever it gets resized, also move and resize the firework display. Eventually, I want to use the same code in other tools that can be attached to the client as well, like a form for a database, a map displayer and other stuff like the firework gadget that is problematic at best, since it requires code execution that is in a seperate thread from the clients scripting, which would hang the client during execution if used to perform the same task. Without being able to get the above to work right, let alone reliably, I am pretty much sunk... :p

Oh yeah.. I am using VB6 on Windows 98se.
KagehiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony PerkinsCommented:
Please maintain this old open question:

http://www.experts-exchange.com/Web/Graphics/Fonts/Q_20564440.html

Thanks,
Anthony
0
zzzzzoocCommented:
This below works for me. Make sure you're getting the parent-most window of the window rect you're trying to obtain. I'm not sure if it's relevant to the parent or the entire screen if not.


Form1:

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Sub Command1_Click()
    Dim lHandle As Long, lpRect As RECT
    Dim iHeight As Integer, iWidth As Integer
    Dim iLeft As Integer, iTop As Integer
    lHandle = Me.hwnd
    Call GetWindowRect(lHandle, lpRect)
    With lpRect
        iHeight = .Bottom - .Top
        iWidth = .Right - .Left
        iLeft = .Left
        iTop = .Top
    End With
    MsgBox iHeight & "," & iWidth
    MsgBox iLeft & "," & iTop
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KagehiAuthor Commented:
I found a resolution for this on another forum (now that it stopped bloody goind awol ever five minutes..). Turns out the key was in the structure for RECT. Many of the online examples I checked had the structure using integers. :p

As for your comment Anthony.. The font one isn't an open issue any more, I found a solution myself, so it wasn't exactly possible for me to accept anyone elses answer, nor does there appear to be an option for terminating a question that you have resolved yourself on here. Seems like the only option is to hand you points to someone else for not actually solving the problem or... Can you even delete things here? The first option isn't acceptable imho and the second is almost as bad, because it would also delete any information posted about how the person asking the question managed to solve it for themselves. I don't think that is a grand option either. :( So how exactly do I close that thread, without having to give credit for a solution that didn't actually help me?
0
Anthony PerkinsCommented:
>>The font one isn't an open issue any more, I found a solution myself, so it wasn't exactly possible for me to accept anyone elses answer<<
I was not suggesting you accept anyone's solution.  In fact, I would encourage you not to, we have enough bogus solutions in the PAQ, as it is.  If you feel no one answered your question than follow the EE Guidelines:
Nobody answered my question. What do I do?
http://www.experts-exchange.com/help/closing.jsp#5

>>nor does there appear to be an option for terminating a question that you have resolved yourself on here.<<
Again, if you took the time to re-read the EE Guidelines you would have read this:
I answered my question myself. What do I do?
http://www.experts-exchange.com/help/closing.jsp#5

>>Can you even delete things here? <<
I think I just answered that question.

>>So how exactly do I close that thread, without having to give credit for a solution that didn't actually help me?<<
Again, RTFM

Anthony
0
KagehiAuthor Commented:
Thanks I found it myself, but either forgot, glossed over or assumed a more direct 'thread' option that would do it. This is the only forum I have posted to that requires a somewhat conveluted way to end something yourself. But then it is also the only one that uses a point system. It wasn't exactly what I was expecting, and yes I really was expecting a big red button labelled 'Close Me!' or the like. ;) lol
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.