Witam,
pomimo moich bardzo obszernych poszukiwań nie bylem w stanie znaleźć nic odnośnie performu danych funkcji w zminimalizowanej aplikacji przy użyciu pointera.
Poniższy kod:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public class ClickOnPointTool
{
[DllImport("user32.dll")]
static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);
[DllImport("user32.dll")]
internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
#pragma warning disable 649
internal struct INPUT
{
public UInt32 Type;
public MOUSEKEYBDHARDWAREINPUT Data;
}
[StructLayout(LayoutKind.Explicit)]
internal struct MOUSEKEYBDHARDWAREINPUT
{
[FieldOffset(0)]
public MOUSEINPUT Mouse;
}
internal struct MOUSEINPUT
{
public Int32 X;
public Int32 Y;
public UInt32 MouseData;
public UInt32 Flags;
public UInt32 Time;
public IntPtr ExtraInfo;
}
#pragma warning restore 649
public static void ClickOnPoint(IntPtr wndHandle, Point clientPoint)
{
var oldPos = Cursor.Position;
/// get screen coordinates
ClientToScreen(wndHandle, ref clientPoint);
/// set cursor on coords, and press mouse
Cursor.Position = new Point(clientPoint.X, clientPoint.Y);
var inputMouseDown = new INPUT();
inputMouseDown.Type = 0; /// input type mouse
inputMouseDown.Data.Mouse.Flags = 0x0002; /// left button down
var inputMouseUp = new INPUT();
inputMouseUp.Type = 0; /// input type mouse
inputMouseUp.Data.Mouse.Flags = 0x0004; /// left button up
var inputs = new INPUT[] { inputMouseDown, inputMouseUp };
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
/// return mouse
Cursor.Position = oldPos;
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("URL");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
ClickOnPointTool.ClickOnPoint(this.Handle, new Point(912, 93));
Thread.Sleep(500);
ClickOnPointTool.ClickOnPoint(this.Handle, new Point(777, 600));
}
private void timer2_Tick(object sender, EventArgs e)
{
//IntPtr hwnd = FindWindow(null, "Belas App v1.1");
ClickOnPointTool.ClickOnPoint(this.Handle, new Point(927, 118));
Thread.Sleep(500);
ClickOnPointTool.ClickOnPoint(this.Handle, new Point(777, 600));
}
private void Start_Click(object sender, EventArgs e)
{
this.timer1.Enabled = true;
this.timer2.Enabled = true;
}
private void Stop_Click(object sender, EventArgs e)
{
this.timer1.Enabled = false;
this.timer2.Enabled = false;
}
private void button1_Click_1(object sender, EventArgs e)
{
ClickOnPointTool.ClickOnPoint(this.Handle, new Point(912, 93));
Thread.Sleep(500);
ClickOnPointTool.ClickOnPoint(this.Handle, new Point(777, 600));
ClickOnPointTool.ClickOnPoint(this.Handle, new Point(927, 118));
Thread.Sleep(500);
ClickOnPointTool.ClickOnPoint(this.Handle, new Point(777, 600));
}
}
}
Ten kod świetnie odgrywa swoja role, mianowicie klika lewa myszka w dane koordynaty bez użycia myszki, teraz moje pytanie:
Czy jest możliwość aby dana symulacja była wykonywana tylko na mojej aplikacji, do tego zminimalizowanej, abym mógł wykonywać inne czynności, bez ingerencji w inne programy?
Znalazłem metode FindWindow:
// BM_CLICK to jest np. left button lick
private const int BM_CLICK = 0x00F5;
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
//IntPtr hwnd = FindWindow(null, "APP");
IntPtr hwndChild = IntPtr.Zero;
IntPtr hwnd = IntPtr.Zero;
hwnd = FindWindow(null, "APP");
hwndChild = FindWindowEx(hwnd, IntPtr.Zero, "APP", "Add");
SendMessage(hwndChild, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
Niestety myślę, że dana funkcja to nie wszystko, dlatego zdecydowałem się zapytać na forum.
DZIĘKUJE Z GÓRY ZA POMOC.