.NET 向计算器发送密钥

发布于 2024-08-28 05:01:30 字数 920 浏览 6 评论 0原文

下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼藏柔 2024-09-04 05:01:30

我将告诉您如何弄清楚如何将按键发送到 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.

荒岛晴空 2024-09-04 05:01:30

我想我遇到了问题。时机是这里的问题。只要我将 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.

我不会写诗 2024-09-04 05:01:30

在 Windows 7 上,您必须执行以下操作:

IntPtr calculatorHandle = FindWindow("CalcFrame", "Calculator");

On Windows 7, you have to do this:

IntPtr calculatorHandle = FindWindow("CalcFrame", "Calculator");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文