c# - VSTO-捕获来自 excel 工作簿上任务栏的单击的事件

标签 c# excel vsto excel-2007 taskbar

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

有两种情况:-

  1. 用户在单击任务栏中的 excel 图标后进入 excel。
  2. 用户在按 ALT+TAB 后出现在 excel 表上

enter image description here

我试过了

 ThisWorkbook_ActivateEvent();

this.Application.WindowActivate

但他们似乎并没有工作。

最佳答案

这是一个完整的 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.");
            }
        }
    }
}

关于c# - VSTO-捕获来自 excel 工作簿上任务栏的单击的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12838985/

相关文章:

vsto - VSTO 运行时是否随 MS Office 安装一起安装?

outlook - 名称 'application' 在当前上下文中不存在

c# - 使用 Nhibernate.Mapping.Attributes 指定复合键

c# - XNA 2D 世界在 x 轴上的反射

c# - 将对象列表转换为接口(interface)列表

excel - VBA大于函数实际上显示小于答案

c# - Solidworks C# 插件 - 将字符串发送到宏

excel - 根据邮政编码的开头突出显示excel中的行

vba - Excel VBA : how to cast a control got from the UserForm's Controls collection to its native ActiveX type?

c# - 当托管在 VSTO/Outlook 加载项中的 ElementHost 中时,WPF 文本框不允许撤消