ThisAddIn.cs (or a dedicated controller class) using Excel = Microsoft.Office.Interop.Excel; using System; using System.Windows; namespace YourAddIn {     public partial class ThisAddIn     {         private CellOverlayWindow overlay;         private bool overlayActive;         private bool editDirectlyPrev;         private void ThisAddInStartup(object sender, System.EventArgs e)         {             overlay = new CellOverlayWindow();             overlay.CommitAndHide += OverlayCommitAndHide;             overlay.CancelAndHide += OverlayCancelAndHide;             // Subscribe at Application level: one handler for all sheets             Application.SheetSelectionChange += ApplicationSheetSelectionChange;         }         private void ThisAddInShutdown(object sender, System.EventArgs e)         {             Application.SheetSelectionChange -= ApplicationSheetSelectionChange;             if (overlay != null)             {                 overlay.Close();             }         }         private void ApplicationSheetSelectionChange(object Sh, Excel.Range Target)         {             // Reposition and show overlay over Target, then DEFER focus.             ShowOverlayFor(Target);         }         private void ShowOverlayFor(Excel.Range target)         {             if (target == null) return;             var win = Application.ActiveWindow;             if (win == null) return;             // Compute screen rectangle of the cell in pixels (using Excel API)             GetCellScreenRect(target, win, out int x, out int y, out int w, out int h);             overlay.Left   = x;             overlay.Top    = y;             overlay.Width  = Math.Max(40, w);  // minimal width             overlay.Height = Math.Max(20, h);             if (!overlay.IsVisible)                 overlay.Show();             // Disable in-cell edit while overlay is active (restore later)             if (!overlayActive)             {                 editDirectlyPrev = Application.EditDirectlyInCell;                 Application.EditDirectlyInCell = false;                 overlayActive = true;             }             // Set the text from the cell (optional)             overlay.Editor.Text = Convert.ToString(target.Value2 ?? string.Empty);             // Defer focus so Excel finishes its own activation             overlay.FocusEditorDeferred();             // If some environments still steal focus, uncomment fallback:             // overlay.ForceWin32Focus();         }         private void OverlayCommitAndHide(object sender, EventArgs e)         {             try             {                 var rng = (Excel.Range)Application.ActiveCell;                 if (rng != null)                 {                     rng.Value2 = overlay.Editor.Text;                 }             }             catch { /* handle/log if needed / }             finally             {                 HideOverlayAndRestoreExcel();             }         }         private void OverlayCancelAndHide(object sender, EventArgs e)         {             HideOverlayAndRestoreExcel();         }         private void HideOverlayAndRestoreExcel()         {             if (overlay?.IsVisible == true)                 overlay.Hide();             if (overlayActive)             {                 Application.EditDirectlyInCell = editDirectlyPrev;  // restore previous setting                 overlayActive = false;             }             // Return focus to Excel             try             {                 Application.ActiveWindow?.Activate();             }             catch { / ignore */ }         }         ///         /// Converts the Excel Range rectangle (points) to screen pixels using Excel’s Window conversion helpers.         ///         private static void GetCellScreenRect(Excel.Range target, Excel.Window win,                                               out int x, out int y, out int width, out int height)         {             // Range.Left/Top/Width/Height are in points relative to the sheet.             double leftPt = (double)target.Left;             double topPt  = (double)target.Top;             double wPt    = (double)target.Width;             double hPt    = (double)target.Height;             // Convert points to screen pixels via ActiveWindow.PointsToScreenPixelsX/Y             // Convert both corners then subtract.             win.PointsToScreenPixelsX((int)Math.Round(leftPt), out x            win.PointsToScreenPixelsX((int)Math.Round(leftPt), out x);             win.PointsToScreenPixelsY((int)Math.Round(topPt),  out y);             int x2, y2;             win.PointsToScreenPixelsX((int)Math.Round(leftPt + wPt), out x2);             win.PointsToScreenPixelsY((int)Math.Round(topPt  + hPt), out y2);             width  = Math.Max(1, x2 - x);             height = Math.Max(1, y2 - y);         }         #region VSTO generated code         private void InternalStartup()         {             this.Startup += new System.EventHandler(ThisAddInStartup);             this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);         }         #endregion     } 3)