.NET 向计算器发送密钥
下面的 sendkeys 代码适用于记事本,但不适用于计算器。问题是什么? (与我在此处发送的内容相比,这是另一个问题来自.NET程序的Sendkeys问题)
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("User32")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
IntPtr calculatorHandle = FindWindow("SciCalc", "Calculator");
//IntPtr calculatorHandle = FindWindow("Notepad", "Untitled - Notepad");
if (calculatorHandle == IntPtr.Zero)
{
MessageBox.Show("Calculator is not running.");
return;
}
SetForegroundWindow(calculatorHandle);
System.Threading.Thread.Sleep(1000);
SendKeys.SendWait("111*11=");
//SendKeys.SendWait("{ENTER}");
//cnt++;
SendKeys.Flush();
}
The sendkeys code below works well for Notepad but it doesn't work for Calculator. What is the problem? (It's another problem compared to what I sent here Sendkeys problem from .NET program)
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("User32")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
IntPtr calculatorHandle = FindWindow("SciCalc", "Calculator");
//IntPtr calculatorHandle = FindWindow("Notepad", "Untitled - Notepad");
if (calculatorHandle == IntPtr.Zero)
{
MessageBox.Show("Calculator is not running.");
return;
}
SetForegroundWindow(calculatorHandle);
System.Threading.Thread.Sleep(1000);
SendKeys.SendWait("111*11=");
//SendKeys.SendWait("{ENTER}");
//cnt++;
SendKeys.Flush();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将告诉您如何弄清楚如何将按键发送到 calc.exe。
在使用 calc.exe 窗口进程时,请使用间谍++ 来监视该进程上的消息。为此,请进入spy++并单击日志消息工具栏按钮。将光标拖到 calc.exe 窗口上。我给出的说明适用于 VS2008,对于其他 VS 版本中包含的 Spy++ 可能略有不同。但相同的功能始终可用。
当您输入文本时,您将准确地看到发送的消息。你也需要这样做。
使用 Win32 API SendMessage、LPARAM 和 WPARAM你找到的窗把手。
I'll tell you how you can figure out how to send keystorkes to calc.exe.
Use spy++ to monitor the messages on the calc.exe window process as you're using it. To do this go into spy++ and click on the log messages toolbar button. Drag the cursor onto the calc.exe window. The instructions I gave are for VS2008, they may differ slightly for the Spy++ included in other VS versions. But the same functionality has always been available.
You will see exactly what messages are sent as you are entering text. You need to do the same.
Use the Win32 API SendMessage, LPARAM and WPARAM to your found window handle.
我想我遇到了问题。时机是这里的问题。只要我将 sleep() 黑白发送命令,calc.exe 就会正确执行。但这不是一个好的解决方案。
I guess I got the problem. Timing is the problem here. As long as I put sleep() b/w sending command, the calc.exe get it right. It's not a good solution though.
在 Windows 7 上,您必须执行以下操作:
On Windows 7, you have to do this: