public static void SetBitmap(Bitmap bitmap, byte opacity, int left, int top, IntPtr handle) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(left, top); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } }
public async Task <bool> SetMouseToBobber(Win32.Point bobberPos, CancellationToken cancellationToken) // move mouse to previous recorded position and check shape { if (!await MoveMouseAndCheckCursor(bobberPos.x, bobberPos.y, cancellationToken, 1)) { Log.Information("Bobber lost. ({bx},{by})", bobberPos.x, bobberPos.y); int fixr = 24; Win32.Rect scanArea; scanArea.Left = bobberPos.x - fixr; scanArea.Right = bobberPos.x + fixr; scanArea.Top = bobberPos.y - fixr; scanArea.Bottom = bobberPos.y + fixr; // initiate a small-area search for bobber Win32.Point npos; npos.x = 0; npos.y = 0; npos = await LookForBobberSpiralImpl(scanArea, npos, 4, 1, cancellationToken); if (npos.x != 0 && npos.y != 0) { // search was successful Log.Information("Bobber found. ({bx},{by})", npos.x, npos.y); return(true); } else { Log.Information("Bobber flost. ({bx},{by})", npos.x, npos.y); return(false); } } return(true); }
private void GiveFeedback(object sender, GiveFeedbackEventArgs giveFeedbackEventArgs) { var w32Mouse = new Win32.Point(); Win32.GetCursorPos(ref w32Mouse); _dragAdorner.OffsetLeft = w32Mouse.X + _offset.X; _dragAdorner.OffsetTop = w32Mouse.Y + _offset.Y; }
/// <para>Changes the current bitmap with a custom opacity level. Here is where all happens!</para> private void SetBitmap(Bitmap bitmap, byte opacity) { if (LastBMPApplied != null) { LastBMPApplied.Dispose(); LastBMPApplied = null; } LastBMPApplied = bitmap; if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); } // The ideia of this is very simple, // 1. Create a compatible DC with screen; // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC; // 3. Call the UpdateLayeredWindow. var screenDc = Win32.GetDC(IntPtr.Zero); var memDc = Win32.CreateCompatibleDC(screenDc); var hBitmap = IntPtr.Zero; var oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = Win32.SelectObject(memDc, hBitmap); var size = new Win32.Size(bitmap.Width, bitmap.Height); var pointSource = new Win32.Point(0, 0); var topPos = new Win32.Point(Left, Top); var blend = new Win32.BLENDFUNCTION { BlendOp = Win32.AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = opacity, AlphaFormat = Win32.AC_SRC_ALPHA }; try { Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } catch { } } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak. Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } }
private void ShowBitmap(Image bitmap, byte opacity) { Bitmap copyBmp; // The idea of this is very simple, // 1. Create a compatible DC with screen; // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC; // 3. Call the UpdateLayeredWindow. try { lock (_form) { // On copie l'image parce qu'avec l'invocation on sait pas trop quand ça va être executé et l'image aura peut être été détruite. copyBmp = new Bitmap(bitmap); } IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = copyBmp.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(copyBmp.Width, copyBmp.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(Left, Top); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; this.InvokeAuto(() => Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA)); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); copyBmp.Dispose(); } } catch { } }
public void SetBits(int x, int y) { //绘制绘图层背景 Bitmap bitmap = new Bitmap(x, y); Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20); //窗体光泽重绘边界 Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighQuality; //高质量 g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量 ImageDrawRect.DrawRect(g, Properties.Resources.bgbk2, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1); if(!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(Width, Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = Byte.Parse("255"); blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if(hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } }
private IEnumerable <BobbyLocation> PointOfScreenDifferences() { var castbmp = Win32.CaptureWindow(_wow); var processingFilter = new FiltersSequence { new Grayscale(0.3725, 0.6154, 0.0121), new Pixellate(), new Difference(_background), new Threshold(15), new Erosion() }; var blobCounter = new BlobCounter(); blobCounter.ProcessImage(processingFilter.Apply(castbmp)); var brl = blobCounter.GetObjectsRectangles(); Log.Information("Bobber imagescan brl: {brl}", brl.Length); var sdl = new List <BobbyLocation>(); foreach (var br in brl) { var pt = new Win32.Point { x = (br.Left + br.Left + br.Right) * 4 / 12, y = (br.Top + br.Bottom + br.Bottom) * 4 / 12 }; Win32.ClientToScreen(_wow, ref pt); if (br.Right - br.Left > 9 && br.Bottom - br.Top > 9) { // Win32.Point pt = new Win32.Point { x= wowRectangle.X+(br.Left + br.Right) / 2, y= wowRectangle.Y+(br.Top+br.Bottom)/2 }; Log.Information("Bobber imagescan br: {bx},{by} - {w},{h}", pt.x, pt.y, br.Right - br.Left, br.Bottom - br.Top); sdl.Add(new BobbyLocation(pt)); // } else { // Log.Information("Bobber imagescan ignore br: {bx},{by} - {w},{h}", pt.x,pt.y, (br.Right-br.Left),(br.Bottom-br.Top)); } } // debug /* * BitmapExt bmpDst = new BitmapExt(castbmp); * using (var g = Graphics.FromImage(bmpDst)) { * foreach (var br in brl) { * if ((br.Right - br.Left) > 11 && (br.Bottom - br.Top) > 11) { * g.DrawRectangle(Pens.White, br); * } * } * } * bmpDst.Save("sc_"+DateTime.UtcNow.Ticks+".png", ImageFormat.Png); */ return(sdl); }
/// <summary> /// Updates the content of this window with a Bitmap /// </summary> /// <param name="Bmp">Bitmap to update this window with</param> /// <param name="Rect">Rectangle where this window should be located</param> public void Update(Bitmap Bmp, Rectangle Rect) { if(Bmp==null) return; // wait until it's safe to proceed //mutex.WaitOne(); /* * This function blits a Bitmap to a Layered Window. * ALL drawing functions that are for the transparent * mode use this function. */ //System.Diagnostics.Debug.WriteLine("started getting hbitmap from bitmap"); IntPtr Bmap=Bmp.GetHbitmap(Color.FromArgb(0)); //System.Diagnostics.Debug.WriteLine("done!"); IntPtr memDc = Win32.GDI.GDIAPI.CreateCompatibleDC(Win32.User32.User32API.GetDC(IntPtr.Zero)); IntPtr oldBitmap = Win32.GDI.GDIAPI.SelectObject(memDc, Bmap); try { Win32.User32.BlendFunction blend = new Win32.User32.BlendFunction(); blend.BlendOp = Win32.User32.BlendOperation.SourceOver; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = Win32.User32.AlphaFormat.SourceAlpha; Win32.Size size = new Win32.Size(Rect.Width, Rect.Height); Win32.Point topPos = new Win32.Point(Rect.X, Rect.Y); Win32.Point pointSource = new Win32.Point(0, 0); Win32.User32.User32API.UpdateLayeredWindow(this.Handle, IntPtr.Zero, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.User32.UpdateLayeredWindowFlags.Alpha); } finally { try { if (Bmap != IntPtr.Zero) { Win32.GDI.GDIAPI.SelectObject(memDc, oldBitmap); Win32.GDI.GDIAPI.DeleteObject(Bmap); } Win32.GDI.GDIAPI.DeleteDC(memDc); } catch(Exception){} //Bmp.Dispose(); } // release our mutex //mutex.ReleaseMutex(); }
public static void SetBits(int Width, int Height, int Left, int Top, Rectangle ClientRectangle) { //绘制绘图层背景 Bitmap bitmap = new Bitmap(Width + 10, Height + 10); Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20);//窗体光泽重绘边界 Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighQuality; //高质量 g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量 ImageDrawRect.DrawRect(g, Resources.main_light_bkg_top123, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1); if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) { throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); } IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(Width, Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = Byte.Parse("255"); blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; //Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } }
public void SetBits() { try { Bitmap bitmap = new Bitmap(base.Width + 10, base.Height + 10); Rectangle rectangle = new Rectangle(20, 20, 20, 20); Graphics graphics = Graphics.FromImage(bitmap); graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; ImageDrawRect.DrawRect(graphics, Resources.border, base.ClientRectangle, Rectangle.FromLTRB(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height), 1, 1); if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat)) { throw new ApplicationException("图片必须是32位带Alhpa通道的图片"); } IntPtr hObj = IntPtr.Zero; IntPtr dC = Win32.GetDC(IntPtr.Zero); IntPtr intPtr = IntPtr.Zero; IntPtr intPtr2 = Win32.CreateCompatibleDC(dC); try { Win32.Point point = new Win32.Point(base.Left, base.Top); Win32.Size size = new Win32.Size(base.Width, base.Height); Win32.BLENDFUNCTION bLENDFUNCTION = default(Win32.BLENDFUNCTION); Win32.Point point2 = new Win32.Point(0, 0); intPtr = bitmap.GetHbitmap(Color.FromArgb(0)); hObj = Win32.SelectObject(intPtr2, intPtr); bLENDFUNCTION.BlendOp = 0; bLENDFUNCTION.SourceConstantAlpha = byte.Parse("255"); bLENDFUNCTION.AlphaFormat = 1; bLENDFUNCTION.BlendFlags = 0; Win32.UpdateLayeredWindow(base.Handle, dC, ref point, ref size, intPtr2, ref point2, 0, ref bLENDFUNCTION, 2); } finally { if (intPtr != IntPtr.Zero) { Win32.SelectObject(intPtr2, hObj); Win32.DeleteObject(intPtr); } Win32.ReleaseDC(IntPtr.Zero, dC); Win32.DeleteDC(intPtr2); } } catch { } }
public void SetBits(Bitmap bitmap) { if (!haveHandle) { return; } if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) { throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); } IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(this.Location.X, this.Location.Y); Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); // 将bitmap创建为一个GDI位图对象 hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // 将hBitmap绘制到memDc当中,并记录被替换下的Bits oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = 255; blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } }
public void SetBits(Bitmap bitmap) { //this.TopMost = true; if (!haveHandle) { return; } if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) { throw new ApplicationException("The picture must be 32bit picture with alpha channel."); } IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = 255; blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } }
/// <para>Changes the current bitmap with a custom opacity level. Here is where all happens!</para> public void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); } // The ideia of this is very simple, // 1. Create a compatible DC with screen; // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC; // 3. Call the UpdateLayeredWindow. IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(this.Width, this.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(this.DesktopLocation.X, this.DesktopLocation.Y); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(this.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak. Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } }
public void SetBits() { if (BackgroundImage != null) { Bitmap bitmap = new Bitmap(BackgroundImage, Width, Height); if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) { throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); } IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(Width, Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = 255; blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } } }
/// <summary> /// Sets a Bitmap as background image for the current per-pixel alpha form. /// An additional global opacity modifier is supported. /// For best results, use .png file format images with 32 bits-per-pixel ARGB color data. /// </summary> /// <param name="bitmap">The background image for the current per-pixel alpha form.</param> /// <param name="opacity"> /// A global opacity modifier, range [0..255], with 0 being fully transparent and 255 being fully /// opaque. /// </param> public void SetBitmap(Bitmap bitmap, byte opacity = byte.MaxValue) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); } //1. Create a compatible DC with screen; //2. Select the bitmap with 32bpp with alpha-channel in the compatible DC; //3. Call the UpdateLayeredWindow. var screenDc = Win32.GetDC(IntPtr.Zero); var memDc = Win32.CreateCompatibleDC(screenDc); var hBitmap = IntPtr.Zero; var oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBitmap = Win32.SelectObject(memDc, hBitmap); var size = new Win32.Size(bitmap.Width, bitmap.Height); var pointSource = new Win32.Point(0, 0); var topPos = new Win32.Point(Left, Top); var blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } }
public void SetBits() { if (BackgroundImage != null) { //绘制绘图层背景 Bitmap bitmap = new Bitmap(BackgroundImage, base.Width, base.Height); if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(Width, Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = Byte.Parse(Main.SkinOpacity.ToString()); blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } } }
private async Task <Win32.Point> LookForBobberImpl(Win32.Rect scanArea, Win32.Point bobberPos, int steps, int retries, CancellationToken cancellationToken) { int XPOSSTEP = (int)((scanArea.Right - scanArea.Left) / steps); int YPOSSTEP = (int)((scanArea.Bottom - scanArea.Top) / steps); int XOFFSET = (int)(XPOSSTEP / retries); for (int tryCount = 0; tryCount < retries; ++tryCount) { for (int x = (int)(scanArea.Left + (XOFFSET * tryCount)); x < scanArea.Right; x += XPOSSTEP) { for (int y = scanArea.Top; y < scanArea.Bottom; y += YPOSSTEP) { if (await MoveMouseAndCheckCursor(x, y, cancellationToken, 1)) { bobberPos.x = x; bobberPos.y = y; return(bobberPos); } } } } return(bobberPos); }
//private void T_Tick(object sender, EventArgs e) //{ // System.Windows.Forms.Timer t = sender as System.Windows.Forms.Timer; // t.Stop(); // Action(); // return; //} private void NewMethod(Bitmap bitmap, IntPtr oldBits, IntPtr screenDC, IntPtr hBitmap, IntPtr memDc) { try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(Width, Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = Byte.Parse("255"); blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; try { Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } catch (Exception) { } } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } }
public void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(Left, Top); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } }
public void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); } IntPtr dC = Win32.GetDC(IntPtr.Zero); IntPtr intPtr = Win32.CreateCompatibleDC(dC); IntPtr intPtr2 = IntPtr.Zero; IntPtr hObj = IntPtr.Zero; try { intPtr2 = bitmap.GetHbitmap(Color.FromArgb(0)); hObj = Win32.SelectObject(intPtr, intPtr2); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.Point point = new Win32.Point(0, 0); Win32.Point point2 = new Win32.Point(base.Left, base.Top); Win32.BLENDFUNCTION bLENDFUNCTION = default(Win32.BLENDFUNCTION); bLENDFUNCTION.BlendOp = 0; bLENDFUNCTION.BlendFlags = 0; bLENDFUNCTION.SourceConstantAlpha = opacity; bLENDFUNCTION.AlphaFormat = 1; Win32.UpdateLayeredWindow(base.Handle, dC, ref point2, ref size, intPtr, ref point, 0, ref bLENDFUNCTION, 2); } finally { Win32.ReleaseDC(IntPtr.Zero, dC); if (intPtr2 != IntPtr.Zero) { Win32.SelectObject(intPtr, hObj); Win32.DeleteObject(intPtr2); } Win32.DeleteDC(intPtr); } }
/// <summary> /// 绘制位图 /// </summary> /// <param name="bitmap">位图</param> /// <param name="opacity">透明度</param> /// <returns></returns> public void SetBitmap(Bitmap bitmap, byte opacity = 255) { //if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat)) //{ // throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); //} IntPtr hObj = IntPtr.Zero; IntPtr dC = Win32.GetDC(IntPtr.Zero); IntPtr intPtr = IntPtr.Zero; IntPtr intPtr2 = Win32.CreateCompatibleDC(dC); try { Win32.Point point = new Win32.Point(base.Left, base.Top); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.BLENDFUNCTION bLENDFUNCTION = default(Win32.BLENDFUNCTION); Win32.Point point2 = new Win32.Point(0, 0); intPtr = bitmap.GetHbitmap(Color.FromArgb(0)); hObj = Win32.SelectObject(intPtr2, intPtr); bLENDFUNCTION.BlendOp = 0; bLENDFUNCTION.SourceConstantAlpha = opacity; bLENDFUNCTION.AlphaFormat = 1; bLENDFUNCTION.BlendFlags = 0; Win32.UpdateLayeredWindow(base.Handle, dC, ref point, ref size, intPtr2, ref point2, 0, ref bLENDFUNCTION, 2); } finally { if (intPtr != IntPtr.Zero) { Win32.SelectObject(intPtr2, hObj); Win32.DeleteObject(intPtr); } Win32.ReleaseDC(IntPtr.Zero, dC); Win32.DeleteDC(intPtr2); } }
public void SetBits(Bitmap bitmap) { if (!haveHandle) return; if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) throw new ApplicationException("The picture must be 32bit picture with alpha channel."); IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = 255; blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } }
public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Win32.Point pptDst, ref Win32.Size psize, IntPtr hdcSrc, ref Win32.Point pptSrc, int crKey, ref Win32.BLENDFUNCTION pblend, int dwFlags);
private async Task <Win32.Point> LookForBobberSpiralImpl(Win32.Rect scanArea, Win32.Point bobberPos, int steps, int retries, CancellationToken cancellationToken) { int XPOSSTEP = (int)((scanArea.Right - scanArea.Left) / steps); int YPOSSTEP = (int)((scanArea.Bottom - scanArea.Top) / steps); int XOFFSET = (int)(XPOSSTEP / retries); int YOFFSET = (int)(YPOSSTEP / retries); for (int tryCount = 0; tryCount < retries; ++tryCount) { int x = (int)((scanArea.Left + scanArea.Right) / 2) + XOFFSET * tryCount; int y = (int)((scanArea.Top + scanArea.Bottom) / 2) + YOFFSET * tryCount; for (int i = 0; i <= 2 * steps; i++) { for (int j = 0; j <= (i / 2); j++) { int dx = 0, dy = 0; if (i % 2 == 0) { if ((i / 2) % 2 == 0) { dx = XPOSSTEP; dy = 0; } else { dx = -XPOSSTEP; dy = 0; } } else { if ((i / 2) % 2 == 0) { dx = 0; dy = YPOSSTEP; } else { dx = 0; dy = -YPOSSTEP; } } x += dx; y += dy; if (await MoveMouseAndCheckCursor(x, y, cancellationToken, 1)) { bobberPos.x = x; bobberPos.y = y; return(bobberPos); } } } } return(bobberPos); }
public async Task <Win32.Point> LookForBobber(CancellationToken cancellationToken) { Win32.Rect scanArea; if (!Properties.Settings.Default.customScanArea) { scanArea.Left = wowRectangle.X + wowRectangle.Width / 5; scanArea.Right = wowRectangle.X + wowRectangle.Width / 5 * 4; scanArea.Top = wowRectangle.Y + wowRectangle.Height / 4; scanArea.Bottom = wowRectangle.Y + wowRectangle.Height / 4 * 3; //Log.Information("Using default area"); } else { scanArea.Left = Properties.Settings.Default.minScanXY.X; scanArea.Top = Properties.Settings.Default.minScanXY.Y; scanArea.Right = Properties.Settings.Default.maxScanXY.X; scanArea.Bottom = Properties.Settings.Default.maxScanXY.Y; //Log.Information("Using custom area"); } Log.Information("Scanning area: " + scanArea.Left.ToString() + " , " + scanArea.Top.ToString() + " , " + scanArea.Right.ToString() + " , " + scanArea.Bottom.ToString() + " cs: " + bobberPosDict.Keys.Count.ToString()); Win32.Point bobberPos = new Win32.Point { x = 0, y = 0 }; foreach (Win32.Point dp in PointOfScreenDifferences()) { if (await MoveMouseAndCheckCursor(dp.x, dp.y, cancellationToken, 2)) { bobberPos = dp; Log.Information("Bobber imagescan hit. ({bx},{by})", bobberPos.x, bobberPos.y); break; } } if (bobberPos.x == 0 && bobberPos.y == 0) { // utilize previous hits foreach (KeyValuePair <Win32.Point, int> pos in System.Linq.Enumerable.OrderBy(bobberPosDict, (key => key.Value))) { // do something with item.Key and item.Value if (await MoveMouseAndCheckCursor(pos.Key.x, pos.Key.y, cancellationToken, 2)) { bobberPos = pos.Key; Log.Information("Bobber position cache hit. ({bx},{by})", bobberPos.x, bobberPos.y); break; } } } if (bobberPos.x == 0 && bobberPos.y == 0) { Random rnd = new Random(); a_ScanningSteps = rnd.Next(Properties.Settings.Default.ScanningStepsLow, Properties.Settings.Default.ScanningStepsHigh); if (Properties.Settings.Default.AlternativeRoute) { bobberPos = await LookForBobberSpiralImpl(scanArea, bobberPos, a_ScanningSteps, Properties.Settings.Default.ScanningRetries, cancellationToken); } else { bobberPos = await LookForBobberImpl(scanArea, bobberPos, a_ScanningSteps, Properties.Settings.Default.ScanningRetries, cancellationToken); } } if (bobberPos.x != 0 && bobberPos.y != 0) { int hitcount = 1; if (bobberPosDict.ContainsKey(bobberPos)) { bobberPosDict.TryGetValue(bobberPos, out hitcount); hitcount++; bobberPosDict.Remove(bobberPos); } bobberPosDict.Add(bobberPos, hitcount); } Log.Information("Bobber scan finished. ({bx},{by})", bobberPos.x, bobberPos.y); return(bobberPos); }
/// <para>Changes the current bitmap with a custom opacity level. Here is where all happens!</para> public void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); // The ideia of this is very simple, // 1. Create a compatible DC with screen; // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC; // 3. Call the UpdateLayeredWindow. Bitmap bmp = null; if (this.Controls.Count == 0) bmp = bitmap; else { bmp = new Bitmap(bitmap); foreach (Control item in this.Controls) { if (item.Visible) item.DrawToBitmap(bmp, item.Bounds); } } IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bmp.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(Left, Top); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } }
/// <para>Changes the current bitmap with a custom opacity level. Here is where all happens!</para> public void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); } // The ideia of this is very simple, // 1. Create a compatible DC with screen; // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC; // 3. Call the UpdateLayeredWindow. Bitmap bmp = null; if (this.Controls.Count == 0) { bmp = bitmap; } else { bmp = new Bitmap(bitmap); foreach (Control item in this.Controls) { if (item.Visible) { item.DrawToBitmap(bmp, item.Bounds); } } } IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bmp.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(Left, Top); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } }
public BobbyLocation(Win32.Point point) { X = point.x; Y = point.y; }
/// <summary> /// Alpha Form核心代码 /// bitmap为一张带有Alpha通道的32位位图 /// opacity指定窗体的透明度 /// </summary> /// <param name="bitmap"></param> /// <param name="opacity"></param> public void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); } IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(curPostion.X, curPostion.Y); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); /**/ ////Construct Win32.BLENDFUNCTION blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; //The Alpha Form Core Function Win32.UpdateLayeredWindow(HWND, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { //Release Resource Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); //Windows.DeleteObject(hBitmap); Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } } #endregion #region 事件处理代码 //登录事件 private void WTMainForm_Load(object sender, EventArgs e) { SetBitmap(leimu[0]); Thread t = new Thread(qiehuan); t.Start(); } void qiehuan() { int lingshi = 1; while (true) { lingshi++; lingshi %= 50; SetBitmap(leimu[lingshi]); Thread.Sleep(60); } } private void WTMainForm_MouseDown(object sender, MouseEventArgs e) { isDrag = true; oldPostion.X = e.X; oldPostion.Y = e.Y; } private void WTMainForm_MouseUp(object sender, MouseEventArgs e) { isDrag = false; } private void WTMainForm_MouseMove(object sender, MouseEventArgs e) { if ((e.Button == MouseButtons.Left) && (isDrag == true)) { this.Left += e.X - oldPostion.X; this.Top += e.Y - oldPostion.Y; curPostion.X = this.Left; curPostion.Y = this.Top; } } private void WTMainForm_MouseEnter(object sender, EventArgs e) { if (isDownLoading == true) { isMouseEnter = true; mouseEnterFinish = false; mouseEnterThread = new Thread(new ThreadStart(MouseEnterThreadFun)); mouseEnterThread.Start(); } } private void WTMainForm_MouseLeave(object sender, EventArgs e) { if (isDownLoading == true) { isMouseEnter = false; mouseLeaveFinish = false; mouseLeaveThread = new Thread(new ThreadStart(MouseLeaveThreadFun)); mouseLeaveThread.Start(); } } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { canColse = true; Environment.Exit(0); } private void 变大ToolStripMenuItem_Click_1(object sender, EventArgs e) { /* * if (isDownLoading == false) * { * isDownLoading = true; * downLoadingThread = new Thread(new ThreadStart(DownLoadingThreadFun)); * downLoadingThread.Start(); * }*/ }
/// <para>Changes the current bitmap with a custom opacity level. Here is where all happens!</para> public void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); // The ideia of this is very simple, // 1. Create a compatible DC with screen; // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC; // 3. Call the UpdateLayeredWindow. IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(Left, Top); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak. Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } }
protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_HOTKEY) { Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed. int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed. if (id == 0 || id == 3) //start-stop { if (!nextActionTimer.Enabled && !playerFeedTimer.Enabled) { Win32.Rect r = Win32.GetARKRectangle(); if (r.Right != 0) { nextActionTimer.Enabled = typeSelector.SelectedIndex != 0; statusLabel.Text = "Feeder switched on."; Console.Out.WriteLine("Feeder switched on. " + r.Right + " " + r.Bottom); ccpLocation = Win32.GetCurrentCursorPoint(); Console.Out.WriteLine("CCP. " + ccpLocation.x + " " + ccpLocation.y); ffLocation.x = 1055 * r.Right / 1920; ffLocation.y = 768 * r.Bottom / 1080; if (r.Bottom == 1024 && r.Right == 1600) { ffLocation.y = 700; } nLocation.x = 920 * r.Right / 1920; nLocation.y = 320 * r.Bottom / 1080; playerFeedTimer.Enabled = typeSelector1.SelectedIndex != 0; } else { Console.Out.WriteLine("ARK window not found."); statusLabel.Text = "ARK window not found. " + feedDelayText.Text + " " + typeSelector.SelectedIndex; // nextActionTimer.Enabled = true; } } else { Console.Out.WriteLine("Feeder switched off."); statusLabel.Text = "Feeder switched off."; nextActionTimer.Enabled = false; playerFeedTimer.Enabled = false; } } else if (id == 1) // increaserate { if (nextActionTimer.Interval > 100) { nextActionTimer.Interval -= 100; // one tenth second } feedDelayText.Text = nextActionTimer.Interval.ToString(); } else if (id == 2) // decreaserate { nextActionTimer.Interval += 100; feedDelayText.Text = nextActionTimer.Interval.ToString(); } } }
private async Task Fish(CancellationToken cancellationToken) { m_mouth.Say(Translate.GetTranslate("manager", "LABEL_CASTING")); m_eyes.updateBackground(); await m_hands.Cast(cancellationToken); m_mouth.Say(Translate.GetTranslate("manager", "LABEL_FINDING")); // Make bobber found async, so can check fishing sound in parallel, the result only important when we hear fish. // The position used for repositioning. CancellationTokenSource eyeCancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); CancellationToken eyeCancelToken = eyeCancelTokenSource.Token; Task <Win32.Point> eyeTask = Task.Run(async() => await m_eyes.LookForBobber(eyeCancelToken)); // Update UI with wait status CancellationTokenSource uiUpdateCancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); CancellationToken uiUpdateCancelToken = uiUpdateCancelTokenSource.Token; var progress = new Progress <long>(msecs => { if (!uiUpdateCancelToken.IsCancellationRequested && !cancellationToken.IsCancellationRequested) { m_mouth.Say(Translate.GetTranslate( "manager", "LABEL_WAITING", msecs / SECOND, a_FishWait / SECOND)); } }); var uiUpdateTask = Task.Run( async() => await UpdateUIWhileWaitingToHearFish(progress, uiUpdateCancelToken), uiUpdateCancelToken); Random rnd = new Random(); a_FishWait = rnd.Next(Properties.Settings.Default.FishWaitLow, Properties.Settings.Default.FishWaitHigh); bool fishHeard = await m_ears.Listen( a_FishWait, cancellationToken); //Log.Information("Ear result: "+a_FishWait.ToString()); uiUpdateCancelTokenSource.Cancel(); try { uiUpdateTask.GetAwaiter().GetResult(); // Wait & Unwrap // https://github.com/StephenCleary/AsyncEx/blob/dc54d22b06566c76db23af06afcd0727cac625ef/Source/Nito.AsyncEx%20(NET45%2C%20Win8%2C%20WP8%2C%20WPA81)/Synchronous/TaskExtensions.cs#L18 } catch (TaskCanceledException) { } finally { uiUpdateCancelTokenSource.Dispose(); } if (!fishHeard) { m_fishingStats.RecordNotHeard(); m_fishErrorLength++; return; } // We heard the fish, let's check bobbers position if (!eyeTask.IsCompleted) { // the search is not finished yet, but fish is heard, we have 2 seconds left to find and hook it eyeTask.Wait(2000, cancellationToken); eyeCancelTokenSource.Cancel(); } eyeCancelTokenSource.Dispose(); if (eyeTask.IsCompleted) { // search is ended what's the result? Win32.Point bobberPos = eyeTask.Result; if (bobberPos.x != 0 && bobberPos.y != 0) { // bobber found if (await m_eyes.SetMouseToBobber(bobberPos, cancellationToken)) { // bobber is still there Log.Information("Bobber databl: ({bx},{by})", bobberPos.x, bobberPos.y); await m_hands.Loot(); m_mouth.Say(Translate.GetTranslate("manager", "LABEL_HEAR_FISH")); m_fishingStats.RecordSuccess(); m_fishErrorLength = 0; Log.Information("Fish success"); return; } } } m_fishingStats.RecordBobberNotFound(); m_fishErrorLength++; }
public SplashScreen(Bitmap splashImage) : base() { if (splashImage == null) { throw new ArgumentNullException(); } if (splashImage.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); } this.UseFadeIn = true; this.UseFadeOut = true; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None; this.DoubleBuffered = true; this.mySplashImage = splashImage; // This form should not have a border or else Windows will clip it. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.MaximizeBox = false; this.Name = "SplashScreen"; this.DestinationOpacity = 0; this.opaSetTimes = 0; this.closingTime = false; this.codeclosing = false; // Force it to be at Center of the screen /*var sizeofImage = Properties.Resources.splashimage.Size; * Rectangle myBound = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea; * if (myBound.Size.Width < sizeofImage.Width || myBound.Size.Height < sizeofImage.Height) * { * this.DesktopBounds = new Rectangle(0, 0, * myBound.Size.Width < sizeofImage.Width ? myBound.Size.Width : sizeofImage.Width, * myBound.Size.Height < sizeofImage.Height ? myBound.Size.Height : sizeofImage.Height); * } * else * this.DesktopBounds = new Rectangle( * (myBound.Size.Width / 2) - (sizeofImage.Width / 2), * (myBound.Size.Height / 2) - (sizeofImage.Height / 2), * sizeofImage.Width, * sizeofImage.Height * ); * //*/ this.ClientSize = this.mySplashImage.Size; this.screenDc = Win32.GetDC(IntPtr.Zero); this.memDc = Win32.CreateCompatibleDC(screenDc); this.hBitmap = IntPtr.Zero; this.oldBitmap = IntPtr.Zero; try { this.hBitmap = this.mySplashImage.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap this.oldBitmap = Win32.SelectObject(this.memDc, this.hBitmap); this.drawsize = new Win32.Size(this.mySplashImage.Width, this.mySplashImage.Height); this.pointSource = new Win32.Point(0, 0); this.blend = new Win32.BLENDFUNCTION(); this.blend.BlendOp = Win32.AC_SRC_OVER; this.blend.BlendFlags = 0; this.blend.AlphaFormat = Win32.AC_SRC_ALPHA; } catch (Exception ex) { Win32.ReleaseDC(IntPtr.Zero, this.screenDc); if (this.hBitmap != IntPtr.Zero) { Win32.SelectObject(this.memDc, this.oldBitmap); // Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak. Win32.DeleteObject(this.hBitmap); } Win32.DeleteDC(this.memDc); throw ex; } this.myTimer = new System.Windows.Forms.Timer(); this.myTimer.Enabled = false; this.myTimer.Tick += MyTimer_Tick; this.myTimer.Interval = 20; }