Excel 추가 기능을 작성할 때 비슷한 문제가 발생했습니다. dll 가져 오기가 필요하지 않습니다. System.Windows.Forms.NativeWindow 클래스를 사용하여이 문제를 해결했습니다.
처음에 NativeWindow 클래스에서 상속받은 클래스를 만들고 Activated 및 Deactivate를 선언하고 마지막으로 WndProc() 메서드를 재정 의하여 WMnACroc 메서드가 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