C# Console利用mspaint打开图像并保存的方法
本文实例讲述了C#Console利用mspaint打开图像并保存的方法。分享给大家供大家参考,具体如下:
调用画图板压缩图片
System.Diagnostics.Processprocess=newSystem.Diagnostics.Process();
process=System.Diagnostics.Process.Start("mspaint.exe",path);
intprocessId=process.Id;
AutomationElementelement=FindWindowByProcessId(processId);
System.Windows.Forms.SendKeys.SendWait("^s");//发送Ctrl+s键
System.Windows.Forms.SendKeys.SendWait("%{F4}");//发送Alt+F4键
代码
publicstaticAutomationElementFindWindowByProcessId(intprocessId)
{
AutomationElementtargetWindow=null;
intcount=0;
try
{
Processp=Process.GetProcessById(processId);
targetWindow=AutomationElement.FromHandle(p.MainWindowHandle);
returntargetWindow;
}
catch(Exceptionex)
{
count++;
StringBuildersb=newStringBuilder();
stringmessage=sb.AppendLine(string.Format("Targetwindowisnotexisting.try#{0}",count)).ToString();
if(count>5)
{
thrownewInvalidProgramException(message,ex);
}
else
{
returnFindWindowByProcessId(processId);
}
}
}
模拟键盘输入
SendKeys.SendWait("{F5}");//发送F5按键
SendKeys.SendWait("^s");//发送Ctrl+s键
SendKeys.SendWait("%{F4}");//发送Alt+F4键
//按键代码
BACKSPACE{BACKSPACE},{BS},或{BKSP}
BREAK{BREAK}
CAPSLOCK{CAPSLOCK}
DELorDELETE{DELETE}或{DEL}
DOWNARROW{DOWN}
END{END}
ENTER{ENTER}或~
ESC{ESC}
HELP{HELP}
HOME{HOME}
INSorINSERT{INSERT}或{INS}
LEFTARROW{LEFT}
NUMLOCK{NUMLOCK}
PAGEDOWN{PGDN}
PAGEUP{PGUP}
PRINTSCREEN{PRTSC}
RIGHTARROW{RIGHT}
SendKeys.SendWait("+{TAB}");
SendKeys.SendWait("%f");//alt+f
SendKeys.SendWait("{Tab}");
SendKeys.SendWait("{Enter}")
//多次按键的代码
//为了指定重复键,使用{keynumber}的形式。必须在key与number之间放置一个空格。//例如,{LEFT42}意指42次按下LEFTARROW键;{h10}则是指10次按下H键。
WhereistheSystem.Windows.Automation
TheUIAutomationClient.dllislocatedinthisfolder:
C:\ProgramFiles\ReferenceAssemblies\Microsoft\Framework\v3.0
Ifyoucan'tfindinyourAddReference->.Nettab,thenyouhavetousetheBrowsetabtogotothegivenpath,andaddtheassembly(RightClickontheReferences,chooseaddreference,clickbrowsetab):
完整demo程序代码如下:
usingSystem;
usingSystem.Text;
usingSystem.Diagnostics;
usingSystem.Threading;
usingSystem.Windows.Automation;
usingSystem.Runtime.InteropServices;
usingSystem.Windows;
usingSystem.Windows.Forms;
namespaceUIATest
{
classProgram
{
staticvoidMain(string[]args)
{
Processprocess=Process.Start(@"E:\WorkBook\ATP\WpfApp\bin\Debug\WpfApp.exe");
intprocessId=process.Id;
AutomationElementelement=FindElementById(processId,"textBox1");
SendKeyssendkeys=newSendKeys();
sendkeys.Sendkeys(element,"Sendingkeystoinputdata");
Console.WriteLine(sendkeys.ToString());
sendkeys.Sendkeys(element,sendkeys.ContextMenu);
Console.WriteLine(sendkeys.ToString());
Console.WriteLine("Testfinised.");
}
///<summary>
///Gettheautomationelementionofcurrentform.
///</summary>
///<paramname="processId">ProcessId</param>
///<returns>Targetelement</returns>
publicstaticAutomationElementFindWindowByProcessId(intprocessId)
{
AutomationElementtargetWindow=null;
intcount=0;
try
{
Processp=Process.GetProcessById(processId);
targetWindow=AutomationElement.FromHandle(p.MainWindowHandle);
returntargetWindow;
}
catch(Exceptionex)
{
count++;
StringBuildersb=newStringBuilder();
stringmessage=sb.AppendLine(string.Format("Targetwindowisnotexisting.try#{0}",count)).ToString();
if(count>5)
{
thrownewInvalidProgramException(message,ex);
}
else
{
returnFindWindowByProcessId(processId);
}
}
}
///<summary>
///GettheautomationelementbyautomationId.
///</summary>
///<paramname="windowName">Windowname</param>
///<paramname="automationId">ControlautomationId</param>
///<returns>AutomatinelementsearchedbyautomationId</returns>
publicstaticAutomationElementFindElementById(intprocessId,stringautomationId)
{
AutomationElementaeForm=FindWindowByProcessId(processId);
AutomationElementtarFindElement=aeForm.FindFirst(TreeScope.Descendants,
newPropertyCondition(AutomationElement.AutomationIdProperty,automationId));
returntarFindElement;
}
}
}
希望本文所述对大家C#程序设计有所帮助。