Well,
I am not sure if someone already asked this qusiton before. I was trying to look arround but noting came up. (if there is, please show me and close this. I am very sorry!)
For a few days now I am looking for a way that when I click on a button in my windows form in C# it will copy paste something to somewhere else.
The best way to expline this:
Lets say I got a Ms Word open, and I want that when I will click on a button in my windows form, after 5 seconds, it will write something in my word office. Of course I will open the Ms Word by my self.
Another thing: is how to make your mouse click on hes key?
edit: When i use this code --
int forhow = int.Parse(textBox1.Text);
for(int i = 0;i <forhow; i++)
{
i++;
SendKeys.Send("ספאמר על ידי פריזו - ספאמר על גירסא ראשונה");
//ספאמר על ידי פThread.Sleep(1200);
开发者_StackOverflow中文版//Thread.Sleep(5000);
SendKeys.Send("{ENTER}");
}
well, its should do it only 1 time. is i write 1 in the text box. but, it is doing it about 50 times. and the stop. any one knows why? + . if you lick on the button, the program stops to work until she compltite all the "Send:".
I couldn't find a way to force a mouse click, but you cam mimic the keyboard using the SendKeys class. All code that is not in between "//{" and "//}" was generated by visual studio. Hope this helps!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//{
using System.Diagnostics;
//}
namespace ClickToWord
{
public partial class Form1 : Form
{
//{
Process imsWord = new Process();
Timer tempTime = new Timer();
int counter = 0;
//}
public Form1()
{
InitializeComponent();
//{
imsWord.StartInfo.FileName = @"";
//Inside the "" put the path to the file/application. No need to escape it, because of the "@"
tempTime.Interval = 1000;
//The interval in miliseconds
tempTime.Tick += new EventHandler(tempTime_Tick);
//}
}
void tempTime_Tick(object sender, EventArgs e)
{
//{
char send = 'a';
send += (char)(counter % 26);
SendKeys.Send(send.ToString());
counter++;
//An example of looping through the alphabet. Send any string via SendKeys, and it will act as if the keyboard ent it.
//This mimics keyboard strokes, and requires the document to have focus. That is why it is not the ideal way to do this.
//To programmatically communicate with Word, use the Microsoft Word Object Model library.
//tempTime.Enabled = false;
//}
}
private void button1_Click(object sender, EventArgs e)
{
//{
imsWord.Start();
//Starts the proccess
tempTime.Enabled = true;
//Starts the timer
//}
}
}
}
精彩评论