我在編寫Excel插件時解決了類似的問題。沒有dll導入是必要的。我使用System.Windows.Forms.NativeWindow類解決了這個問題。
起初,我讓自己的類繼承自NativeWindow類,並在其中聲明瞭兩個事件Activated和Deactivate,並在WM_ACTIVATE消息傳遞給WndProc方法時最終重寫WndProc()方法以產生這些事件。根據「消息」參數,WParm將Excel窗口激活或取消激活。
public class ExcelWindow: NativeWindow
{
public const int WM_ACTIVATED = 0x0006;
public ExcelWindow():base(){}
//events
public event EventHandler Activated;
public event EventHandler Deactivate;
//catching windows messages
protected override void WndProc(ref Message m)
{
if (m.Msg== WM_ACTIVATED)
{
if (m.WParam.ToInt32() == 1)
{
//raise activated event
if (Activated!=null)
{
Activated(this, new EventArgs());
}
}
else if (m.WParam.ToInt32() == 0)
{
//raise deactivated event
if (Deactivate!=null)
{
Deactivate(this, new EventArgs());
}
}
}
base.WndProc(ref m);
}
}
然後我在插件類字段「ExcelWindow myExcelWindow」製成,添加以下代碼OnConnection方法我插件的:
ExcelWindow myExcelWindow;
void Extensibility.IDTExtensibility2.OnConnection(object application, Extensibility.ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
{
excel = application as Excel.Application;
myExcelWindow = new ExcelWindow();
myExcelWindow.AssignHandle(new IntPtr(excel.Hwnd));
myExcelWindow.Activated += new EventHandler(myExcelWindow_Activated);
myExcelWindow.Deactivate += new EventHandler(myExcelWindow_Deactivate);
//addin code here
}
void myExcelWindow_Activated(object sender, EventArgs e)
{
//do some stuff here
}
void myExcelWindow_Deactivate(object sender, EventArgs e)
{
//do some stuff here
}
我希望這會幫助你。
這是驚人的,你知道如果有一種方法可以在Word/PowerPoint中做到這一點?更具體地說,這些應用程序是否有.Hwnd類型的屬性? – Tom 2013-10-03 20:44:22