public static BitmapSource GetBitMapSourceFromBitmap(Bitmap bitmap) { IntPtr intPtrl = bitmap.GetHbitmap(); BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(intPtrl, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); GDIWin32Api.DeleteObject(intPtrl); return(bitmapSource); }
public void MouseMoveEventHandler(object sender, System.Windows.Forms.MouseEventArgs data) { this.txbCode.Text = $"x:{data.X},y:{data.Y},clickCount:{data.Clicks},button:{data.Button.ToString()}"; #region 查找坐标句柄并且跟上次对比 //根据当前鼠标位置获得窗口句柄 var currentHwnd = BaseWin32Api.WindowFromPoint(); if (maindHwnd == currentHwnd) { return; } maindHwnd = currentHwnd; if (currentHwnd == null) { return; } #endregion #region 查找句柄的当前元素 Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); //获得当前窗口句柄的UI元素 var rootHwnd = Utility.GetAutomationElementFromHandle(currentHwnd); stopwatch.Stop(); this.txbCode.Text += $"查找跟UI 花费时间:{stopwatch.ElapsedMilliseconds}毫秒"; if (rootHwnd != null) { if (rootHwnd.Current.Name.Equals(this.txbMoney.Text)) { stopwatch.Start(); GDIWin32Api.DrawRectangle(currentHwnd, Convert.ToInt32(rootHwnd.Current.BoundingRectangle.Width), Convert.ToInt32(rootHwnd.Current.BoundingRectangle.Height), 2, System.Drawing.Color.Red); stopwatch.Stop(); this.txbCode.Text += $"绘图 花费时间:{stopwatch.ElapsedMilliseconds}毫秒"; this.txbDescription.Text = $"总共找到{1}处金额,{rootHwnd.Current.Name}"; } } #endregion TextHelper.WriteAsync(this.txbCode.Text); }