【问题标题】:VSTO- Event to capture a click from taskbar on the excel workbookVSTO-事件以捕获从 Excel 工作簿上的任务栏单击
【发布时间】:2012-10-02 02:00:31
【问题描述】:

我正在开发 VSTO excel 2007 工作簿应用程序,并正在寻找一个跟踪 Excel 应用程序点击的事件。

有两种情况:-

  1. 用户在单击任务栏中的 Excel 图标后进入 Excel。
  2. 用户在按 ALT+TAB 后进入 Excel 工作表

我试过了

 ThisWorkbook_ActivateEvent();

this.Application.WindowActivate

但他们似乎没有工作。

【问题讨论】:

    标签: c# excel vsto excel-2007 taskbar


    【解决方案1】:

    这是一个完整的 VSTO 解决方案,它应该可以工作,虽然它不是很好,因为它使用了一个计时器。我已经用你的两个场景测试了它。

    约尔格


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools.Excel;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Diagnostics;
    
    namespace ExcelAddIn_TestExcelWindowActivation
    {
        public partial class ThisAddIn
        {
            [DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
            public static extern IntPtr GetForegroundWindow();
    
            private IntPtr _excelWindowHandle = IntPtr.Zero;
            private IntPtr _lastForegroundWindowHandle = IntPtr.Zero;
            private Timer _timerForegroundWindowObserver = null;
    
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                //Get excel handle
                _excelWindowHandle = new IntPtr(Globals.ThisAddIn.Application.Hwnd);
    
                //Initialize and start the timer
                _timerForegroundWindowObserver = new Timer();
                _timerForegroundWindowObserver.Interval = 1000; //ms
                _timerForegroundWindowObserver.Tick +=new EventHandler(_timerForegroundWindowObserver_Tick);
                _timerForegroundWindowObserver.Start();
    
                Debug.Print("ThisAddIn_Startup completed.");
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
                //Stop and delete the timer
                _timerForegroundWindowObserver.Stop();
                _timerForegroundWindowObserver = null;
            }
    
            private void _timerForegroundWindowObserver_Tick(object sender, EventArgs e)
            {
                var foregroundWindowHandle = GetForegroundWindow();
    
                //Remember the last foreground window and exit if there were no changes...
                if (foregroundWindowHandle == _lastForegroundWindowHandle) return;
                _lastForegroundWindowHandle = foregroundWindowHandle;
    
                //When Excel is activated: Give info...
                if (foregroundWindowHandle == _excelWindowHandle)
                {
                    Debug.Print("Excel window is activated yet.");
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢!真的很感激。已经 3 年了 :) 但对于新的 VSTO 开发人员来说应该会派上用场。不幸的是,我无法测试它,因为我不再使用 VSTO。
    • 它有效。我已经测试过了。如果你能接受它作为答案,我会很高兴。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    相关资源
    最近更新 更多