Best WinAppDriver code snippet using WinAppDriverUIRecorder.NativeMethods.PostMessage
MouseKeyboardEventHandler.cs
Source:MouseKeyboardEventHandler.cs
...136 nHoverCount = 0;137 if (mouseState == MouseState.LeftMouseUp)138 {139 XmlNodePathRecorder.SetUiToRootXmlNodes(ref s_strXmlNodes, true);140 NativeMethods.PostMessage(MainWindow.windowHandle, (uint)UiTaskName.Inspect, 0, 0);141 }142 }143 }144 }145 s_eventRecordNow.Reset();146 }147 }148 }149 public static void EnsureXPathReady(int nWaitTime = 1000)150 {151 // Get xpath, if it is empty, without waiting for timer to fire152 if (string.IsNullOrEmpty(s_strXmlNodes))153 {154 s_eventRecordNow.Set();155 while (s_eventRecordNow.WaitOne(0) && nWaitTime > 0)156 {157 System.Threading.Thread.Sleep(10);158 nWaitTime -= 10;159 }160 }161 }162 public static void MouseMove(int left, int top)163 {164 ResetRecordTimer();165 int dist = Math.Abs(left - ptUiWalking.X) + Math.Abs(top - ptUiWalking.Y);166 int moveDelta = Math.Abs(left - ptCursorMove.X) + Math.Abs(top - ptCursorMove.Y);167 ptCursorMove.X = left;168 ptCursorMove.Y = top;169 if (mouseState == MouseState.LeftMouseUp)170 {171 if (dist > nMinDist)172 {173 if ((Environment.TickCount - s_keyboardInputTick) > 2000)174 {175 PublishKeyboardInput();176 }177 }178 if (moveDelta < 2 && string.IsNullOrEmpty(s_strXmlNodes) == false)179 {180 nHoverCount++;181 }182 if (nHoverCount > nMinDist)183 {184 nHoverCount = 0;185 XmlNodePathRecorder.SetUiToRootXmlNodes(ref s_strXmlNodes, false);186 NativeMethods.PostMessage(MainWindow.windowHandle, (uint)UiTaskName.MouseHover, 0, 0);187 }188 }189 else if (mouseState == MouseState.LeftMouseDown) // drag start - down and up are at different points190 {191 if (dist > nMinDist)192 {193 mouseState = MouseState.LeftMouseDrag;194 PublishKeyboardInput();195 if (s_strXmlNodes != null)196 {197 lock (s_lockUiPath)198 {199 XmlNodePathRecorder.SetUiToRootXmlNodes(ref s_strXmlNodes);200 NativeMethods.PostMessage(MainWindow.windowHandle, (uint)UiTaskName.Drag, 0, 0);201 }202 }203 ptCursorDown.X = left;204 ptCursorDown.Y = top;205 }206 }207 else if (mouseState == MouseState.LeftMouseDrag)208 {209 }210 else211 {212 //log error213 }214 }215 public static void MouseWheel(int left, int top, short delta)216 {217 lock (MouseKeyboardEventHandler.s_lockUiPath)218 {219 if (s_strXmlNodes != null)220 {221 XmlNodePathRecorder.SetUiToRootXmlNodes(ref s_strXmlNodes);222 }223 }224 NativeMethods.PostMessage(MainWindow.windowHandle, (uint)UiTaskName.MouseWheel, (uint)(ConstVariables.DragDeltaOffset + delta), 0);225 ResetRecordTimer();226 }227 public static void LeftMouseDown(int left, int top)228 {229 ptCursorDown.X = left;230 ptCursorDown.Y = top;231 if (mouseState == MouseState.LeftMouseUp)232 {233 mouseState = MouseState.LeftMouseDown;234 }235 else if (mouseState == MouseState.LeftMouseDown)236 {237 //should not get here238 }239 else if (mouseState == MouseState.LeftMouseDrag)240 {241 //should not get here242 }243 else244 {245 //log error246 }247 ResetRecordTimer();248 }249 public static void LeftMouseUp(int left, int top)250 {251 ptCursorUp.X = left;252 ptCursorUp.Y = top;253 int dist = Math.Abs(left - ptCursorDown.X) + Math.Abs(top - ptCursorDown.Y);254 if (dist <= nMinDist)255 {256 //Check if it is double click257 if ((Environment.TickCount - tickLeftUp) < nDoubleClickDelta)258 {259 NativeMethods.PostMessage(MainWindow.windowHandle, (uint)UiTaskName.LeftDblClick, 0, 0);260 }261 else262 {263 EnsureXPathReady();264 // Create a keyboard task if keys are recorded 265 PublishKeyboardInput();266 // Create a click task267 if (!string.IsNullOrEmpty(s_strXmlNodes))268 {269 lock (MouseKeyboardEventHandler.s_lockUiPath)270 {271 XmlNodePathRecorder.SetUiToRootXmlNodes(ref s_strXmlNodes);272 NativeMethods.PostMessage(MainWindow.windowHandle, (uint)UiTaskName.LeftClick, 0, 0);273 }274 }275 }276 }277 else if (mouseState == MouseState.LeftMouseDrag) // drag stop - down and up are at different points278 {279 int dragDeltaX = ptCursorUp.X - ptCursorDown.X;280 int dragDeltaY = ptCursorUp.Y - ptCursorDown.Y;281 //Add DragDeltaOffset to dragDeltaX and dragDeltaY in case they are negative282 NativeMethods.PostMessage(MainWindow.windowHandle, (uint)UiTaskName.DragStop, (uint)(ConstVariables.DragDeltaOffset + dragDeltaX), (uint)(ConstVariables.DragDeltaOffset + dragDeltaY));283 }284 else285 {286 //log error287 }288 tickLeftUp = Environment.TickCount;289 mouseState = MouseState.LeftMouseUp;290 }291 public static void RightMouseDown(int left, int top)292 {293 ResetRecordTimer();294 }295 public static void RightMouseUp(int left, int top)296 {297 EnsureXPathReady();298 PublishKeyboardInput();299 if (s_strXmlNodes != null)300 {301 lock (MouseKeyboardEventHandler.s_lockUiPath)302 {303 XmlNodePathRecorder.SetUiToRootXmlNodes(ref s_strXmlNodes);304 NativeMethods.PostMessage(MainWindow.windowHandle, (uint)UiTaskName.RightClick, 0, 0);305 }306 }307 }308 static int PublishKeyboardInput()309 {310 if (s_listRecordedKeycode.Count == 0)311 {312 return 0;313 }314 string strBase64 = Convert.ToBase64String(s_listRecordedKeycode.ToArray());315 s_listRecordedKeycode.Clear();316 XmlNodePathRecorder.SetBase64KeyboardInput(strBase64, s_bCapsLock, s_bNumLock, s_bScrollLock);317 NativeMethods.PostMessage(MainWindow.windowHandle, (uint)UiTaskName.KeyboardInput, (uint)s_keyboardInputTick, 0);318 return strBase64.Length;319 }320 public static void RecordKey(KeyboardEvents keyEvent, VirtualKeys vKey, int scanCode)321 {322 ResetRecordTimer();323 bool bIsKeydown = keyEvent == KeyboardEvents.SystemKeyDown || keyEvent == KeyboardEvents.KeyDown;324 // return if same key and up/down state325 if (s_lastKeyCode == vKey)326 {327 if (s_lastKeyDown == bIsKeydown)328 {329 return;330 }331 }...
Win32API.cs
Source:Win32API.cs
...23 public const int BUFFERSIZE = 4096 * 4; // must be same as BUFFERSIZE defined in UiTreeWalk.h24 [DllImport("user32.dll", SetLastError = true)]25 public static extern bool SetProcessDPIAware();26 [DllImport("User32.Dll")]27 public static extern bool PostMessage(IntPtr hWnd, uint msg, uint wParam, uint lParam);28 [DllImport("user32.dll")]29 public static extern IntPtr WindowFromPoint(int x, int y);30 [DllImport("kernel32.dll")]31 public static extern uint GetCurrentThreadId();32 [DllImport("user32.dll")]33 public static extern bool GetPhysicalCursorPos(out System.Drawing.Point point);34 [DllImport("user32.dll")]35 public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);36 [DllImport("USER32.dll")]37 public static extern short GetKeyState(int nVirtKey);38 [DllImport("UIXPathLib.dll", CallingConvention = CallingConvention.Winapi)]39 public static extern uint GetKeyboardReading(uint wParam, uint nParam, ref int vKeyCode, ref int scanCode);40 [DllImport("UIXPathLib.dll", CallingConvention = CallingConvention.Winapi)]41 public static extern uint SetWindowsHookExNative(HookProc hProc, uint nHookId, uint nThreadId);...
PostMessage
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Runtime.InteropServices;7using System.Windows.Forms;8using System.Threading;9{10 {11 [DllImport("user32.dll")]12 public static extern int PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);13 static void Main(string[] args)14 {15 Console.WriteLine("Enter the window handle of the application you wish to record");16 string windowHandle = Console.ReadLine();17 IntPtr handle = new IntPtr(Convert.ToInt32(windowHandle, 16));18 PostMessage(handle, 0x0312, 0, 0);19 Console.WriteLine("Recording started");20 Thread.Sleep(10000);21 PostMessage(handle, 0x0312, 0, 0);22 Console.WriteLine("Recording stopped");23 Console.ReadLine();24 }25 }26}27using System;28using System.Collections.Generic;29using System.Linq;30using System.Text;31using System.Threading.Tasks;32using System.Windows.Forms;33{34 {35 static void Main(string[] args)36 {37 Console.WriteLine("Enter the window handle of the application you wish to record");38 string windowHandle = Console.ReadLine();39 IntPtr handle = new IntPtr(Convert.ToInt32(windowHandle, 16));40 SendKeys.SendWait("^{ESC}");41 Console.WriteLine("Recording started");42 Thread.Sleep(10000);43 SendKeys.SendWait("^{ESC}");44 Console.WriteLine("Recording stopped");45 Console.ReadLine();46 }47 }48}49using System;50using System.Collections.Generic;51using System.Linq;52using System.Text;53using System.Threading.Tasks;54using System.Windows.Forms;55{56 {57 static void Main(string[] args)58 {59 Console.WriteLine("Enter the window handle of the application you wish to record");60 string windowHandle = Console.ReadLine();61 IntPtr handle = new IntPtr(Convert.ToInt32(windowHandle, 16));62 SendKeys.SendWait("^{ESC}");63 Console.WriteLine("Recording started");64 Thread.Sleep(10000);65 SendKeys.SendWait("^{ESC}");66 Console.WriteLine("Recording stopped");67 Console.ReadLine();
PostMessage
Using AI Code Generation
1private void button1_Click(object sender, EventArgs e)2{3 NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST,4 NativeMethods.WM_START_RECORDING, IntPtr.Zero, IntPtr.Zero);5}6private void button2_Click(object sender, EventArgs e)7{8 NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST,9 NativeMethods.WM_STOP_RECORDING, IntPtr.Zero, IntPtr.Zero);10}11private void button3_Click(object sender, EventArgs e)12{13 NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST,14 NativeMethods.WM_START_PLAYING, IntPtr.Zero, IntPtr.Zero);15}16private void button4_Click(object sender, EventArgs e)17{18 NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST,19 NativeMethods.WM_STOP_PLAYING, IntPtr.Zero, IntPtr.Zero);20}21private void button5_Click(object sender, EventArgs e)22{23 NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST,
PostMessage
Using AI Code Generation
1{2 {3 public Form1()4 {5 InitializeComponent();6 }7 private void button1_Click(object sender, EventArgs e)8 {9 NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_MYMESSAGE, IntPtr.Zero, IntPtr.Zero);10 }11 }12}13{14 {15 public Form2()16 {17 InitializeComponent();18 }19 protected override void WndProc(ref Message m)20 {21 if (m.Msg == NativeMethods.WM_MYMESSAGE)22 {23 MessageBox.Show("Message received");24 }25 base.WndProc(ref m);26 }27 }28}29{30 {31 public const int WM_MYMESSAGE = 0x400 + 1;32 public const int HWND_BROADCAST = 0xffff;33 [DllImport("user32.dll")]34 public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);35 }36}
PostMessage
Using AI Code Generation
1NativeMethods.PostMessage(hwnd, WM_APP, 0, 0);2NativeMethods.PostMessage(hwnd, WM_APP, 0, 0);3NativeMethods.SendMessage(hwnd, WM_APP, 0, 0);4NativeMethods.SendMessage(hwnd, WM_APP, 0, 0);5NativeMethods.SendNotifyMessage(hwnd, WM_APP, 0, 0);6NativeMethods.SendNotifyMessage(hwnd, WM_APP, 0, 0);7NativeMethods.SendMessageTimeout(hwnd, WM_APP, 0, 0, 0, 100, out int result);8NativeMethods.SendMessageTimeout(hwnd, WM_APP, 0, 0, 0, 100, out int result);9NativeMethods.SendNotifyMessageTimeout(hwnd, WM_APP, 0, 0, 0, 100, out int result);10NativeMethods.SendNotifyMessageTimeout(hwnd, WM_APP, 0, 0, 0, 100, out int result);11NativeMethods.SendMessageCallback(hwnd, WM_APP, 0, 0, null, 0);12NativeMethods.SendMessageCallback(hwnd, WM_APP, 0, 0, null, 0);
PostMessage
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6{7 {8 static void Main(string[] args)9 {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!