VSTO - событие для захвата щелчка с панели задач в книге Excel

Я работаю над приложением рабочей книги VSTO excel 2007 и ищу событие, которое отслеживает щелчок по приложению excel.

Есть 2 сценария: -

  1. Пользователь переходит на Excel после нажатия значка Excel на панели задач.
  2. Пользователь переходит на лист Excel после нажатия клавиш ALT + TAB

введите описание изображения здесь

я пытался

 ThisWorkbook_ActivateEvent();

и

this.Application.WindowActivate

но они, похоже, не работают.


person Sangram Nandkhile    schedule 11.10.2012    source источник


Ответы (1)


это полное решение VSTO, которое должно работать, хотя и не очень хорошо, потому что использует таймер. Я протестировал это с обоими вашими сценариями.

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.");
            }
        }
    }
}
person jreichert    schedule 12.05.2016
comment
Спасибо ! Действительно ценю это. Прошло уже 3 года :), но должно пригодиться новым разработчикам VSTO. К сожалению, я не смогу протестировать его, так как больше не работаю над VSTO. - person Sangram Nandkhile; 12.05.2016
comment
Оно работает. Я это проверил. Буду рад, если вы примете это как ответ. - person jreichert; 16.05.2016