用c#控制windows自带计算器实例

news/2024/7/5 18:32:22

用c#控制windows自带计算器实例
代码出自http://www.codeproject.com/csharp/WindowsAPIsFromCS.asp
我只是将英文“计算器”标题改为了中文的,本来目的是为了将工作中常扫描程序加三个快捷键,因为家里没有扫描仪,以至装了ocr工具,还是不能打开扫描介面。只得将此文先发到博客上,一是方便大家学习,二是可以容易找到,在公司再作这未完成的东东。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace TestWinAPI
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class MainForm : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button btnGetWindows;
  private const int WM_CLOSE = 16;
  private const int BN_CLICKED = 245;
  private System.Windows.Forms.Button btnCloseCalc;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;
  /// <summary>
  /// The FindWindow API
  /// </summary>
  /// <param name="lpClassName">the class name for the window to search for</param>
  /// <param name="lpWindowName">the name of the window to search for</param>
  /// <returns></returns>
  [DllImport("User32.dll")]
  public static extern Int32 FindWindow(String lpClassName,String lpWindowName);

  /// <summary>
  /// The SendMessage API
  /// </summary>
  /// <param name="hWnd">handle to the required window</param>
  /// <param name="msg">the system/Custom message to send</param>
  /// <param name="wParam">first message parameter</param>
  /// <param name="lParam">second message parameter</param>
  /// <returns></returns>
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
  
  /// <summary>
  /// The FindWindowEx API
  /// </summary>
  /// <param name="parentHandle">a handle to the parent window </param>
  /// <param name="childAfter">a handle to the child window to start search after</param>
  /// <param name="className">the class name for the window to search for</param>
  /// <param name="windowTitle">the name of the window to search for</param>
  /// <returns></returns>
  [DllImport("user32.dll", SetLastError = true)]
  public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  string  windowTitle);

  public MainForm()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.btnGetWindows = new System.Windows.Forms.Button();
   this.btnCloseCalc = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // btnGetWindows
   //
   this.btnGetWindows.Location = new System.Drawing.Point(16, 24);
   this.btnGetWindows.Name = "btnGetWindows";
   this.btnGetWindows.Size = new System.Drawing.Size(120, 23);
   this.btnGetWindows.TabIndex = 0;
   this.btnGetWindows.Text = "Use Calculator";
   this.btnGetWindows.Click += new System.EventHandler(this.btnGetWindows_Click);
   //
   // btnCloseCalc
   //
   this.btnCloseCalc.Location = new System.Drawing.Point(16, 72);
   this.btnCloseCalc.Name = "btnCloseCalc";
   this.btnCloseCalc.Size = new System.Drawing.Size(120, 23);
   this.btnCloseCalc.TabIndex = 2;
   this.btnCloseCalc.Text = "Close the Calculator";
   this.btnCloseCalc.Click += new System.EventHandler(this.btnCloseCalc_Click);
   //
   // MainForm
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(234, 119);
   this.Controls.Add(this.btnCloseCalc);
   this.Controls.Add(this.btnGetWindows);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
   this.MaximizeBox = false;
   this.Name = "MainForm";
   this.Text = "Test Windows API";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new MainForm());
  }

  private void btnGetWindows_Click(object sender, System.EventArgs e)
  {
   int hwnd=0;
   IntPtr hwndChild=IntPtr.Zero;

   //Get a handle for the Calculator Application main window
   hwnd=FindWindow(null,"计算器");
   if(hwnd == 0)
   {
    if(MessageBox.Show("Couldn't find the calculator application. Do you want to start it?","TestWinAPI",MessageBoxButtons.YesNo)== DialogResult.Yes)
    {
     System.Diagnostics.Process.Start("Calc");
    }
   }
   else
   {
    
    //Get a handle for the "1" button
    hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","2");
    
    //send BN_CLICKED message
    SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

    //Get a handle for the "+" button
    hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","+");
    
    //send BN_CLICKED message
    SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

    //Get a handle for the "2" button
    hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","3");
    
    //send BN_CLICKED message
    SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

    //Get a handle for the "=" button
    hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","=");
    
    //send BN_CLICKED message
    SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);

   }//花纯春改写,http://ike.126.com

 

  }

  private void btnCloseCalc_Click(object sender, System.EventArgs e)
  {
   int hwnd=0;

   //Get a handle for the Calculator Application main window
   hwnd=FindWindow(null,"计算器");

   //send WM_CLOSE system message
   if(hwnd!=0)
    SendMessage(hwnd,WM_CLOSE,0,IntPtr.Zero);
  }
 }
}
 


http://www.niftyadmin.cn/n/2662199.html

相关文章

Java jsp sevlet中文乱码问题解决方案

如果您喜欢这些文章&#xff0c;欢迎点击此处订阅本Blog title"RSS 2.0" type"application/rssxml" href"http://feed.feedsky.com/softwave" rel"alternate"> <script type"text/javascript"></script> <…

Python 2 大限还有 112 天

(给技术最前线加星标&#xff0c;每天看技术热点)转自&#xff1a;Python开发者近日&#xff0c;Python 官网再次发文&#xff0c;提醒将于 2020 年 1 月 1 日正式对 Python 2 停止支持&#xff0c;未升级到 Python 3 的童鞋&#xff0c;应当尽快做好迁移。官网通告&#xff0…

改注册表,在一定时间内只能运行指定程序,知道用组策略管理器,可以实现,不过我想用编程的方法。

我能作的就到步了&#xff0c;如果在重启后还不能达到预期效果那我只得另辟蹊径了。以下是我在论坛上求助的原文&#xff1a;改注册表&#xff0c;在一定时间内只能运行指定程序&#xff0c;知道用组策略管理器&#xff0c;可以实现&#xff0c;不过我想用编程的方法。以下是我…

网页前台配色秘诀-黄金分割配色法

如果您喜欢这些文章&#xff0c;欢迎点击此处订阅本Blog title"RSS 2.0" type"application/rssxml" href"http://feed.feedsky.com/softwave" rel"alternate"> <script type"text/javascript"></script> <…

又一例:网红送餐无人车冒充AI,真人海外远程操控

(给技术最前线加星标&#xff0c;每天看技术热点)转自&#xff1a;快科技美国网红外卖机器人Kiwibot实际由远在南美哥伦比亚的真人远程操控&#xff0c;每人时薪不到2美元&#xff0c;最多控制三台。2017年成立的Kiwi Campus公司累计获得200万美元融资&#xff0c;约人民币1414…

11.26潜龙每周综述:轻指数重个股,个股炒作黄金时节来临!

我花钱买来的&#xff0c;当然要公开共享给大家。至于正确与否&#xff0c;且待验证。 11.26潜龙每周综述&#xff1a;轻指数重个股&#xff0c;个股炒作黄金时节来临&#xff01;以下内容需要支付 20 个金币方可查看&#xff0c;您已经购买。 http://ike.126.com 到本…

Java对文件压缩/加密/解密/解压缩的例子,DES/RSA

如果您喜欢这些文章&#xff0c;欢迎点击此处订阅本Blog title"RSS 2.0" type"application/rssxml" href"http://feed.feedsky.com/softwave" rel"alternate" /> <script type"text/javascript"></script> &…

新工具完成了!!!上班时间只能用指定的部分程序了。唉,有时太过火了~~

我的工具终于完成了&#xff01;&#xff01;&#xff01;以下是全部代码&#xff0c;在vs2005xp环境下运行通过。以前我发贴问的问题&#xff0c;也解决了&#xff0c;感谢网友们的帮助&#xff0c;特别是raozhiven(朗屹) 的提示&#xff0c;感谢&#xff01;以前我总是认为代…