Send mouse click

I am trying to simulate a mouse click without using sendmessage mouse anyway it does not work, but there is no error. here is my new code

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Input; using System.Diagnostics; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private const int BM_CLICK = 0x00F5; [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [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); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { IntPtr hwndChild = IntPtr.Zero; IntPtr hwnd = IntPtr.Zero; hwnd = FindWindow(null, "MSPaintApp"); hwndChild = FindWindowEx(hwnd, IntPtr.Zero, "Afx:00007FF765740000:8", null); SendMessage(hwndChild, BM_CLICK, IntPtr.Zero, IntPtr.Zero); } } } 

Can you show me how to set up XY coordination so that it clicks on the child window. I saw a lot of messages on how to do this, but I can’t understand, understand, and the system said that do not ask a question in the question of other people :(

0
source share
2 answers

For me, the problem is that the MSPaintApp window needs to be searched with FindWindow("MSPaintApp", null) , because MSPaintApp is the name of the class, not the window title.

Then Afx:00007FF765740000:8 not a child of MSPaintApp , but is a child of MSPaintView , which is a child of MSPaintApp .

Also you do not need to "simulate" BM_CLICK, but you must simulate WM_LBUTTONDOWN and then WM_LBUTTONUP

Please consider this example which seems to work (on Windows 7)

 class Program { private const uint WM_LBUTTONDOWN = 0x201; private const uint WM_LBUTTONUP = 0x202; private const uint MK_LBUTTON = 0x0001; public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr parameter); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, 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); [DllImport("user32.dll", SetLastError = true)] public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam); static IntPtr childWindow; static void Main(string[] args) { IntPtr hwndMain = FindWindow("MSPaintApp", null); IntPtr hwndView = FindWindowEx(hwndMain, IntPtr.Zero, "MSPaintView", null); // Getting the child windows of MSPaintView because it seems that the class name of the child isn't constant EnumChildWindows(hwndView, new EnumWindowsProc(EnumWindow), IntPtr.Zero); // Simulate press of left mouse button on coordinates 10, 10 SendMessage(childWindow, WM_LBUTTONDOWN, new IntPtr(MK_LBUTTON), CreateLParam(10, 10)); // Simulate release of left mouse button on coordinates 100, 100 SendMessage(childWindow, WM_LBUTTONUP, new IntPtr(MK_LBUTTON), CreateLParam(100, 100)); } static bool EnumWindow(IntPtr handle, IntPtr pointer) { // Get the first child because it seems that MSPaintView has only this child childWindow = handle; // Stop enumerating the windows return false; } private static IntPtr CreateLParam(int LoWord, int HiWord) { return (IntPtr)((HiWord << 16) | (LoWord & 0xffff)); } } 
0
source

I come back many years ago, but I am sure that mouse event window messages will not be delivered when applications are minimized. I am sure that the minimized window is handled differently.

Aside, does this code work when the window is not minimized? You might want to use Spy ++ to look at the window structure a little more, because I think you will need to get hWnd of what you are trying to click and send a message there. A lot depends on how the application was written and how the user interface is created.

+1
source

Source: https://habr.com/ru/post/1260711/


All Articles