程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# Console應用mspaint翻開圖象並保留的辦法

C# Console應用mspaint翻開圖象並保留的辦法

編輯:C#入門知識

C# Console應用mspaint翻開圖象並保留的辦法。本站提示廣大學習愛好者:(C# Console應用mspaint翻開圖象並保留的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C# Console應用mspaint翻開圖象並保留的辦法正文


本文實例講述了C# Console應用mspaint翻開圖象並保留的辦法。分享給年夜家供年夜家參考,詳細以下:

挪用繪圖板緊縮圖片

System.Diagnostics.Process process = new System.Diagnostics.Process();
process = System.Diagnostics.Process.Start("mspaint.exe", path);
int processId = process.Id;
AutomationElement element = FindWindowByProcessId(processId);
System.Windows.Forms.SendKeys.SendWait("^s"); //發送 Ctrl + s 鍵
System.Windows.Forms.SendKeys.SendWait("%{F4}"); // 發送 Alt + F4 鍵

代碼

public static AutomationElement FindWindowByProcessId(int processId)
{
  AutomationElement targetWindow = null;
  int count = 0;
  try
  {
    Process p = Process.GetProcessById(processId);
    targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
    return targetWindow;
  }
  catch (Exception ex)
  {
    count++;
    StringBuilder sb = new StringBuilder();
    string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
    if (count > 5)
    {
      throw new InvalidProgramException(message, ex);
    }
    else
    {
      return FindWindowByProcessId(processId);
    }
  }
}

模仿鍵盤輸出

SendKeys.SendWait("{F5}");     //發送F5按鍵
SendKeys.SendWait("^s");    //發送 Ctrl + s 鍵
SendKeys.SendWait("%{F4}");   // 發送 Alt + F4 鍵
//按鍵 代碼 
BACKSPACE {BACKSPACE}, {BS}, 或 {BKSP} 
BREAK {BREAK} 
CAPS LOCK {CAPSLOCK} 
DEL or DELETE {DELETE} 或 {DEL} 
DOWN ARROW {DOWN} 
END {END} 
ENTER {ENTER}或 ~ 
ESC {ESC} 
HELP {HELP} 
HOME {HOME} 
INS or INSERT {INSERT} 或 {INS} 
LEFT ARROW {LEFT} 
NUM LOCK {NUMLOCK} 
PAGE DOWN {PGDN} 
PAGE UP {PGUP} 
PRINT SCREEN {PRTSC} 
RIGHT ARROW {RIGHT} 
SendKeys.SendWait("+{TAB}");
SendKeys.SendWait("%f");//alt+f
SendKeys.SendWait("{Tab}");
SendKeys.SendWait("{Enter}")
//屢次按鍵的代碼
//為了指定反復鍵,應用 {key number} 的情勢。必需在 key 與 number 之間放置一個空格。//例如,{LEFT 42} 意指 42 次按下 LEFT ARROW 鍵;{h 10} 則是指 10 次按下 H 鍵。

Where is the System.Windows.Automation

The UIAutomationClient.dll is located in this folder:

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0

If you can't find in your Add Reference->.Net tab, then you have to use the Browse tab to go to the given path, and add the assembly (Right Click on the References, choose add reference, click browse tab):

完全demo法式代碼以下:

using System;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
namespace UIATest
{
  class Program
  {
    static void Main(string[] args)
    {
      Process process = Process.Start(@"E:\WorkBook\ATP\WpfApp\bin\Debug\WpfApp.exe");
      int processId = process.Id;
      AutomationElement element = FindElementById(processId, "textBox1");
      SendKeys sendkeys = new SendKeys();
      sendkeys.Sendkeys(element, "Sending keys to input data");
      Console.WriteLine(sendkeys.ToString());
      sendkeys.Sendkeys(element, sendkeys.ContextMenu);
      Console.WriteLine(sendkeys.ToString());
      Console.WriteLine("Test finised."); 
    }
    /// <summary>
    /// Get the automation elemention of current form.
    /// </summary>
    /// <param name="processId">Process Id</param>
    /// <returns>Target element</returns>
    public static AutomationElement FindWindowByProcessId(int processId)
    {
      AutomationElement targetWindow = null;
      int count = 0;
      try
      {
        Process p = Process.GetProcessById(processId);
        targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
        return targetWindow;
      }
      catch (Exception ex)
      {
        count++;
        StringBuilder sb = new StringBuilder();
        string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
        if (count > 5)
        {
          throw new InvalidProgramException(message, ex);
        }
        else
        {
          return FindWindowByProcessId(processId);
        }
      }
    }
    /// <summary>
    /// Get the automation element by automation Id.
    /// </summary>
    /// <param name="windowName">Window name</param>
    /// <param name="automationId">Control automation Id</param>
    /// <returns>Automatin element searched by automation Id</returns>
    public static AutomationElement FindElementById(int processId, string automationId)
    {
      AutomationElement aeForm = FindWindowByProcessId(processId);
      AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
      new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
      return tarFindElement;
    }
  }
}

願望本文所述對年夜家C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved