The Basics for a Simple Screen Magnifier in C#
One of the first things I did in C# was to rewrite an application that I previously wrote using C++ and the Win32 API.
It is a magnifier that works in a window of arbitrary size. It works by taking a portion of the screen defined by a square with the mouse pointer at it’s top-left corner (in my example, a 200 by 200 area) and copying it onto a form, stretching it as necessary.
This is what I did:
1. I create a new WinForms application in Visual Studio.
2. In the properties window of the form, make sure double buffering is enabled.
3. In the class public partial class Form1 : Form I add the following member:
private Bitmap bmp = new Bitmap(200, 200, PixelFormat.Format32bppArgb);
I also add an additional using-statement: using System.Drawing.Imaging;
4. Finally I override the OnPaint event to do what I want, and to cause an immediate invalidation of the form:
protected override void OnPaint(PaintEventArgs e)
{
Graphics pgfx = e.Graphics;
pgfx.CopyFromScreen(Cursor.Position.X, Cursor.Position.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
pgfx.DrawImage(bmp, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
this.Invalidate();
}
Not very useful, but neat!
A bad thing right now is that the form continually repaints itself, despite the fact that this might not be needed. A possibility would be to only repaint the form when the mouse cursor moves, although that would cause problems with other things in the area that might change, a progress bar for instance.
