CALLBACK is just a macro that adds a specialized Microsoft attribute to to a function declaration. Use it ONLY in a declaration, not where you use the function.
The second parameter of EnumChildWindows is declared as a WNDENUMPROC
, which is declared as
typedef BOOL (CALLBACK* WNDENUMPROC)(HWND, LPARAM);
so you would declare your callback function as
BOOL CALLBACK myFunction (HWND w, LPARAM var1);
or
BOOL CALLBACK file::myFunction (HWND w, LPARAM var1);
The scope qualifier ::
always comes right between the scope name (in this case file
) and the dependent name (myFunction
)
If file
is a namespace then you're all set; if file
is a class remember to also declare myFunction static
in the class. You may also need extern
in certain circumstances.
When you enumerate child windows
HWND hwparent=/* handle of the window you want to enumerate */;
LPARAM var1=/* the value you want to pass in*/;
EnumChildWindows (hwParent, myFunction, var1);
Windows calls your function for each window, passing the w
parameter in so you know which window it is and var1
which is your parameter.
As for where: It is not necessary to declare myFunction
in the header file. Most likely only the call to EnumChildWindows
uses myFunction
. It's not going to be called from any other file. So you're OK as long as myFunction
comes before the call to EnumChildWindows
in the .cpp file.
Of course, if file
is a class, then the static member function myFunction
has to be inside the class declaration, which usually goes in a header somewhere.
When you use the function, you only need to add file::
if the place you call it from is not already in the file
(class or namespace) scope.
If file
is a class you need file::
where you define myFunction, but don't need file::
if you're calling EnumChildWindows
in another class member function.
// .h file
class file
{
private:
HWND m_hwParent;
static HRESULT myFunction (HWND hw, LPARAM arg1);
public:
HRESULT OnAllChildWindows (LPARAM arg1);
// more stuff
};
// .cpp file
HRESULT file::myFunction (HWND hw, LPARAM arg1)
{
// whatever this does
}
HRESULT file::OnAllChildWindows (LPARAM arg1)
{
EnumChildWindows (m_hwParent, myFunction, arg1);
}
If file
is a namespace you don't need file::
if you wrapped all your code in a namespace block:
namespace file {
HRESULT myFunction (HWND hw, LPARAM arg1)
{
// whatever this does
}
HRESULT OnAllChildWindows (HWND hwParent, LPARAM arg1)
{
EnumChildWindows (myFunction, arg1);
}
}
but if you didn't, or if it's a different scope:
HRESULT someting_else::OnAllChildWindows (HWND hwParent, LPARAM arg1)
{
EnumChildWindows (hwParent, file::myFunction, arg1);
}