I have downloaded the source code files from here of a magnifier glass effect:
http://www.codeproject.com/Articles/18235/Simple-Magnifier
And this is the main Form code:
///----------------------------------------------------------------------------
/// Class : MagnifierMainForm
/// Purpose : Provide simple magnifier.
/// Written by: Ogun TIGLI
/// History : 31 May 2006/Wed starting date.
/// 22 Dec 2006/Fri minor code fixes and hotsot support addition.
/// 01 Apr 2007/Sun XML serialization support added.
///
/// Notes:
/// This software is provided 'as-is', without any express or implied
/// warranty. In no event will the author be held liable for any damages
/// arising from the use of this software.
///
/// Permission is granted to anyone to use this software for any purpose,
/// including commercial applications, and to alter it and redistribute it
/// freely, subject to the following restrictions:
/// 1. The origin of this software must not be misrepresented;
/// you must not claim that you wrote the original software.
/// If you use this software in a product, an acknowledgment
/// in the product documentation would be appreciated.
/// 2. Altered source versions must be plainly marked as such, and
/// must not be misrepresented as being the original software.
/// 3. This notice cannot be removed, changed or altered from any source
/// code distribution.
///
/// (c) 2006-2007 Ogun TIGLI. All rights reserved.
///----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Magnifier20070401
{
public partial class MagnifierMainForm : Form
{
public MagnifierMainForm()
{
InitializeComponent();
GetConfiguration();
//--- My Init ---
FormBorderStyle = FormBorderStyle.None;
TopMost = true;
StartPosition = FormStartPosition.CenterScreen;
mImageMagnifierMainControlPanel = Properties.Resources.magControlPanel;
if (mImageMagnifierMainControlPanel == null)
throw new Exception("Resource cannot be found!");
Width = mImageMagnifierMainControlPanel.Width;
Height = mImageMagnifierMainControlPanel.Height;
HotSpot hsConfiguration = new HotSpot(new Rectangle(50, 15, 35, 30));
hsConfiguration.OnMouseDown += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseDown);
hsConfiguration.OnMouseUp += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseUp);
hsConfiguration.OnMouseMove += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseMove);
HotSpot hsMagnfier = new HotSpot(new Rectangle(10, 15, 30, 30));
hsMagnfier.OnMouseMove += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseMove);
hsMagnfier.OnMouseDown += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseDown);
hsMagnfier.OnMouseUp += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseUp);
HotSpot hsExit = new HotSpot(new Rectangle(95, 20, 15, 15));
hsExit.OnMouseUp += new HotSpot.MouseEventDelegate(hsExit_OnMouseUp);
mHotSpots.Add(hsConfiguration);
mHotSpots.Add(hsMagnfier);
mHotSpots.Add(hsExit);
ShowInTaskbar = false;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
if (mConfiguration.LocationX != -1 && mConfiguration.LocationY != -1)
{
Location = new Point(mConfiguration.LocationX, mConfiguration.LocationY);
}
}
private string mConfigFileName = "configData.xml";
private void GetConfiguration()
{
try
{
mConfiguration = (Configuration)XmlUtility.Deserialize(mConfiguration.GetType(), mConfigFileName);
}
catch
{
mConfiguration = new Configuration();
}
}
private void SaveConfiguration()
{
try
{
XmlUtility.Serialize(mConfiguration, mConfigFileName);
}
catch (Exception e)
{
Console.WriteLine("Serialization problem: " + e.Message);
}
}
private void hsConfiguration_OnMouseMove(Object sender)
{
}
private void hsConfiguration_OnMouseUp(Object sender)
{
ConfigurationForm configForm = new ConfigurationForm(mConfiguration);
configForm.ShowDialog(this);
}
private void hsConfiguration_OnMouseDown(Object sender)
{
}
private void hsMagnfier_OnMouseUp(object sender)
{
}
private void hsMagnfier_OnMouseDown(object sender)
{
int x = mLastCursorPosition.X;
int y = mLastCursorPosition.Y;
MagnifierForm magnifier = new MagnifierForm(mConfiguration, mLastCursorPosition);
magnifier.Show();
}
private void hsMagnfier_OnMouseMove(object sender)
{
}
private void hsExit_OnMouseUp(Object sender)
{
SaveConfiguration();
Application.Exit();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
if (mImageMagnifierMainControlPanel != null)
{
g.DrawImage(mImageMagnifierMainControlPanel, 0, 0, Width, Height);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
mPointMouseDown = new Point(e.X, e.Y);
mLastCursorPosition = Cursor.Position;
foreach (HotSpot hotSpot in mHotSpots)
{
// If mouse event handled by this hot-stop then return!
if (hotSpot.ProcessMouseDown(e)) return;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
foreach (HotSpot hotSpot in mHotSpots)
{
// If mouse event handled by this hot-stop then return!
if (hotSpot.ProcessMouseUp(e)) return;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
foreach (HotSpot hotSpot in mHotSpots)
{
// If mouse event handled by this hot-stop then return!
if (hotSpot.ProcessMouseMove(e))
{
Cursor = Cursors.Hand;
return;
}
}
Cursor = Cursors.SizeAll;
if (e.Button == MouseButtons.Left)
{
int dx = e.X - mPointMouseDown.X;
int dy = e.Y - mPointMouseDown.Y;
Left += dx;
Top += dy;
mConfiguration.LocationX = Left;
mConfiguration.LocationY = Top;
}
}
private Image mImageMagnifierMainControlPanel = null;
private List<HotSpot> mHotSpots = new List<HotSpot>();
private Point mPointMouseDown;
private Point mLastCursorPosition;
private Configuration mConfiguration = new Configuration();
}
}
Now it will work on two cases:
I want to change it to:
I tried many different things, but couldn't figure out how to do it.
How can I do it?
Ok, so this is a complete hack and should probably be done a bit cleaner, but this should get you what you want:
In MagnifierMainForm.cs move the hsMagnfier_OnMouseDown
code to hsMagnfier_OnMouseUp
function.
In MagnifierForm.cs remove mTimer completely. Everywhere you see mTimer.Enabled = true
(except for in the constructor MagnifierForm
), add the line HandleTimer(null, new EventArgs());
I see that in OnMouseDown
and OnMouseMove
In OnMouseMove, remove the check for e.Button == MouseButtons.Left
I am the author for the code that I published in the codeproject many years ago (https://www.codeproject.com/Articles/18235/Simple-Magnifier), now making it publicly available in the GitHub.
Please feel free to contribute and/or request features.
Ogun (omt66)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With