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

解决方案 »

  1.   

    不是word插件,是一个窗体程序,可以向word或者txt等可编辑区域插入字符的程序
      

  2.   

    如何取得在word或者txt中选定的文字呢?
      

  3.   

    代码未整理.  
    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);  
                }        }
        }
    }
      

  4.   

    谢谢heatol ,好像可以,我试试.可以的话马上给分.
    另外想问一下,
    你的查找窗体是通过GetProcessesByName("notepad")
    能不能通过我鼠标所在的窗体来实现?比如说,鼠标在写字板里面,这里就不要查找写字板的线程,而直接可以在写字板里面插入字符呢?
      

  5.   

    上面的问题已解决,
    还有一个问题,如何把剪贴板中的数据 发送到word里面?用sendmessage不管用.
    (不要用虚拟ctrl+v的方法)