How to get the hwnd of an app....

  • 19 years ago

    Hi all,


    I got a problem with my prog. It starts an Application via the Shell function. Then, if u click another button in my prog it should do something with the started prog. For example:


    1. U click button1 on my prog. It starts Notepad.exe via Shell("notepad.exe"),vbnormalfocus. Now the notepad app has the focus.
    2. u change back to my prog, and click button2. It now should send some text to notepad using the Sendkeys function.


    There is the prob. If i start Notepad via the Shell function, and later wants to send some text with another button, it cant find notepad. So i think i should use the hwnd function to get the hwnd of notepad. Then i should be able to send some text. But how should i do that ? I have tried some things, but nothing works
    And how can i make my prog always stay on top so it dont minimize if i change to another app ?
    I hope anybody can help me with this prob.
    If u got more questions about my prob please ask. And if u have an working solution 4 it, I´ll be very happy


    Glow

  • 19 years ago

    The following code is kindly provided by an excelent resourse called www.vbapi.com. It lists loads of API calls for windoze and is a great site. In order to "find" the hWnd of a window, you need to know it's title (in the blue bar at the top). This function returns the hWnd so you can use the "SendKeys" function on it.


    Regards,


    Jonathan


    ' This code is licensed according to the terms and conditions listed here.


    ' Declarations and such needed for the example:
    ' (Copy them to the (declarations) section of a module.)
    Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
       ByVal lpWindowName As String) As Long
    Public Declare Function FlashWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal bInvert As Long) As Long
    Public Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)


    ' * Place the following code inside a form. *


    Private Sub cmdFind_Click()
       Dim hWnd As Long    ' receives handle to the found window
       Dim retval As Long  ' generic return value
       
       ' Attempt to locate a window titled Minesweeper.
       hWnd = FindWindow(vbNullString, "Minesweeper")
       If hWnd = 0 Then
           Debug.Print "Minesweeper is not currently running."
       Else
           ' Flash the window's title bar on and off once. Or use your sendkeys function.
           retval = FlashWindow(hWnd, 1)
           Sleep 500  ' pause for half a second
           retval = FlashWindow(hWnd, 0)
       End If
    End Sub

  • 19 years ago

    Jonathan ... we now have some new code for links etc, so that HTML code doesn't muck up the forum layout....
    See here for more information....

  • 19 years ago

    Hi


    I got it to work. But I used another way. First I used the FindWindow code. then I used BringWindowToTop to give the app the focus. Now i can easily send the text to notepad
    But thx 4 ur help


    Glow

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”