Solved

Get window size with mouse

Posted on 2000-03-18
24
201 Views
Last Modified: 2008-03-17
I need some code which will do the following:

I need the user to click a button, then a special mouse pointer which I already set will be displayed, then the user can click on any window of his or her choice and the details of that window size will be display...

When I say details I mean.. Size (Width and Height) and also if possible that windows title...
0
Comment
Question by:jasinski
  • 8
  • 5
  • 4
  • +4
24 Comments
 
LVL 1

Expert Comment

by:vsankarv
ID: 2632380
Private Sub Command1_Click()
Form1.MousePointer = Value
End Sub

Private Sub Form_Click()
MsgBox (Me.Width)
MsgBox (Me.Height)
MsgBox (Me.Caption)
End Sub

i think i have given the soln. if not pl explain the q clearly.
0
 
LVL 6

Expert Comment

by:Marine
ID: 2632407
command click or else where
MsgBox Me.ScaleWidth & " " & Me.ScaleHeight & "" & me.caption
0
 
LVL 27

Expert Comment

by:Ark
ID: 2632470
Hi
Place timer on your form with command button.
Paste this code into form code section:

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

Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Dim sWinCaption As String, MyPointer As Integer
Private Sub Command1_Click()
   If Timer1.Enabled Then
      Screen.MousePointer = 0
   Else
      Screen.MousePointer = MyPointer
   End If
   Timer1.Enabled = Not Timer1.Enabled
End Sub

Private Sub Form_Load()
  MyPointer = 2
  Timer1.Enabled = False
  Timer1.Interval = 100
End Sub

Private Sub Timer1_Timer()
   Dim hWin As Long, sName As String, c As Integer
   hWin = GetForegroundWindow
'Uncomment following line if you want get your form info.
'   If hWin = Me.hwnd Then Exit Sub
   sName = String$(255, 0)
   c = GetWindowText(hWin, sName, 256)
   If c <= 0 Then Exit Sub
   sName = Left$(sName, c)
   If sName = sWinCaption Then Exit Sub
   sWinCaption = sName
   Dim rct As RECT
   Call GetWindowRect(hWin, rct)
   Debug.Print "Caption: " & vbTab & sWinCaption
   Debug.Print "Top: " & vbTab & rct.Top
   Debug.Print "Left: " & vbTab & rct.Left
   Debug.Print "Width: " & vbTab & rct.Right - rct.Left
   Debug.Print "Height: " & vbTab & rct.Bottom - rct.Top
   Debug.Print "-----------"
End Sub

Cheers
0
 
LVL 6

Expert Comment

by:Marine
ID: 2632537
Ark its a good sample but realy not neccassary to use API's to get this done. It be very helpful if he needed to find the size of the window and the caption of titlebar from another app. But if its his own then there is no need to use it.
0
 
LVL 27

Expert Comment

by:Ark
ID: 2632546
Hi
Marine: read the question closely, jasinski wrote - "user can click on ANY WINDOW"
Cheers
0
 
LVL 6

Expert Comment

by:Marine
ID: 2632574
Hmm you got a point there sorry i though it was inside his app
0
 
LVL 27

Expert Comment

by:Ark
ID: 2632589
Marine: In the beginning I thought in the same way , looking at thr points value for this question <smile>
Cheers
0
 

Expert Comment

by:FWAllan
ID: 2632759

There is an API call to capture the mouse pointer.  This is dangerous.  You must release the mouse pointer afterwards.

Public Declare Function GetCapture Lib "user32" Alias "GetCapture" () As Long

Public Declare Function ReleaseCapture Lib "user32" Alias "ReleaseCapture" () As Long


In addition to these, you need to determine the window for the mouse click.  There is another API that will do this.


To make the above look really good, you can alter the border of any window the mouse is over, allowing the user to see immediately which window is selected.


The above functions may have altered from W311 to NT/W95.






0
 
LVL 32

Expert Comment

by:Erick37
ID: 2633491
How is that an answer?
0
 

Expert Comment

by:FWAllan
ID: 2634060

It informs the person of the relevent APIs.  I ran out of time to create a module to use the relevent API calls.

0
 

Author Comment

by:jasinski
ID: 2635418

---=+ THANKS TO ALL THE PEOPLE WHO POSTED A MESSAGE !! +=---

I tryed usings "Ark's" code but all it did was change the mouse point.. You can only move it within the form before it changed back to a normal pointer...


Let me explain a little more what I need.


The user click a button...

The pointer changes to whateva u want..

He OR She can move it all over the desktop and click on an open window.

From this point it will display the Title of the window in one text box.

the width in another

and the height in another...



I'm sorry I changed the request but maybe that makes it easy..
0
 
LVL 27

Expert Comment

by:Ark
ID: 2635460
Hi
Do you knoe about Debug.Print? After you stop your programm, Press Ctrl+G and look at debug window. There are the results - windows Captions and sizes of windows, that user clicked.
Cheers
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:VBGuru
ID: 2635716
'Here is the code you requested for.
'Have timer control, 3 text boxes on your form.
'Copy and paste the following code on the form and run.
'In the first text box you will get the title of the window.
'In the 2nd and third text boxes you will get the width and height in pixels respectivly.


Option Explicit

Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
        x As Long
        y As Long
End Type
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Private Sub Form_Load()
Timer1.Interval = 200
End Sub

Private Sub Timer1_Timer()
Dim x As POINTAPI
Dim cliRect As RECT
Dim strName As String
Dim lngHandle As Long

strName = String(255, Chr(0))
GetCursorPos x
lngHandle = WindowFromPoint(x.x, x.y)
GetWindowText lngHandle, strName, 250&
Text1 = strName
GetClientRect lngHandle, cliRect
Text2 = Abs(cliRect.Right - cliRect.Left)
Text3 = Abs(cliRect.Top - cliRect.Bottom)
End Sub
0
 
LVL 6

Expert Comment

by:VBGuru
ID: 2635718
I think you need to increase the points for this question
0
 
LVL 27

Accepted Solution

by:
Ark earned 25 total points
ID: 2638315
Hi
VBGuru: Agreed with your last comment. What's about answer - jasinski asked about Mouse_Click - not about Mouse_Move. Full answer for this question is global mouse hook, but, IMHO, even if multiple points value by 10, it's not a big price for this task. So everybody play with Timer, which, actually will eat a lot of resource. Anyway, here is code for textboxes (BTW, it's almost the same as with "debug.print")

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

Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Dim sWinCaption As String, MyPointer As Integer
Dim hWin As Long
Private Sub Command1_Click()
   If Timer1.Enabled Then
      Screen.MousePointer = 0
   Else
      Screen.MousePointer = MyPointer
   End If
   Timer1.Enabled = Not Timer1.Enabled
End Sub

Private Sub Form_Click()
   If Timer1.Enabled Then
      Text1 = "Caption: " & Caption
      Text2 = Width / Screen.TwipsPerPixelX
      Text3 = Height / Screen.TwipsPerPixelY
      hWin = Me.hwnd
      sWinCaption = Text1
   End If
End Sub

Private Sub Form_Load()
  MyPointer = 2
  Timer1.Enabled = False
  Timer1.Interval = 100
End Sub

Private Sub Timer1_Timer()
   Dim sName As String, c As Integer
   hWin = GetForegroundWindow
   If hWin = Me.hwnd Then Exit Sub
   sName = String$(255, 0)
   c = GetWindowText(hWin, sName, 256)
   If c <= 0 Then Exit Sub
   sName = Left$(sName, c)
   If sName = sWinCaption Then Exit Sub
   sWinCaption = sName
   Dim rct As RECT
   Call GetWindowRect(hWin, rct)
   Text1 = "Caption: " & sWinCaption
'   Debug.Print "Top: " & vbTab & rct.Top
'   Debug.Print "Left: " & vbTab & rct.Left
   Text2 = "Width: " & rct.Right - rct.Left
   Text3 = "Height: " & rct.Bottom - rct.Top
   Me.Show
End Sub

Cheers
0
 
LVL 6

Expert Comment

by:VBGuru
ID: 2638794
If you are interseted to know the window which is currently active and its text,height and width, then just substitute GetForegroundWindow instead of "WindowFromPoint(x.x, x.y)". Rest of the code should simply work.
0
 

Author Comment

by:jasinski
ID: 2638964



--=+ Thanks ARK +=--

How do I select your answer because on my screen it says I have to rate, VBGuru.... ??

p.s It does not have accept answer down the right side of each persons comment
0
 
LVL 27

Expert Comment

by:Ark
ID: 2639017
Hi
VBGuru:if "just substitute GetForegroundWindow instead of "WindowFromPoint(x.x, x.y)"" then your code will be exactly the same as one I've posted before <smile>
jasinski: I don't know, because I still haven't posted questions. I thing you can reject one answer and accept another, but I'm not sure.

Cheers
0
 
LVL 6

Expert Comment

by:VBGuru
ID: 2639074
Ark,
I am not telllig that it is your answer or mine. What you did while putting the comments of yours, you asked for me the solution for the click of the mouse and at the same time you suggested an answer. So if you have not suggested an answer , I would have told the same.
0
 
LVL 27

Expert Comment

by:Ark
ID: 2639132
VBGuru,
I only check "Answer" option button if I can give COMPLETE solution, EXATLY matching question. For this question, as i've already said, comlete solution is "global mouse hoook", which intercept all mouse events. I have no this solution and it's too much work to do this, so I offered some trick with timer. In this case I can post my code only as comment, not as answer, because somebody can offer complete solution. If asking is satisfied with some trick, he/she can accept this comment as an answer, but this is his/her choice.
Cheers
0
 
LVL 6

Expert Comment

by:VBGuru
ID: 2639280
Ark,
you don't have to tell me when I should answer/comment. You may have different opinion about something and I may have different opinion about something and I have not asked for that also.

I told you why my answer with the GetForwardWindow happened to be the same comment which you had posted. Thats it.
0
 
LVL 27

Expert Comment

by:Ark
ID: 2639390
VBGuru,
I didn' tell you when YOU should answer/comment, I only told when I choose answer/comment. I'm newbe at this forum, so I try more listen then speak. I'm sorry, it seems I didn't understand your previous comment (English is not my native language). If I have offended you by something, I ask to accept my apologies.
Cheers (sorry again for my awful English)
0
 

Author Comment

by:jasinski
ID: 2643188
Cause Ark gave me an answer I wanted..
0
 

Author Comment

by:jasinski
ID: 2643205
Ark, U Rock !
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now