2010-10-25 13 views
0

J'ai créé un bouton de barre d'outils dans OneNote comme Daniel Escapa showed. Habituellement, cela fonctionne, mais OneNote décide parfois de griser le bouton de la barre d'outils, rendant impossible le clic. Je ne peux pas comprendre quel état cause cela. Comment puis-je empêcher cela?OneNote ne cesse de griller mon bouton de plugin

Je suis prudent pour renvoyer true à partir des gestionnaires OnEvent et OnClick, mais peut-être qu'un cas spécial provoque le renvoi false? Voici mon code:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using OneNoteAddin; 
using System.Runtime.InteropServices; 
using Microsoft.Win32; 
using System.IO; 
using OneNote = Microsoft.Office.Interop.OneNote; 

namespace NoteTakerPlugin 
{ 

    [Guid("792d0410-d53c-402d-92c9-5db9ea29f644")] 
    public class NoteTakerButton : IOneNoteAddIn 
    { 
     // for sending messages to window handles 
     private struct COPYDATASTRUCT 
     { 
      public IntPtr dwData; 
      public int cbData; 
      [MarshalAs(UnmanagedType.LPStr)] 
      public string lpData; 
     } 

     // this should be hardcoded in the note taker application 
     private const string NoteTakerAppClassName = "CubicNoteTaker-792d0410-d53c-402d-92c9-5db9ea29f644::EventReceiver"; 

     private const int WM_COPYDATA = 0x4A; 

     [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] 
     static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 

     [DllImport("User32.dll", EntryPoint = "SendMessage")] 
     private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam); 

     IntPtr app; 

     public NoteTakerButton() 
     { 
      tellAppSomething("{ \"appStart\": true }", false); 
     } 

     public bool OnClick([In] String strActivePageID) 
     { 
      // send the click event to the note taker app 
      tellAppSomething("{ \"click\": { \"pageId\": \"" + strActivePageID + "\" }}", true); 
      return true; 
     } 

     public bool OnEvent([In] OneNote.OneNoteAddIn_Event evt, [In] String strParameter) 
     { 
      // send the event to the note taker app 
      tellAppSomething("{ \"event\": { \"id\": " + (int)evt + ", \"param\": \"" + strParameter + "\" }}", false); 
      return true; 
     } 

     private void tellAppSomething(String message, bool startProgram) 
     { 
      // if the process is running 
      refreshAppStatus(); 
      if(app != IntPtr.Zero ) 
      { 
       // send a window message to the process telling it the message 
       sendMessageTo(app, message); 
      } 
      else if(startProgram) 
      { 
       // start the process with a command line telling it the message 
       startApp(message); 
      } 
     } 

     private void refreshAppStatus() 
     { 
      app = FindWindowByCaption(IntPtr.Zero, NoteTakerAppClassName); 
     } 

     private void sendMessageTo(IntPtr hWnd, String msg) 
     { 
      int wParam = 0; 
      int result = 0; 

      if (hWnd != IntPtr.Zero) 
      { 
       byte[] sarr = System.Text.Encoding.Default.GetBytes(msg); 
       int len = sarr.Length; 
       COPYDATASTRUCT cds; 
       cds.dwData = IntPtr.Zero; 
       cds.lpData = msg; 
       cds.cbData = len + 1; 
       result = SendMessage(hWnd, WM_COPYDATA, wParam, ref cds); 
      } 
     } 

     private void startApp(String args) 
     { 
      string exe = getAppExe(); 
      if (File.Exists(exe)) 
      { 
       System.Diagnostics.Process.Start(exe, args); 
      } 
      else 
      { 
       System.Windows.Forms.MessageBox.Show("You need to reinstall the Note Taker application. The installation is corrupted."); 
      } 
     } 

     private string getAppExe() 
     { 
      // use the registry to find where the application is installed 
      RegistryKey noteTakerKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\NoteTaker"); 
      String path = noteTakerKey.GetValue("Path","").ToString(); 
      String exe = noteTakerKey.GetValue("Exe", "").ToString(); 

      if (!Directory.Exists(path)) 
       return ""; 

      String combo = Path.Combine(path, exe); 

      if(File.Exists(combo)) 
       return combo; 
      else // not installed 
       return ""; 
     } 
    } 
} 

Répondre

0

Dans mon onglet Build properties properties, j'avais oublié de cocher "Register for COM interop" en mode Release. Cela a causé des erreurs intermittentes.

0

EDIT: Je me souviens de ce numéro, maintenant! Lorsque vous fermez OneNote, vérifiez si une instance de celui-ci est toujours en cours d'exécution dans taskman, je pense que cela verrouille l'addon dans les instances futures et les grise, assurez-vous que vous n'avez aucun code en cours d'exécution qui causerait l'instance vivant.

+0

Merci pour le conseil. Je revérifierai ceci, mais je suis sûr que j'ai tué toutes les instances de OneNote et de mon application avant de redémarrer OneNote pour trouver qu'il était toujours grisé. Mais je crois que le redémarrage a peut-être été une solution rapide, alors vous pouvez être sur quelque chose. – andrewrk

+0

Je ne me souviens pas exactement ce qui l'a causé pour moi, c'était soit les paramètres de confidentialité ou l'instance ne fermait pas correctement, votre problème peut être totalement différent cependant. Vous pouvez également jeter un coup d'œil sur la mise au rebut de certaines choses comme l'IntPtr qui peut contribuer au blocage de l'application. – kyndigs