Solved

Getting the size of another applications window..

Posted on 2003-11-01
5
234 Views
Last Modified: 2011-09-20
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.
0
Comment
Question by:Kagehi
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9663931
Please maintain this old open question:

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

Thanks,
Anthony
0
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 250 total points
ID: 9665572
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
 

Author Comment

by:Kagehi
ID: 9666570
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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9667584
>>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
 

Author Comment

by:Kagehi
ID: 9667604
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

14 Experts available now in Live!

Get 1:1 Help Now