用c#控制windows自带计算器实例
来源:互联网 发布:现货分析软件免费 编辑:程序博客网 时间:2024/05/28 03:03
用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);
}
}
}
- 用c#控制windows自带计算器实例
- VC 控制windows自带计算器做加法运算
- c# 编程实现对WINDOWS自带计算器进行操作
- Windows自带的计算器
- C#编写的计算器程序(仿windows自带的计算器)
- C# 调用Windows Mobile自带的控制面板项
- JAVA实现Windows自带的计算器
- windows 自带计算器 calc 的使用
- Windows自带防火墙控制
- 自带计算器
- Windows 系统自带计算器使用说明和技巧
- 个人作品——类windows自带的计算器
- windows自带计算器模式切换的实现研究
- 使用Java实现Windows自带计算器(超简单)
- 安卓模仿Windows自带计算器简易布局
- C#调用微软自带的计算器、记事本等软件
- C#利用Windows自带gdi32.dll实现抓取屏幕功能实例
- C# Windows自带的MediaPlayer用法
- Top 10 time waster games
- C++书单
- 欺骗的艺术 (一) 翻译 凯文·米特尼克著
- 招聘会的听闻感受
- java运行速度比C++慢的几点原因
- 用c#控制windows自带计算器实例
- SQL Server 2000的安装
- Tapestry介绍
- MDA模型转换
- strspn ( )【C语言库函数源代码】
- 未来的世界是,方向比努力重要,能力比知识重要,健康比成绩重要,生活比文凭重要,情商比智商重要!
- 改注册表,在一定时间内只能运行指定程序,知道用组策略管理器,可以实现,不过我想用编程的方法。
- SQLSERVER存储过程入门与提高
- 逛超市遇到问题了还是好心人多