用c#控制windows自帶計算器例項
用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# 位運算及例項計算C#
- javascript 計算器程式碼例項JavaScript
- 物件導向的例項應用:圖形計算器物件
- win10 自帶計算器快捷鍵有哪些_windows10計算器快捷鍵彙總Win10Windows
- 由例項計數器引出(C#) (轉)C#
- 原生javascript開發計算器例項JavaScript
- Windows XP 故障恢復控制檯應用例項(轉)Windows
- win10 自帶計算器怎麼輸入_win10自帶計算器的使用教程Win10
- 第二個MFC例項:GPA計算器
- CSS 選擇器 - 帶例項CSS
- css選擇器,帶例項CSS
- javascript帶有毫秒的計時器程式碼例項JavaScript
- Android感測器程式設計帶例項(轉)Android程式設計
- c#計算器C#
- 自動重新啟動oracle例項 for windowsOracleWindows
- 執行caffe自帶的mnist例項教程
- 微控制器C程式設計例項指導pdfC程式程式設計
- Excel函式應用例項:計算保險收益(轉)Excel函式
- win10計算器在哪裡 win10自帶計算器功能怎麼開啟Win10
- iOS計步器例項iOS
- 字串自帶的String的正則例項字串
- Excel函式應用例項:折舊值計算(轉)Excel函式
- Excel函式應用例項:計算客流均衡度(轉)Excel函式
- Jmeter-邏輯控制器If Controller的例項運用JMeterController
- C# BitArray 例項C#
- JVM指令分析例項二(算術運算、常量池、控制結構)JVM
- Excel函式應用例項:計算授課天數(轉)Excel函式
- angular實現購物車自動計算價格程式碼例項Angular
- angularjs實現自動計算商品總價格程式碼例項AngularJS
- jafka安裝配置和啟動一個自帶例項和手寫一個例項
- QTP測試Windows計算器QTWindows
- 機器視覺應用例項視覺
- Excel函式應用例項:計算貸款月償還額(轉)Excel函式
- C#多執行緒程式設計例項C#執行緒程式設計
- C# 2.0 套接字程式設計例項初探程式設計
- C#例項構造器,型別構造器 -筆記型別筆記
- HTML5自帶表單驗證程式碼例項HTML
- 51微控制器競賽設計44例全部帶proteus模擬+程式