/// <summary> /// In order for a dialog to interact with OneNote, it must run modeless so it doesn't /// block the OneNote main UI thread. This method runs the current form as a modeless /// window and invokes the specified callbacks upon OK and Cancel. /// </summary> /// <param name="closedAction"> /// An event handler to run when the modeless dialog is closed /// </param> /// <param name="topDelta"> /// Optionally percentage of the dialog height to subtract from the top coordinate, 0-100 /// </param> public void RunModeless(EventHandler closedAction = null, int topDelta = 0) { StartPosition = FormStartPosition.Manual; TopMost = true; modeless = true; var rect = new Native.Rectangle(); using (var one = new OneNote()) { Native.GetWindowRect(one.WindowHandle, ref rect); } var yoffset = (int)(Height * topDelta / 100.0); Location = new System.Drawing.Point( (rect.Left + ((rect.Right - rect.Left) / 2)) - (Width / 2), (rect.Top + ((rect.Bottom - rect.Top) / 2)) - (Height / 2) - yoffset ); if (closedAction != null) { ModelessClosed += (sender, e) => { closedAction(sender, e); }; } var thread = new Thread(() => { Application.Run(this); }); thread.Start(); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// <summary> /// Called after Show() /// </summary> /// <param name="e"></param> protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (execute == null) { StartPosition = FormStartPosition.Manual; TopMost = true; var rect = new Native.Rectangle(); using (var one = new OneNote()) { Native.GetWindowRect(one.WindowHandle, ref rect); } var yoffset = (int)(Height * 20 / 100.0); Location = new System.Drawing.Point( (rect.Left + ((rect.Right - rect.Left) / 2)) - (Width / 2), (rect.Top + ((rect.Bottom - rect.Top) / 2)) - (Height / 2) - yoffset ); } }