A
Alex
Hello,
I am writing a Word add-in in C#.
I am trying to create a dialog window that will behave similarly to the "Find" dialog in Word or Visual Studio
(actually, I try to mimic the Visual Studio "find" as it does not appear on the taskbar).
That is:
- The dialog is modeless (the user can continue working in the application when it is open).
- The dialog is always on top of the application window (but not on top of other applications).
So far I am failing miserably.
What am I doing wrong?
Here are the relevant parts of the code:
////////// code begins //////////
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Extensibility;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
[GuidAttribute("3C6ED867-A0F7-4747-BBAA-34D1D8FA2E3E"), ProgId("MyAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2 {
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = (Word.Application)application;
addInInstance = addInInst;
if (connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup) {
OnStartupComplete(ref custom);
}
}
public void OnStartupComplete(ref System.Array custom)
{
applicationObject.CustomizationContext = applicationObject.ActiveDocument;
CommandBar commandBar = applicationObject.CommandBars.Add("My Command Bar", MsoBarPosition.msoBarTop, false, false);
CommandBarButton button = (CommandBarButton) commandBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true);
button.Tag = "Launch Panel";
button.Click += new _CommandBarButtonEvents_ClickEventHandler(LaunchPanel);
commandBar.Visible = true;
}
private void LaunchPanel(CommandBarButton button, ref bool cancel)
{
string caption = applicationObject.ActiveWindow.Caption + " - " + applicationObject.Caption;
Form1 form = new Form1(ref caption);
form.Show();
}
private Word.Application applicationObject;
private object addInInstance;
static object missing = System.Reflection.Missing.Value;
}
public class Form1 : System.Windows.Forms.Form
{
public Form1(ref string caption)
{
InitializeComponent();
IntPtr hwnd = Win32.FindWindow("OpusApp", caption);
Win32.SetParent(Handle, hwnd);
int style = Win32.GetWindowLong(Handle, Win32.GWL_STYLE);
Win32.SetWindowLong(Handle, Win32.GWL_STYLE, style | Win32.WS_CHILD);
MaximizeBox = false;
MinimizeBox = false;
ControlBox = false;
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.FixedToolWindow;
}
}
public class Win32
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
public const int GWL_EXSTYLE = -20;
public const int WS_CHILD = 0x40000000;
}
////////// code ends //////////
Thnank you,
Alex.
I am writing a Word add-in in C#.
I am trying to create a dialog window that will behave similarly to the "Find" dialog in Word or Visual Studio
(actually, I try to mimic the Visual Studio "find" as it does not appear on the taskbar).
That is:
- The dialog is modeless (the user can continue working in the application when it is open).
- The dialog is always on top of the application window (but not on top of other applications).
So far I am failing miserably.
What am I doing wrong?
Here are the relevant parts of the code:
////////// code begins //////////
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Extensibility;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
[GuidAttribute("3C6ED867-A0F7-4747-BBAA-34D1D8FA2E3E"), ProgId("MyAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2 {
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = (Word.Application)application;
addInInstance = addInInst;
if (connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup) {
OnStartupComplete(ref custom);
}
}
public void OnStartupComplete(ref System.Array custom)
{
applicationObject.CustomizationContext = applicationObject.ActiveDocument;
CommandBar commandBar = applicationObject.CommandBars.Add("My Command Bar", MsoBarPosition.msoBarTop, false, false);
CommandBarButton button = (CommandBarButton) commandBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true);
button.Tag = "Launch Panel";
button.Click += new _CommandBarButtonEvents_ClickEventHandler(LaunchPanel);
commandBar.Visible = true;
}
private void LaunchPanel(CommandBarButton button, ref bool cancel)
{
string caption = applicationObject.ActiveWindow.Caption + " - " + applicationObject.Caption;
Form1 form = new Form1(ref caption);
form.Show();
}
private Word.Application applicationObject;
private object addInInstance;
static object missing = System.Reflection.Missing.Value;
}
public class Form1 : System.Windows.Forms.Form
{
public Form1(ref string caption)
{
InitializeComponent();
IntPtr hwnd = Win32.FindWindow("OpusApp", caption);
Win32.SetParent(Handle, hwnd);
int style = Win32.GetWindowLong(Handle, Win32.GWL_STYLE);
Win32.SetWindowLong(Handle, Win32.GWL_STYLE, style | Win32.WS_CHILD);
MaximizeBox = false;
MinimizeBox = false;
ControlBox = false;
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.FixedToolWindow;
}
}
public class Win32
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
public const int GWL_EXSTYLE = -20;
public const int WS_CHILD = 0x40000000;
}
////////// code ends //////////
Thnank you,
Alex.