2012-10-11 3 views
3

VSTO Excel 2007 통합 문서 응용 프로그램 &에서 Excel 작업에 대한 클릭 추적 이벤트를 찾고 있습니다.VSTO- Excel 통합 문서의 작업 표시 줄에서 클릭을 캡처하는 이벤트

  1. 사용자가 작업 표시 줄에서 엑셀 아이콘을 클릭 한 후 엑셀에서 오는 : -

    는이 개 시나리오가 있습니다.

  2. 사용자 내가

    ThisWorkbook_ActivateEvent(); 
    

    this.Application.WindowActivate 
    

    을 시도

enter image description here

ALT + TAB을 누른 후 엑셀 시트에오고 있지만하지 않는 것 일하고있어.

답변

0

이것은 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."); 
      } 
     } 
    } 
} 
+0

감사합니다! 정말 고맙습니다. 3 년이 지났지 만 새로운 VSTO 개발자에게는 도움이 될 것입니다. 불행히도 VSTO에서 더 이상 작동하지 않으므로 테스트 할 수 없습니다. –

+0

그것은 작동합니다. 나는 그것을 시험했다. 대답으로 받아 들일 수 있다면 기쁠 것입니다. – jreichert

관련 문제