Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I solved similar problem when writing Excel addin. No dll import is needed. I solved this issue using System.Windows.Forms.NativeWindow class.</p> <p>At first, I made my own class inherited from NativeWindow class and declared two events Activated and Deactivate in it and finaly overrided WndProc() method to rise these events when message WM_ACTIVATE is passed to the WndProc method. According to "Message" parameter WParm is Excel window activated or deactivated.</p> <pre><code> 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); } } </code></pre> <p>Then I made in my addin class field "ExcelWindow myExcelWindow" and added following code to OnConnection method of my addin:</p> <pre><code>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 } </code></pre> <p>I hope this will help you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload