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 A
You are entering the fun world of platform invoking (P/Invoke). Your best friend in this world is www.pinvoke.net, which contains c# (and vb.net) signatures for a great number of winapi functions.
For example, SendMessage
:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
and this list of message codes.
You'll notice that all p/invoke methods use the DllImport
attribute, found in the System.Runtime.InteropServices
namespace.
They also have an extern
modifier in the method signature. This means that the code is found outside of the current assembly.
You can then use SendMessage
to send messages to the handle of the calculator.
As Jonathan mentioned in his comment, you should use Spy++ to detect which messages are being sent that result in the actions you want to replicate.
For good measure, here is an article with more introductory information on P/Invoke.
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();
}
}