2010-09-25 64 views
3

嘿,我使用C#,试图键命令发送到Windows Media Center的Windows 7中使用keySend的Windows Media Center

目前,我公司可以派键,如4和看到的数字上的Windows Media 4次出现中央。

问题是像Ctrl + p(暂停电影)的任何组合键似乎对媒体中心没有任何影响。

任何帮助将不胜感激。这是我的代码片段。

// Get a handle to an application window. 
    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] 
    public static extern IntPtr FindWindow(string lpClassName, 
    string lpWindowName); 

    // Activate an application window. 
    [DllImport("USER32.DLL")] 
    public static extern bool SetForegroundWindow(IntPtr hWnd); 


    String HandleClass = "eHome Render Window"; 
    String HandleWindow = "Windows Media Center"; 

    private bool SendKeyCommand() 
    { 
     bool success = true; 
     IntPtr PrgHandle = FindWindow(HandleClass, HandleWindow); 
     if (PrgHandle == IntPtr.Zero) 
     { 
      MessageBox.Show(HandleWindow + " is not running"); 
      return false; 
     } 
     SetForegroundWindow(PrgHandle); 
     SendKeys.SendWait("^p"); 
     return success; 
    } 
+0

我知道这是无关紧要的,但我已经注意到了的extern方法,它代表着什么。 – Tarik 2010-09-25 06:14:51

+0

extern修饰符表示该方法在C#代码之外实现。 http://msdn.microsoft.com/en-us/library/e59b22c5%28VS.80%29.aspx – Scott 2010-09-25 19:17:00

回答

0

我居然能终于发现本网站上工作的解决方案:

http://michbex.com/wordpress/?p=3

最后我用他的VK级和远程发送类的方法来解决这个问题。 Windows媒体中心必须具有较低级别的关键挂钩,并且必须实施一个keyup/keydown发送解决方案来利用挂钩。

我终于可以暂停电影了!我将清理代码并稍后发布。

1

我实际上无法在VK Class中实现任何有用的功能。 MediaCenter不会回应这个keydown/keyup的东西。

相反,我用这个方法把媒体中心前:

[DllImport("user32.dll")] 
static extern bool SetForegroundWindow(IntPtr hWnd); 

public static void activateMediaCenterForm() 
{ 
    System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("ehshell"); 
    if (p.Length > 0) //found 
    { 
     SetForegroundWindow(p[0].MainWindowHandle); 
    } 
    //else not Found -> Do nothing. 
} 

之后,应该的SendKeys工作。我只是把它包裹在try/catch上。

private void SendKey(string key) 
{ 
    activateMediaCenterForm(); 
    try 
    { 
     SendKeys.SendWait(key); 
    } 
    catch (Exception e) 
    { 
     //Handle exception, if needed. 
    } 
} 

现在SendKey("{ENTER}");以及SendKey("{RIGHT}");和所有其他键只是正常的Windows 7

+0

非常酷,对不起,你无法让VK课程工作。在过去的两年里,Windows媒体播放器的更新可能会打破VK课程。 – Scott 2013-02-27 01:17:45

相关问题