Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Get window size with mouse

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
jasinski
Asked:
jasinski
  • 8
  • 5
  • 4
  • +4
1 Solution
 
vsankarvCommented:
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
 
MarineCommented:
command click or else where
MsgBox Me.ScaleWidth & " " & Me.ScaleHeight & "" & me.caption
0
 
ArkCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MarineCommented:
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
 
ArkCommented:
Hi
Marine: read the question closely, jasinski wrote - "user can click on ANY WINDOW"
Cheers
0
 
MarineCommented:
Hmm you got a point there sorry i though it was inside his app
0
 
ArkCommented:
Marine: In the beginning I thought in the same way , looking at thr points value for this question <smile>
Cheers
0
 
FWAllanCommented:

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
 
Erick37Commented:
How is that an answer?
0
 
FWAllanCommented:

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

0
 
jasinskiAuthor Commented:

---=+ 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
 
ArkCommented:
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
 
VBGuruCommented:
'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
 
VBGuruCommented:
I think you need to increase the points for this question
0
 
ArkCommented:
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
 
VBGuruCommented:
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
 
jasinskiAuthor Commented:



--=+ 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
 
ArkCommented:
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
 
VBGuruCommented:
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
 
ArkCommented:
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
 
VBGuruCommented:
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
 
ArkCommented:
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
 
jasinskiAuthor Commented:
Cause Ark gave me an answer I wanted..
0
 
jasinskiAuthor Commented:
Ark, U Rock !
0
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.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 4
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now