日期:2014-05-19  浏览次数:20759 次

c#的API 实现
请教个问题,我想写个程序,实现的功能是:
打开word或者txt文件,光标位于word或者txt中
点击我写的这个程序上面的按钮,就在word   或者txt上的光标处加一个字符,
请问如何实现?

------解决方案--------------------
你想写一个word的插件吗?
------解决方案--------------------
代码未整理.
namespace SendKeysTest
{
public partial class Form1 : Form
{
[DllImport( "user32.dll ")]
public static extern bool SetWindowPos(IntPtr hWnd,
IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
public IntPtr HWND_TOP = IntPtr.Zero;
public uint SWP_NOMOVE = 2;
public uint SWP_NOSIZE = 1;
public uint SWP_NOACTIVATE = 0x10;
public uint SWP_SHOWWINDOW = 0x40;
public Form1()
{

InitializeComponent();



}

private void button1_Click(object sender, EventArgs e)
{


Process[] vProcesses = Process.GetProcessesByName( "notepad ");

if (vProcesses.Length <= 0) return;
SetWindowPos(vProcesses[0].MainWindowHandle, HWND_TOP, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
this.timer1.Enabled = true;


}

private void timer1_Tick(object sender, EventArgs e)
{
// string[] key = this.textBox1.Text.Split( ', ');
string[] key = new string[] { "1 ", "space ", "2 "};
SendKeys(key);
}
private void SendKeys(string [] key)
{
for (int i = 0; i < key.Length; i++)
{
System.Windows.Forms.SendKeys.Send(key[i]);
System.Threading.Thread.Sleep(2000);
}

}
}
}
------解决方案--------------------
up