Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 67,915 times

Contents

Related Categories

Enum Windows & SendMessage API - EnumWindows

EnumWindows

EnumWindows enumerate (returns) handles of all the top-level windows. Now most of the times the enumeration, whether its of parent windows or child windows or fonts etc, is done with the help of a ‘callback’ function. You need not overwhelm yourself with the word ‘callback’, its not a big thing actually. It’s like this: the EnumWindows will pass the handles of all the top-level windows that it’ll find to the callback function which ‘you’ will write yourself. Its easy, in fact its declaration already is available to you in the API documentation in the MSDN library. Your program can have several callback functions, but how does EnumWindows know which callback function to pass the handles to? You manage it yourself by giving it the ‘address’ of the callback function that you want to use with EnumWindows, and you get the address of the function with the help of a especial operator called ‘AddressOf’ operator (it was first time introduced in VB5). Following is the declaration of EnumWindows as given in the API viewer:

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

The parameter ‘lpEnumFunc’ requires the address of the function that you’ll will be using as a callback.

The parameter ‘lParam’ is an application-defined value. This value is of no use to EnumWindows and it is just there for you to pass any value you want to the callback function. Although it is of type long here but it can be of any type you want, you’ll have to change the type in the declaration likewise, like I wanted to pass a listview control to the callback so I changed the type to ‘Any”, this will be explained later on.

Now the callback function. It is not declared with the ‘Declare’ statement, you would write it like any other function but you’ll have to observe the number of arguments it can receive which is also given in the MSDN library. It’s normally defined as:

Public Function WndEnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
End Sub

hwnd is the handle of the window that EnumWindow passes it.

lParam is the user-defined value, but because I was passing it a ListView control so I changed the lParam type as ‘ListView’ so that it could accept a ListView object.

Note: you must place the callback function in the ‘Module’ i.e. the *.bas file because it’s a condition of the AddressOf operator that the function whose address is passed should be located in the *Bas file.

Also AddressOf can only be used as argument of any function like:

x = somefunction(AddressOf anyuserdefinedfunction)

you cant write it as:

x = addressof(anyvariableoruserdefinedfunction) ‘ this is illegal

Once you’ve called the EnumWindows function and given it the address of the callback function, it will start passing the handles of all the windows that it’ll find. When the execution reaches inside the callback function you can do whatever you want with the passed on data but in the last line of the function you must ‘tell’ the EnumWindows whether it should continue passing the handles or not. If you want EnumWindows to keep on passing you the handles you return 1 else you return 0. In EnumerationX I wanted EnumWindows to pass me all the handles it found so I kept returning 1 until it stopped itself.

In EnumerationX, when inside the WndEnumProc (callback) I took the handle, got the caption of the window through GetWindowText and filled the listview control with the window names.

EnumChildWindows is exactly the same function as EnumWindows except that it enumerates child window handles. I hope that through the explanation of EnumWindows you’ll be easily able to understand the working of EnumChildWindows.

Nothing to say anything about me yet.

Comments

  • [HELP] SendMessage API

    Posted by khickyphutz on 27 Jul 2007

    Hi All,

    I wanna ask about the equivalent of SendKey if I will use SendMessage:

    Ex.

        If ctr2 <= length Then
            myKey...

  • Re: cann't get selected text for HTML file/VC/PDF file/SourceInsight File

    Posted by wangdl5201314 on 13 Jul 2007

    you said you can't get selected text from html file..etc ,may be you havn't find the right handle of area which can be edit.

  • Re: [34] Enum Windows &amp; SendMessage API

    Posted by fcescobar on 10 Jul 2007

    Hi,


    About this subject, I´m trying to define the size of an image captured from a generic camera.


    I'm using the SendMessage and SetWindowPos functions with the following instru...

  • Posted by erictioh on 21 Jan 2006

    i'm doing something similar to this thread starter. below is my code:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    prevWindow =...

  • Posted by DavidChiu on 09 Jun 2005

    [quote][1]Posted by [b]arshad[/b] on 29 Jun 2004 12:18 PM[/1]
    Hi Riaz,
    Could you plz explain me how did u fetch data from MS-Word.
    I can able to get typed text from notepad using sendmessage API.
    ...