I’d like to create a dll file in C# that has a static method calling Application.AddMessageFilter() on a Win form loading this dll.
For example,
public Form1()
{
// initializing
MyDll.InvokeFilter(SomeClass); // Perfect!
}
And I don’t want it to look like as below, (I don’t even think that this code would correctly work though)
public Form1()
{
// initializing
// Send also the delegate to Application.AddMessageFilter as a parameter
// to let MyDll know it, which is not as good.
MyDll.InvokeFilterWithDelegate(SomeClass, Application.AddMessageFilter);
}
The thing is I don’t know how to invoke Application.AddMessageFilter from my Dll file because Application class belongs to Form1, not to my Dll library.
What am I missing?
Thanks in advance.
The
Applicationclass, or more specificallySystem.Windows.Forms.Applicationdoes not “belong to Form1”, it can be found inSystem.Windows.Forms.dll. Add a reference to it in your DLL project.There is a potential issue with ApplicationContexts, but it’s unlikely the caller would be making the call from anything other than the primary UI thread.
Having said that, if “SomeClass” is just an implementation of
IMessageFilter, what’s the point of your library function? They can just callApplication.AddMessageFilterthemselves.