SHARE
TWEET

Untitled

a guest Dec 7th, 2013 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.         Rectangle bounds = new Rectangle(this.Location, this.Size);
  3.         Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
  4.         Graphics g = Graphics.FromImage(bitmap);
  5.         g.CopyFromScreen(this.Location, Point.Empty, bounds.Size);
  6.         Bitmap bp2 = new Bitmap(bitmap); //  local copy of image...
  7.         pictureBox1.Image = bp2;
  8.     }
  9.        
  10. {
  11.         this.StartPosition = FormStartPosition.Manual;  // get current window location
  12.         Point cur = this.Location;
  13.         this.Location = new Point(-500, -500);  //  hide the active app off screen.
  14.  
  15.         Rectangle bounds = new Rectangle(cur, this.Size);
  16.         Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
  17.         Graphics g = Graphics.FromImage(bitmap);
  18.         g.CopyFromScreen(cur, Point.Empty, bounds.Size);
  19.         Bitmap bp2 = new Bitmap(bitmap); //  local copy of image...
  20.         pictureBox1.Image = bp2;
  21.  
  22.         this.Location = cur;  // restore application location
  23.     }
  24.        
  25. using System;
  26. using System.ComponentModel;
  27. using System.Drawing;
  28. using System.Windows.Forms;
  29. using System.Runtime.InteropServices;
  30.  
  31. class Magnifier : Control {
  32.     public Magnifier() {
  33.         if (!MagInitialize()) throw new NotSupportedException();
  34.         timer = new Timer { Interval = 45 };
  35.         timer.Tick += (o, ea) => { if (trackMouse) setSource(false); else Invalidate(); };
  36.     }
  37.     [DefaultValue(false)]
  38.     public bool TrackMouse {
  39.         get { return trackMouse; }
  40.         set { trackMouse = value; setSource(false); }
  41.     }
  42.     [DefaultValue(2.0f)]
  43.     public float Magnification {
  44.         get { return magnification; }
  45.         set { magnification = Math.Max(1, value); setSource(true); }
  46.     }
  47.  
  48.     protected override CreateParams CreateParams {
  49.         get {
  50.             var cp = base.CreateParams;
  51.             if (!this.DesignMode) {
  52.                 cp.ClassName = "Magnifier";
  53.                 //cp.Style |= MS_SHOWMAGNIFIEDCURSOR;
  54.                 this.SetStyle(ControlStyles.UserPaint, true);
  55.             }
  56.             return cp;
  57.         }
  58.     }
  59.  
  60.     protected override void OnHandleCreated(EventArgs e) {
  61.         base.OnHandleCreated(e);
  62.         if (!this.DesignMode) {
  63.             setSource(true);
  64.             this.FindForm().LocationChanged += ParentLocationChanged;
  65.             timer.Start();
  66.         }
  67.     }
  68.     protected override void Dispose(bool disposing) {
  69.         if (disposing) {
  70.             var frm = this.FindForm();
  71.             if (frm != null) frm.LocationChanged -= ParentLocationChanged;
  72.             timer.Dispose();
  73.             MagUninitialize();
  74.         }
  75.         base.Dispose(disposing);
  76.     }
  77.  
  78.     private void ParentLocationChanged(object sender, EventArgs e) {
  79.         if (!trackMouse) setSource(false);
  80.     }
  81.     protected override void OnSizeChanged(EventArgs e) {
  82.         setSource(false);
  83.         base.OnSizeChanged(e);
  84.     }
  85.     protected override void OnMouseWheel(MouseEventArgs e) {
  86.         this.Magnification += e.Delta / 100f;
  87.         ((HandledMouseEventArgs)e).Handled = true;
  88.     }
  89.  
  90.     private void setSource(bool newmag) {
  91.         if (!this.IsHandleCreated || this.DesignMode) return;
  92.         if (newmag) {
  93.             var xform = new MAGTRANSFORM();
  94.             xform.v11 = xform.v22 = magnification;
  95.             xform.v33 = 1.0f;
  96.             MagSetWindowTransform(this.Handle, ref xform);
  97.         }
  98.         Point center;
  99.         if (trackMouse) center = Cursor.Position;
  100.         else {
  101.             var rc = this.RectangleToScreen(this.Bounds);
  102.             center = new Point(rc.Left + rc.Width / 2, rc.Top + rc.Height / 2);
  103.         }
  104.         var scr = Screen.FromPoint(center);
  105.         var rect = new RECT();
  106.         rect.left = Math.Max(scr.Bounds.Left, center.X - (int)(this.Width / magnification / 2));
  107.         rect.top = Math.Max(scr.Bounds.Top, center.Y - (int)(this.Height / magnification / 2));
  108.         rect.right = rect.left + (int)(this.Width / magnification);
  109.         if (rect.right > scr.Bounds.Right) {
  110.             rect.right = scr.Bounds.Right;
  111.             rect.left = rect.right - (int)(this.Width / magnification);
  112.         }
  113.         rect.bottom = center.Y + (int)(this.Height / magnification);
  114.         if (rect.bottom > scr.Bounds.Bottom) {
  115.             rect.bottom = scr.Bounds.Bottom;
  116.             rect.top = rect.bottom - (int)(this.Height / magnification);
  117.         }
  118.         MagSetWindowSource(this.Handle, ref rect);
  119.         this.Invalidate();
  120.     }
  121.  
  122.     private Timer timer;
  123.     private bool trackMouse;
  124.     private float magnification = 2.0f;
  125.  
  126.     private struct RECT {
  127.         public int left, top, right, bottom;
  128.     }
  129.     private struct MAGTRANSFORM {
  130.         public float v11, v12, v13;
  131.         public float v21, v22, v23;
  132.         public float v31, v32, v33;
  133.     }
  134.     [DllImport("magnification.dll")]
  135.     private static extern bool MagInitialize();
  136.     [DllImport("magnification.dll")]
  137.     private static extern bool MagUninitialize();
  138.     [DllImport("magnification.dll")]
  139.     private static extern bool MagSetWindowSource(IntPtr hWnd, ref RECT rc);
  140.     [DllImport("magnification.dll")]
  141.     private static extern bool MagSetWindowTransform(IntPtr hWnd, ref MAGTRANSFORM xform);
  142.     private const int MS_SHOWMAGNIFIEDCURSOR = 1;
  143.     private const int MS_CLIPAROUNDCURSOR = 2;
  144.     private const int MS_INVERTCOLORS = 4;
  145. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top