推荐文章

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符
C#监控程序启动和关闭

C#监控程序启动和关闭

C#监控程序启动和关闭using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Threading;namespace ProcessListener{ class Program { static void Main(stri
C#操作USB口的摄像头

C#操作USB口的摄像头

private const uint BM_CLICK = 0xF5; 鼠标点击的消息,对于各种消息的数值,查API手册,也可用VS2010自带的SPY++ [DllImpt("user32.dll", EntryPoint = "SendMessage", SetLastErr = true, Set = Set.Auto)]private static extern int SendMe
C# FTP操作

C# FTP操作

C# FTP操作
C#文件监控对象FileSystemWatcher

C#文件监控对象FileSystemWatcher

使用C#文件监控对象FileSystemWatcher对文件夹下的删除、修改、新增

C# 利用Windows API控制外部的win32 程序

日期:2018-08-15 点击:2210 来源:PB2.CN

利用 Windows API的FindWindow, FindWindowEx, SendMessage, SetForegroundWindow,控制打开“计算器”程序,并模拟点击数字进行计算。


引用Winddows API

using System.Runtime.InteropServices;
[DllImport ( "user32.dll", EntryPoint = "FindWindow", SetLastError = true )]
private static extern IntPtr FindWindow( string lpClassName, string lpWindowName );
[DllImport ( "user32.dll", EntryPoint = "FindWindowEx", SetLastError = true )]
private static extern IntPtr FindWindowEx( IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow );
[DllImport ( "user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto )]
private static extern int SendMessage( IntPtr hwnd, uint wMsg, int wParam, int lParam );
[DllImport ( "user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true )]
private static extern void SetForegroundWindow( IntPtr hwnd );


执行控制操作

private void button1_Click( object sender, EventArgs e )
{
    const uint BM_CLICK = 0xF5; //鼠标点击的消息,对于各种消息的数值,大家还是得去API手册
    IntPtr hwndCalc = FindWindow ( null, "计算器" ); //查找计算器的句柄
    if ( hwndCalc != IntPtr.Zero )
    {
    IntPtr hwndThree = FindWindowEx ( hwndCalc, 0, null, "3" ); //获取按钮3 的句柄
    IntPtr hwndPlus = FindWindowEx ( hwndCalc, 0, null, "+" );  //获取按钮 + 的句柄
    IntPtr hwndTwo = FindWindowEx ( hwndCalc, 0, null, "2" );  //获取按钮2 的句柄
    IntPtr hwndEqual = FindWindowEx ( hwndCalc, 0, null, "=" ); //获取按钮= 的句柄
    SetForegroundWindow ( hwndCalc );    //将计算器设为当前活动窗口
    System.Threading.Thread.Sleep ( 2000 );   //暂停2秒让你看到效果
    SendMessage ( hwndThree, BM_CLICK, 0, 0 );
    System.Threading.Thread.Sleep ( 2000 );   //暂停2秒让你看到效果
    SendMessage ( hwndPlus, BM_CLICK, 0, 0 );
    System.Threading.Thread.Sleep ( 2000 );   //暂停2秒让你看到效果
    SendMessage ( hwndTwo, BM_CLICK, 0, 0 );
    System.Threading.Thread.Sleep ( 2000 );   //暂停2秒让你看到效果
    SendMessage ( hwndEqual, BM_CLICK, 0, 0 );
    System.Threading.Thread.Sleep ( 2000 );
    MessageBox.Show ("你看到结果了吗?");
    }
    else
    {
    MessageBox.Show ("没有启动 [计算器]");
    }
}


这篇文档对您是否有帮助?

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符
C#监控程序启动和关闭

C#监控程序启动和关闭

C#监控程序启动和关闭using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Threading;namespace ProcessListener{ class Program { static void Main(stri
C#操作USB口的摄像头

C#操作USB口的摄像头

private const uint BM_CLICK = 0xF5; 鼠标点击的消息,对于各种消息的数值,查API手册,也可用VS2010自带的SPY++ [DllImpt("user32.dll", EntryPoint = "SendMessage", SetLastErr = true, Set = Set.Auto)]private static extern int SendMe
C# FTP操作

C# FTP操作

C# FTP操作
C#文件监控对象FileSystemWatcher

C#文件监控对象FileSystemWatcher

使用C#文件监控对象FileSystemWatcher对文件夹下的删除、修改、新增