【发布时间】:2010-09-25 05:38:37
【问题描述】:
嘿,我正在使用 C# 尝试在 Windows 7 中向 Windows Media Center 发送按键命令。
目前我可以发送像 4 这样的键,然后在 windows 媒体中心看到数字 4。
问题是任何组合键,如 Ctrl+p(暂停电影)似乎对媒体中心没有任何影响。
任何帮助将不胜感激。这是我的代码 sn-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;
}
【问题讨论】:
-
我知道这无关紧要,但我注意到 extern 方法及其代表什么。
-
extern修饰符表示该方法在C#代码之外实现。 msdn.microsoft.com/en-us/library/e59b22c5%28VS.80%29.aspx