簡體   English   中英

VSTO-從excel工作簿上的任務欄捕獲單擊的事件

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

我正在研究VSTO excel 2007工作簿應用程序並尋找跟蹤excel應用程序點擊的事件。

有兩種情況: -

  1. 用戶在任務欄中單擊excel圖標后進入excel。
  2. 用戶按ALT + TAB后進入Excel工作表

在此輸入圖像描述

我努力了

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM