Results 1 to 6 of 6

Thread: [Resolved]Getting the active window Co-ords

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    [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:
    1. Public xpos As Long
    2. Public ypos As Long
    3. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    4. 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
    5.  
    6. Private Sub Command1_Click()
    7. Dim wScreen As Long
    8. Dim hScreen As Long
    9. Dim w As Long
    10. Dim h As Long
    11. Picture1.Cls
    12. wScreen = Screen.Width \ Screen.TwipsPerPixelX
    13. hScreen = Screen.Height \ Screen.TwipsPerPixelY
    14. Picture1.ScaleMode = vbPixels
    15. 'w = Picture1.ScaleWidth
    16. 'h = Picture1.ScaleHeight
    17. w = Screen.Width / Screen.TwipsPerPixelX
    18. h = Screen.Height / Screen.TwipsPerPixelY
    19. hdcScreen = GetDC(0)
    20. r = StretchBlt(Picture1.hdc, 0, 0, w, h, hdcScreen, 0, 0, wScreen, hScreen, vbSrcCopy)
    21. 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?
    Last edited by Slyke; Aug 16th, 2006 at 12:35 PM.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Getting the active window Co-ords

    left = Me.Left
    top = Me.Top
    width = Me.Width
    height = Me.Height
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    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.

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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:
    1. Option Explicit
    2.  
    3. Private Type RECT
    4.     Left As Long
    5.     Top As Long
    6.     Right As Long
    7.     Bottom As Long
    8. End Type
    9.  
    10. Private Declare Function GetActiveWindow Lib "user32" () As Long
    11. Private Declare Function GetWindowRect Lib "user32" _
    12.     (ByVal hWnd As Long, lpRect As RECT) As Long
    13.  
    14. Private Sub Command1_Click()
    15.     Timer1.Enabled = Not Timer1.Enabled
    16. End Sub
    17.  
    18. Private Sub Form_Load()
    19.     Timer1.Interval = 1000
    20.     Timer1.Enabled = False
    21. End Sub
    22.  
    23. Private Sub Timer1_Timer()
    24. Dim lWnd As Long
    25. Dim rct As RECT
    26.  
    27.     lWnd = GetActiveWindow()
    28.     GetWindowRect lWnd, rct
    29.    
    30.     Debug.Print "Left   = " & rct.Left * Screen.TwipsPerPixelX
    31.     Debug.Print "Top    = " & rct.Top * Screen.TwipsPerPixelY
    32.     Debug.Print "Height = " & (rct.Bottom - rct.Top) * Screen.TwipsPerPixelX
    33.     Debug.Print "Width  = " & (rct.Right - rct.Left) * Screen.TwipsPerPixelY
    34.  
    35. End Sub

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    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.
    Last edited by Slyke; Aug 16th, 2006 at 12:26 PM.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    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:
    1. 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.
    Last edited by Slyke; Aug 16th, 2006 at 12:34 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width