Results 1 to 17 of 17

Thread: [RESOLVED] hwnd help?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Resolved [RESOLVED] hwnd help?

    Code:
    Public Class Form1
        Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Integer, ByVal x As Int32, ByVal y As Int32) As Int32
        Private Declare Function GetForegroundWindow Lib "user32" () As Integer
        Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Integer) As Integer
        Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Integer, ByVal hdc As Integer) As Integer
    
      Private Sub MSPaint()
            Dim HDC As Integer
            Dim hWnd As Integer = GetForegroundWindow()
            HDC = (GetWindowDC(hWnd))
            If GetPixel(HDC, 400, 400) = &HFFFFFF Then
                MsgBox("Correct!")
            Else
                MsgBox("Fail!")
            End If
            ReleaseDC(hWnd, HDC)
        End Sub
    Is there anyway to activate a window first, and then perform getpixel on that active window? Also, is there anyway to use the window title as the hwnd?

    For instance: Untitled - Paint instead of GetForegroundWindow.

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: hwnd help?

    Here's a pretty good example:

    http://www.codeguru.com/forum/showthread.php?p=1757526

    The code is C# but it should be pretty easy to follow.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: hwnd help?

    Great link, but I'm having alot of problems converting the c# api to vb.net api.

    Can someone post an example code of what i'm trying to do?

  4. #4

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: hwnd help?

    how can i turn:

    Code:
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow( _
         ByVal lpClassName As String, _
         ByVal lpWindowName As String) As IntPtr
    End Function
    
    <DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowByClass( _
         ByVal lpClassName As String, _
         ByVal zero As IntPtr) As IntPtr
    End Function
    
    <DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowByCaption( _
         ByVal zero As IntPtr, _
         ByVal lpWindowName As String) As IntPtr
    End Function
    into a simple and single line of code:

    Code:
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Integer) As Integer

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: hwnd help?

    anyone?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: hwnd help?

    Ok well I've found some info, but still need help.

    Code:
     Dim WindowName As String
            Dim Window As Long
            WindowName = "Untitled - Notepad"
            Window = FindWindow(vbNullString, WindowName)
            AppActivate(WindowName)
    This code works great other than the fact that it doesn't restore the window if it's minimized. How can I have it restore the minimized window IF IT'S minimized.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: hwnd help?

    try this:

    vb Code:
    1. Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
    2.  
    3. Dim SW_HIDE As Integer = 0
    4. 'Hide the window.
    5. Dim SW_MAXIMIZE As Integer = 3
    6. 'Maximize the window.
    7. Dim SW_MINIMIZE As Integer = 6
    8. 'Minimize the window.
    9. Dim SW_RESTORE As Integer = 9
    10. 'Restore the window (not maximized nor minimized).
    11. Dim SW_SHOW As Integer = 5
    12. 'Show the window.
    13. Dim SW_SHOWMAXIMIZED As Integer = 3
    14. 'Show the window maximized.
    15. Dim SW_SHOWMINIMIZED As Integer = 2
    16. 'Show the window minimized.
    17. Dim SW_SHOWMINNOACTIVE As Integer = 7
    18. 'Show the window minimized but do not activate it.
    19. Dim SW_SHOWNA As Integer = 8
    20. 'Show the window in its current state but do not activate it.
    21. Dim SW_SHOWNOACTIVATE As Integer = 4
    22. 'Show the window in its most recent size and position but do not activate it.
    23. Dim SW_SHOWNORMAL As Integer = 1
    24. 'Show the window and activate it (as usual).

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: hwnd help?

    Thanks paul, but how can i Show "Untitled - Notepad"

    Dim SW_SHOWNORMAL As Integer = 1 looks like it does any window, but not the hwnd. i need it to only restore notepad if it's minimized and then activate it.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: hwnd help?

    you need to use findwindow to get the window handle (hWnd) then use something like this:

    vb Code:
    1. Dim WindowName As String
    2. Dim Window As intptr
    3. WindowName = "Untitled - Notepad"
    4. Window = FindWindow(vbNullString, WindowName)
    5. ShowWindow(Window, SW_SHOWNORMAL)
    6. AppActivate(WindowName)

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: hwnd help?

    thanks paul.

    now how can i declare the SW_SHOWNORMAL?

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: hwnd help?

    Ok so I have:

    Dim WindowName As String
    Dim Window As IntPtr
    WindowName = "Untitled - Notepad
    Window = FindWindow(vbNullString, WindowName)
    ShowWindow(Window, SW_SHOWNORMAL)
    AppActivate(WindowName)

    And this in my declarations:

    Public Const SW_SHOWNORMAL = 1

    And I receive error: Arithmetic Operation Resulted in An Overflow

    And this : Window = FindWindow(vbNullString, WindowName)

    is highlighted as the error I guess.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: hwnd help?

    there's a discrepancy in your variable and/or api declarations.
    you need to check all of your datatypes

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: hwnd help?

    ok so I cleared everything that had to do with activating the window.

    my declarations are :

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
    Public Const SW_SHOWNORMAL = 1

    My code is still exactly the same as what you posted. I don't know how I can make this work. everything looks like it falls in place, but it doesn't..

    thanks for the help.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: hwnd help?

    you should turn Option Strict on in all of your projects + it'll help you identify datatype discrepancies before they cause a problem:

    vb Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    2. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer
    3. Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
    4. Public Const SW_SHOWNORMAL As Integer = 1

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: hwnd help?

    thanks paul! code works great.

  17. #17
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: hwnd help?

    Quote Originally Posted by Ritzky View Post
    This code works great other than the fact that it doesn't restore the window if it's minimized. How can I have it restore the minimized window IF IT'S minimized.
    One simple way is to use the IsIconic API to test if a window is minimized, theres also the IsZoomed to tell you if the window is maximized, and if its not either then its in a normal windowstate.

    Example..., (see pauls code for ShowWindow)

    Code:
    <DllImport("user32.dll")> _
    Private Shared Function IsZoomed(hWnd As IntPtr) As Boolean
    End Function
    
    Private Declare Auto Function IsIconic Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
    
    Window = FindWindow(Nothing, WindowName)
    
    ' Always make sure you have a vaild window handle before calling APIs!
    If Not Window.Equals(IntPtr.Zero) Then 
      ' Restore window if minimized
      If IsIconic(Window) Then
          ShowWindow(Window, SW_RESTORE)
      End If
    End If
    Last edited by Edgemeal; Feb 9th, 2012 at 09:16 PM. Reason: check handle

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