Blue Theme Orange Theme Green Theme Red Theme
 
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win $500 Cash
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id:
Password:  
Forgot Password | Forgot UserName
   Home » COM Interoperability » Changing variables in other applications
       
Author Reply
Kent Caspersen
posted 1 posts
since Jul 25, 2006 
from

 Changing variables in other applications
  Posted on: 10/8/2008 6:50:58 AM       

Hi, i don't know if this is possible.

I want to make a C# applications, which when is run, changes the selected colour in the MsPaint application (which must be running before i run my own application)

Is it possible to interact with other applications and chaning their variables?

www.tidstyven.dk
Alan
posted  1600 posts
since  Jun 02, 2007 
from 

 Re: Changing variables in other applications
  Posted on: 10/8/2008 11:43:39 AM       

It's not too difficult to interact with other running applications. For example, the following C# program will  check to see if MSPaint is already running and, if it is, will  force input focus onto it. If MSPaint isn't running, it will launch it and wait until it's ready to receive input.

It will then send it the keystrokes Alt+FO to open a window so that a file can be selected for viewing or editing:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

class Test
{

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

static void Main()
{
Process[] procs = Process.GetProcessesByName("mspaint"); // don't use .exe extension here
if (procs.Length == 0)
{
Console.WriteLine("MSPaint was not currently running but an instance has been started");
Process proc = new Process();
proc.StartInfo.FileName = "mspaint.exe";
proc.Start();
proc.WaitForInputIdle();
}
else
{
IntPtr hWnd = procs[0].MainWindowHandle;
SetForegroundWindow(hWnd);
}

SendKeys.SendWait("%FO");
}
}

However, MSPaint isn't really designed for automation and AFAIK there are no keyboard shortcuts to select a color.

You can send mouse commands to it using either the mouse_event or SendInput() API functions but it's not going to be easy to get the mouse coordinates right to select whichever color you want. However, if you want to try, here are some links and sample code for P/Invoking these functions from C#. The mouse_event function is deprecated in Win32 programming but is far easier to use than its replacement, SendInput:

http://www.pinvoke.net/default.aspx/user32/mouse_event.html 

http://www.pinvoke.net/default.aspx/user32/SendInput.html

       
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 3.2009.8.27
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved