0

Is it possible to detect when a form opens inside my application?

I have an assembly which is used and initialized by several applications. I need to use this assembly to detect, when a form inside my application opens, to be able to modify it.

The only solution I found, yet, is using a MessageFilter, EDIT: like Most Efficient Way for getting notified on window open - but they ask for windows in all processes where I only want to get the windows in my own process. Since I'm only interested in my own Application/Process I hope to find a sole .NET solution.

Application.AddMessageFilter(new MessageFilterImpl());

class MessageFilterImpl : IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        Control wnd= Form.FromHandle(m.HWnd);
        if (wnd is Form)
            knownForms.Add((Form)wnd);

        return false;
    }
}

I found the first Message to receive was 0xC052, but I still didn't find its actual meaning.

The first intention would be to choose a better design, but I'm not allowed to.

Community
  • 1
  • 1
Patrik
  • 1,355
  • 12
  • 22
  • possible duplicate of [Most Efficient Way for getting notified on window open](http://stackoverflow.com/questions/21912686/most-efficient-way-for-getting-notified-on-window-open) – Luaan Sep 21 '15 at 08:29
  • Thank you, but Actually, I ask for a solution not involving windows messages, as the solution in the SO-article you provide suggests. – Patrik Sep 21 '15 at 09:51
  • What about deriving your custom class `FormWithNotification` which inherits `Form` and then moving all your forms into that class? – miroxlav Sep 21 '15 at 10:06
  • Of course I thought about that solution, but that's sadly not an option. I may not touch the existing Forms/Projects. An Assembly, which is called in every application must do the job. That is why I ask here - with the option to change the design I would already have done that. – Patrik Sep 21 '15 at 10:18
  • Are there any external dependencies in the app which could be broken? Because if you have apps with 50 forms, you will replace inheritance reference of `System.Windows.Forms.Form` with `System.Windows.Forms.FormWithNotifications` in 50 designer files and you are done. If you add a method to derived form class, there's absolutely nothing you can break. (Except if [this](http://thedailywtf.com/articles/no-changes-please) is kind of your story.) You can create the prototype in 10 mins. But if you are really-really unable to change the design, then:detection through msgs+identifying their app. – miroxlav Sep 21 '15 at 11:03
  • It's not a decision I made... But thank you, I'll rely on messages. – Patrik Sep 21 '15 at 11:06
  • 1
    Umm, there's no way to avoid windows messages in a Windows Forms application - WinForms is a well-behaving windows application framework, so it's well integrated in the windows message infrastructure. Your own solution also uses windows messages. The question I linked has multiple answers - I'd avoid hooks if possible, for example. – Luaan Sep 21 '15 at 11:19
  • I agree. I don't want to use hooks here. The other solution in that linked article doesn't work for me (only triggers on the first window of the application). So I stick with the solution I posted initially. – Patrik Sep 21 '15 at 11:31

0 Answers0