[ 来源:http://www.it55.com | 作者: | 时间:2007-10-31 | 收藏 | 推荐 ] 【大 中 小】
Windows窗口、控件的任何操作,是通过消息事件来完成。在我们自己的程序中,只要能准确地找到相应功能所在的窗口或控件的句柄Handle,发出相应的消息,即可完成相应任务。关键在于两个地方:一是找准句柄,二是找对消息。
推荐分析一个窗体(控件)的句柄或消息的工具:SPY++,这在Visual Studio Tools中有,操作起来很简单。

C#中实现外部程序调用,可以通过封装User32.dll中sendmessage等函数来实现。我已将常用的功能封装成一个类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace MuliCall
{
class WinApi
{
#region 宏定义
public const int WM_KEYDOWN = 0x100;
public const int WM_KEYUP = 0x101;
public const int VK_CONTROL = 0x11;
public const int VK_F5 = 0x74;
public const int KEYEVENTF_KEYUP = 0x2;
public const int VK_MENU = 0x12;
public const int WM_SETTEXT = 0xC;
public const int WM_CLEAR = 0x303;
public const int BN_CLICKED = 0;
public const int WM_LBUTTONDOWN = 0x201;
public const int WM_LBUTTONUP = 0x202;
public const int WM_CLOSE = 0x10;
public const int WM_COMMAND = 0x111;
public const int WM_SYSKEYDOWN = 0x104;
#endregion
public delegate bool EnumChildWindowsProc(IntPtr hwnd, int lParam);
#region WinAPI定义
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendTxtMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
char[] lParam
// int lParam // second message parameter
);
[DllImport("user32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(
int hwnd,
int wMsg,
int wParam,
int lParam
);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern int FindWindowEx(
int hwndParent,
(编辑:IT资讯之家 www.it55.com)