2016-03-15 40 views
2

我已經花了近3天嘗試在網絡上找到,並通過自己的解決方案。發現了很多相關的材料,但沒有什麼符合我的情況。 我已經通過下面的代碼啓動了一個新進程,它是Windows語音識別應用程序,我想以編程方式調用其右鍵單擊菜單中的某個選項。調用右鍵菜單在Windows的C#

我已經測試 PostMessage的,SendMessage函數和sendClick事件,他們沒有工作。

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     [DllImport("User32.dll")] 
     public static extern int SetForegroundWindow(IntPtr point); 

     [DllImport("user32.dll", CharSet = CharSet.Auto)] 
     static extern IntPtr FindWindow(string lpClass, string lpW); 

     [DllImport("user32.dll", CharSet = CharSet.Auto)] 
     static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); 
     [return: MarshalAs(UnmanagedType.Bool)] 
     [DllImport("user32.dll", SetLastError = true)] 
     public static extern void PostMessage(IntPtr hWnd, int msg, int wParam, int lParam); 

     public Form1() 
     { 
      //InitializeComponent(); 

      const int WM_COMMAND = 0x0111; 
      Process proc = new Process(); 

      proc.StartInfo.FileName = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessibility\Speech Recognition.lnk"; 

      proc.Start(); 
      Thread.Sleep(200); 
      IntPtr Window = FindWindow(null, "Speech Recognition"); 

      SetForegroundWindow(Window); 

      SendKeys.SendWait("{Move 200,200}"); 
     } 
    } 
} 

enter image description here

+0

而不是試圖「點擊」是上下文菜單選項,不講話的應用程序有任何公共API來訪問「語音字典」,你嘗試調用它別的辦法嗎?不知道這是否合理 – GER

+0

嘗試自動化GUI時使用官方自動化界面:[UI Automation](https://msdn.microsoft.com/en-us/library/ms747327.aspx)。 – IInspectable

回答

0

假設你想Open the Speech Dicitonary您可以通過

+0

'private void button1_Click(object sender,EventArgs e) { SpSharedRecoContext ssrc = new SpSharedRecoContext(); ISpeechRecognizer ispSpeechReco = ssrc.Recognizer;如果(ispSpeechReco.IsUISupported(「AddRemoveWord」,null)) ispSpeechReco.DisplayUI(this.Handle.ToInt32(),「Additional Training」,「AddRemoveWord」,「」); }' 我使用下面顯示的AddRemove用戶界面,但它與一個COM錯誤崩潰,任何想法? –