Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

manipulating the simple windows Calculator using win32 API in c#?

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.

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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();
 }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...