VSTO Outlook: popup a custom notification on compose window

Viewed 76

I have an VSTO Outlook Add-in and I would like to popup a notification (floating window) with a custom text message for a specific time (5 seconds for example) and then disappear automatically. I want to show this notification from within the compose window. How can I achieve this? some example will highly appreciated.

UPDATED: I would like the notification window to be a child of the compose window.

UPDATE 29.06.2022: I have done the following:

   Outlook.Inspector currentInspector = this.Window as Outlook.Inspector;
    IOleWindow ioleWnd = (IOleWindow)currentInspector;
    ioleWnd.GetWindow(out IntPtr phwnd);

    NativeWindow nativeWnd = new System.Windows.Forms.NativeWindow();
    nativeWnd.AssignHandle(phwnd);
    
    // frm is my notification window, borderless and without maximize, minimize and close buttons and without title bar.
    Form frm = new Form();
    frm.ControlBox = false;
    frm.FormBorderStyle = FormBorderStyle.None;


    NativeMethods.Rect rect = new NativeMethods.Rect();
    NativeMethods.GetWindowRect(phwnd, ref rect);


    frm.Left = rect.Right - 85;
    frm.Top = rect.Bottom - 55;
    frm.Width = 80;
    frm.Height = 50;
    frm.Location = new System.Drawing.Point(rect.Right - 85, rect.Bottom - 55);
    frm.BackColor = System.Drawing.Color.Red;

    TextBox txtBox = new TextBox();
    txtBox.BackColor = System.Drawing.Color.Red;
    txtBox.ForeColor = System.Drawing.Color.White;
    txtBox.Location = new System.Drawing.Point(5, 5);
    txtBox.Visible = true;
    txtBox.Text = "This is a notification message";

    frm.Controls.Add(txtBox);
    frm.Show(nativeWnd);

What happens with above code is below:

  1. Notification window is not positioned on the bottom right hand corner of the compose window.
  2. If I move compose window, notification window keeps in the same position and it is not moving while I move the compose window.
  3. In windows task bar it appears as the notification window is a different process/program, not being part of the same compose window, maybe I need to do something like frm.Owner = nativeWnd but it is not working.

Any ideas?

2 Answers

You can create a custom Windows form that closes itself automatically after a timeout.

To make your form a child of an Outlook inspector, Q/cast the Inspector object (e.g. from Application.ActiveInspector) to the IOleWindow interface, call IOleWindow.GetWindow to get the HWND. Create an instance of the System.Windows.Forms.NativeWindow class and call NativeWindow.AssignHandle. You can then pass the NativeWindow object to Form.Show or Form.ShowModal (NativeWindow object implements the IWin32Window interface).

Use a timer which fires the Tick event on the main thread (UI) where you can call the Close method of your form. The System.Windows.Forms.Timer's event is raised from the UI thread by the message loop when it receives a WM_TIMER message. So, you are good to calling the Close method.

Note, the timer can be run and the form is closed from your code outside of the form or inside the form (built-in to the form).

Related