-
Events and Delegate
Ok, it's days I'm in trouble with this... it should be easy!
Say we have to do a class (say Windows) wich wraps EnumWindows (DllImpors[(user32.dll)])
This class have just to raise the event Enumerate for each call of EnumWindows to its callback.
I've no idea about how to do it! :confused: :confused:
PHP Code:
public class Windows
[DllImport("user32.dll")] private static extern
int EnumWindows(EnumWindowsProc ewp, int lParam);
//delegate used for EnumWindows() callback function
public delegate bool EnumWindowsProc(int hWnd, int lParam);
public Windows()
{
//Declare a callback delegate for EnumWindows() API call
EnumWindowsProc ewp = new EnumWindowsProc(EvalWindow);
//Enumerate all Windows
EnumWindows(ewp, 0);
}
//EnumWindows CALLBACK function
private bool EvalWindow(int hWnd, int lParam)
{
// RAISE THE EVENT!
// HOW???
}
}
}
How to fill the "raising of event"?
Thanx
-
What parameters do you want the event to have? Here is something basic. You can change it according to your needs.
Code:
public class Windows
[DllImport("user32.dll")] private static extern
int EnumWindows(EnumWindowsProc ewp, int lParam);
//delegate used for EnumWindows() callback function
public delegate bool EnumWindowsProc(int hWnd, int lParam);
public delegate void EnumWindowsHandler(string msg);
public event EnumWindowsHandler Log;
public Windows()
{
//Declare a callback delegate for EnumWindows() API call
EnumWindowsProc ewp = new EnumWindowsProc(EvalWindow);
//Enumerate all Windows
EnumWindows(ewp, 0);
}
//EnumWindows CALLBACK function
private bool EvalWindow(int hWnd, int lParam)
{
// RAISE THE EVENT!
if(Log != null)
{
Log("Enumeration Complete");
}
}
}
}