Add using System.Runtime.InteropServices;
in your Form class, and then the code below within that same class:
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public async Task Clicar() {
// Pedir ao usuário que informe o horário em que o click deva ocorrer
// (exemplificado pelas variáveis hora_click, min_click, seg_click, miles_click)
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff")); // imprime a hora atual.
//Refatorar isso de acordo com sua necessidade e seu Form,
//mas a string no formato desejado é DateTime.Now.ToString("HH:mm:ss:fff")
DateTime objetivo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hora_click, min_click, seg_click, miles_click); // definir uma data em que o evento acontecerá
// você precisa usar o objetivo.CompareTo(DateTime.Now) pra saber se a hora informada pelo usuário é depois do horário atual. Nesse caso o método retorna 1. Se for igual retorna 0 e anterior retorna -1
TimeSpan wait_time = objetivo.Subtract(DateTime.Now); // pegar o tempo de espera para realizar o click
await Task.Delay(wait_time); // recomendo uso de await pois não trava a GUI. É bom evitar métodos async com retorno void, mas nesse caso acho que não tem problema
DoMouseClick(); // faz o click do mouse
}
public void DoMouseClick() {
uint X = (uint)Cursor.Position.X; // você pode colocar as coordenadas X,Y pra qualquer valor que quiser, nesse caso ficou a coordenada atual
uint Y = (uint)Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
Then just call the Click() method the way you prefer and modify it according to your need.
Part of the code was taken from this question
https://stackoverflow.com/questions/2416748/how-to-simulate-mouse-click-in-c
Here the link to the Datetime method.Compareto
https://msdn.microsoft.com/pt-br/library/system.datetime.compareto(v=vs.110). aspx
I have never used either async or await.
public async void Clicar()
because I can’t merge async with void... Windows Forms and not the Console?– Bruno
The context is fine, it is possible to use asynchronous programming in Windows Forms yes, I even tested this code with a Form and it worked. The problem is that the return
void
is not recommended in an asynchronous method and your IDE or compiler should be pointing this out. Try changing the method to:public async Task Clicar()
because, according to the Microsoft, Async methods that don’t contain a Return statement or that contain a Return statement that doesn’t Return an operand usually have a Return type of Task.– Vinicius Victor
Exactly. I was wrong and I thank you for the quick and explanatory answers you offered. I was using Visual Studio 2010 and this release uses a framework not compatible with these commands. Thanks again.
– Bruno