0

I need to open notification form without giving it a focus when it appears. It works fine using P/Invoke except the fact that Shown event is not getting launched. My solution is here, but is there more elegant (more generic) one?

Public Shared Sub ShowInactiveTopmost(frm As Form)
    'standard P/Invoke code
    ShowWindow(frm.Handle, SW_SHOWNOACTIVATE)
    SetWindowPos(frm.Handle.ToInt32(), HWND_TOPMOST,
                 frm.Left, frm.Top, frm.Width, frm.Height, SWP_NOACTIVATE)

    'invocation of 'Shown' event - can this be made independent of 'NotifForm' type?
    CType(frm, NotifForm).NotifForm_Shown(Nothing, New EventArgs())
End Sub

I tried more generic approach using RaiseEvent Shown() but it was throwing error Derived classes cannot raise base class events.

Community
  • 1
  • 1
miroxlav
  • 11,796
  • 5
  • 58
  • 99
  • You could pretend a picture box is the notification form and set it to visible and in front. – Jeremy Thompson May 30 '15 at 07:29
  • @JeremyThompson – yeah, a nice workaround. This time I would like to stay with separate form due to larger possibilities: it can hover over caller form border, it won't mess with tab order in existing form, it can be made draggable... On the other hand, MouseMove and several other events simply do not work. I see I'll need to seek advice beyond the `Shown` event. The funny part is that in WPF, the whole "show form without activation" thing is implemented since .NET 3.5, but in WinForms we are left to workarounds. – miroxlav May 30 '15 at 07:56
  • You already know about the ShowWithoutActivation property. Why you are not using it is very unclear. – Hans Passant May 30 '15 at 11:19

1 Answers1

0

After Hans' comment I've found out that form can be shown unfocused even without P/Invoke, using standard Show() with added override of ShowWithoutActivation property as stated in MSDN description of ShowWithoutActivation property.

Then Shown event works as expected.

'use this with standard form.Show() instead of P/Invoke stuff
Protected Overrides ReadOnly Property ShowWithoutActivation As Boolean
    Get
        Return True
    End Get
End Property
miroxlav
  • 11,796
  • 5
  • 58
  • 99