CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2000
    Posts
    24

    hwnd of active window

    hi,
    Can anybody help me in solving this,
    if user opens a notepad window,it should be notified to my application, and i should get the hwnd of that.

    thanx



  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: hwnd of active window

    Now, this is just an idea, and you will have to work to set it.
    From api-guide I extracted an example about enumwindowproc.
    Add a timer to a form
    and a module (.bas) to the project
    add this code to form:
    '----------------------------------------------------
    Private Sub Timer1_Timer()
    'call the Enumwindows-function
    Me.Cls
    EnumWindows AddressOf EnumWindowsProc, ByVal 0&
    End Sub

    '----------------------------------------------------
    'add this code to module:
    Option Explicit

    Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
    Dim sSave As String, Ret As Long
    Dim foundpos As Integer
    Ret = GetWindowTextLength(hwnd)
    sSave = Space(Ret)
    GetWindowText hwnd, sSave, Ret + 1
    'for make you see, print all to the form
    'rem this if not needed
    Form1.AutoRedraw = True
    Form1.Print Str$(hwnd) + " " + sSave
    ''hwnd is window handle; sSave, the title, if any
    'now, if you find a title, look if you already added it to form
    'caption. For your purpouse, you should add this to an array
    'from where to search if a desiderd program is opened
    'you may furter look for Notepad inside a cell
    string of your array (remember whole string would be:
    'title of file opened and notepad
    'ie: mydoc1 - notepad)
    If Trim(sSave) <> "" Then
    foundpos = InStr(1, Form1.Caption, sSave, vbTextCompare)
    If foundpos = 0 Then
    Form1.Caption = Form1.Caption & " " & sSave
    End If
    End If
    'continue enumeration
    EnumWindowsProc = True
    End Function

    Hope this may give a hint

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: hwnd of active window

    ... timer interval is ok over 5000 ms


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured