在 c# 中使用 win32 API 操作简单的 windows 计算器? [英] manipulating the simple windows Calculator using win32 API in c#?

查看:41
本文介绍了在 c# 中使用 win32 API 操作简单的 windows 计算器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已尽力查看代码示例和网络上有关如何执行此操作的帖子,但几个月来我一直未能在使用 Windows API 与另一个交互方面取得任何进展已经在运行的程序.我不是最伟大的程序员,其中一些东西超出了我的范围.

Well, I've tried my best to look at code examples and posts all over the web on how to do this, but I haven't been able to make any headway in a few months using windows API to interact with another program that's already running. I'm not the greatest programmer and some of this stuff is beyond me.

我所能做的最多就是找到计算器进程及其句柄,并将其与 SetWindowText 一起使用来更改标题.我真正想学习的方法是让我的程序使用 windows user32(我认为这必须是正确的库)通过实际按下软件计算器上的数字键按钮进行简单计算来输入一些数字.

The most I've been able to do is find the Calculator process and its handle, and use that with SetWindowText to change the title. What I'd really like to learn how to do is make my program use the windows user32 (I think this must be the correct library) to enter some numbers by actually pressing the number key buttons on the software calculator to do a simple calculation.

我对这个程序并没有真正的用处,这只是我试图达到的一个目标,以学习如何在 C# 中特别是初学者级别之后使用 Windows API.如果没有人有这方面的代码,或者即使你有,我也非常感谢您提供一些关于我应该阅读的书籍或网络资源的建议,以了解如何做到这一点.

I don't really have a use for this program, it's just a goal I'm trying to reach to learn how to use the windows API past my very beginner level SPECIFICALLY in C#. If no one has the code for this, or even if you do, I'd most appreciate some suggestions for books or resources on the web I should be reading to learn how to do this.

推荐答案

既然你说你在使用 C#,你应该使用 System.Windows.Automation 命名空间,它的全部目的是允许您通过自动化控制其他程序.

Since you say you're using C# you should be using the System.Windows.Automation namespace, whose entire purpose in life is to allow you to control other programs via automation.

您没有详细说明您想要什么,但这里有一个程序可以在计算器中按下7".

You didn't give details as to what you wanted, but here's a program that pushes "7" in the calculator.

using System.Windows.Automation;

class Program
{
 public static void Main()
 {
  var calcWindow = AutomationElement.RootElement.FindFirst(
   TreeScope.Children,
   new PropertyCondition(AutomationElement.NameProperty, "Calculator"));
  if (calcWindow == null) return;

  var sevenButton = calcWindow.FindFirst(TreeScope.Descendants,
   new PropertyCondition(AutomationElement.NameProperty, "7"));

  var invokePattern = sevenButton.GetCurrentPattern(InvokePattern.Pattern)
                     as InvokePattern;
  invokePattern.Invoke();
 }
}

这篇关于在 c# 中使用 win32 API 操作简单的 windows 计算器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆