[Resolved]Getting the active window Co-ords
Thanks for those who help with the key-press before!
I've come to another problem in creating that screen shot application.
I have an option that either takes a picture of the entire screen, or of the focused window.
Now, this wouldn't be a problem if i got the screen shot from using the kb_event API call. But i need the Clipboard to stay intact and unchanged, so i use a different way of getting the screen shot:
VB Code:
Public xpos As Long
Public ypos As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Sub Command1_Click()
Dim wScreen As Long
Dim hScreen As Long
Dim w As Long
Dim h As Long
Picture1.Cls
wScreen = Screen.Width \ Screen.TwipsPerPixelX
hScreen = Screen.Height \ Screen.TwipsPerPixelY
Picture1.ScaleMode = vbPixels
'w = Picture1.ScaleWidth
'h = Picture1.ScaleHeight
w = Screen.Width / Screen.TwipsPerPixelX
h = Screen.Height / Screen.TwipsPerPixelY
hdcScreen = GetDC(0)
r = StretchBlt(Picture1.hdc, 0, 0, w, h, hdcScreen, 0, 0, wScreen, hScreen, vbSrcCopy)
End Sub
I know about using the StretchBlt API call to manipulate the picture box.
The part that i'm missing is how to get the active window's top, left, width and height to set the picture. So in other words, it takes the entire screen & cuts off the bit that doesn't belong to the focused window. Is there any way to do this?
Re: Getting the active window Co-ords
left = Me.Left
top = Me.Top
width = Me.Width
height = Me.Height
Re: Getting the active window Co-ords
Ohhhh, sorry. I didn't explain myself correctly.
I mean of any window. Including external windows such as word or media player.
If the user presses the correct key combination then the program will take a screen shot of the entire screen. It will then check if the setting is for the active window only, if so then it will cut off the outside bits of that window. From there it will save automatically, but i haven't reached that yet.
Re: Getting the active window Co-ords
Quote:
Originally Posted by Slyke
...The part that i'm missing is how to get the active window's top, left, width and height ...
Would this work for you?
VB Code:
Option Explicit
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetWindowRect Lib "user32" _
(ByVal hWnd As Long, lpRect As RECT) As Long
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Form_Load()
Timer1.Interval = 1000
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Dim lWnd As Long
Dim rct As RECT
lWnd = GetActiveWindow()
GetWindowRect lWnd, rct
Debug.Print "Left = " & rct.Left * Screen.TwipsPerPixelX
Debug.Print "Top = " & rct.Top * Screen.TwipsPerPixelY
Debug.Print "Height = " & (rct.Bottom - rct.Top) * Screen.TwipsPerPixelX
Debug.Print "Width = " & (rct.Right - rct.Left) * Screen.TwipsPerPixelY
End Sub
Re: Getting the active window Co-ords
Yes, that is what i am looking for, but there's a problem; It works fine on the form and on visual basic itself. But i tried it with notepad and with paint, all that comes back is 0.
I put in Debug.Print lWnd to check if the GetActiveWindow API was being called correctly. This returns 0 on note pad, paint etc. Which means there's an error or something else?
Sorry i took so long, my net dropped out.
Re: Getting the active window Co-ords
Just figured it out.
I did some research on that API you gave me and it says it's only for VB.net. Is there another for 6.0?
http://www.allapi.net/apilist/GetActiveWindow.shtml
EDIT*** Oh wait my bad, it does work for 6... well this site says so any ways.
Edit***EDIT***
VB Code:
Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Long
This API works, however the GetActiveWindow doesn't. Thanks any ways, you gave me a hint, lol.