简体   繁体   English

VSTO-从excel工作簿上的任务栏捕获单击的事件

[英]VSTO- Event to capture a click from taskbar on the excel workbook

I am working on the VSTO excel 2007 workbook application & looking for an event which tracks the click on excel application. 我正在研究VSTO excel 2007工作簿应用程序并寻找跟踪excel应用程序点击的事件。

There are 2 scenarios:- 有两种情况: -

  1. User coming on the excel after clicking excel icon from the task bar. 用户在任务栏中单击excel图标后进入excel。
  2. User coming on the excel sheet after Pressing ALT+TAB 用户按ALT + TAB后进入Excel工作表

在此输入图像描述

I have tried 我努力了

 ThisWorkbook_ActivateEvent();

and

this.Application.WindowActivate

but they doesn't seem to be working. 但他们似乎没有工作。

this is a complete VSTO solution which should work, although it's not very nice, because it's using a timer. 这是一个完整的VSTO解决方案应该可以工作,虽然它不是很好,因为它使用的是计时器。 I've tested it with both of your scenarios. 我已经用你的两个场景进行了测试。

Jörg 约尔格


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.");
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM