C# 如何使效果仅在移动鼠标时显示?
我从这里下载了放大镜效果的源代码文件: 这是主要的表单代码:C# 如何使效果仅在移动鼠标时显示?,c#,C#,我从这里下载了放大镜效果的源代码文件: 这是主要的表单代码: ///---------------------------------------------------------------------------- /// Class : MagnifierMainForm /// Purpose : Provide simple magnifier. /// Written by: Ogun TIGLI /// History : 31 May 2006/Wed st
///----------------------------------------------------------------------------
/// 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();
}
}
///----------------------------------------------------------------------------
///类别:放大镜主窗体
///用途:提供简易放大镜。
///作者:Ogun TIGLI
///历史记录:2006年5月31日/Wed开始日期。
///2006年12月22日/Fri次要代码修复和hotsot支持添加。
///2007年4月1日/添加了Sun XML序列化支持。
///
///注:
///本软件按“原样”提供,无任何明示或暗示
///保证书。在任何情况下,提交人都不对任何损害负责
///由于使用本软件而产生的。
///
///允许任何人出于任何目的使用本软件,
///包括商业应用程序,并对其进行修改和重新发布
///自由,受以下限制:
/// 1. 不得歪曲本软件的来源;
///您不能声称您编写了原始软件。
///如果您在产品中使用此软件,则会发出确认
///请在产品文档中注明。
/// 2. 更改后的源版本必须清楚地标记,并且
///不得被误传为原始软件。
/// 3. 本通知不得从任何来源删除、更改或更改
///代码分发。
///
///(c)2006-2007年奥贡·蒂格利。版权所有。
///----------------------------------------------------------------------------
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统文本;
使用System.Windows.Forms;
名称空间放大镜20070401
{
公共部分类放大镜main窗体:窗体
{
公共放大镜mainform()
{
初始化组件();
GetConfiguration();
//---我的名字---
FormBorderStyle=FormBorderStyle.无;
最顶端=真;
StartPosition=FormStartPosition.CenterScreen;
mImageMagnifierMainControlPanel=Properties.Resources.magControlPanel;
if(mimage放大镜主控制面板==null)
抛出新异常(“找不到资源!”);
宽度=mimage放大镜主控制面板。宽度;
高度=图像放大镜主控制面板。高度;
热点配置=新热点(新矩形(50,15,35,30));
hsConfiguration.OnMouseDown+=新热点.MouseEventDelegate(hsconfigurationonmousedown);
hsConfiguration.OnMouseUp+=新热点.MouseEventDelegate(hsConfiguration\OnMouseUp);
hsConfiguration.OnMouseMove+=新热点.MouseEventDelegate(hsConfiguration\OnMouseMove);
热点hsmagnifier=新热点(新矩形(10,15,30,30));
hsmagnifier.OnMouseMove+=新热点.MouseEventDelegate(hsmagnifier\u OnMouseMove);
hsmagnifier.OnMouseDown+=新热点.MouseEventDelegate(hsmagnifier\u OnMouseDown);
hsmagnifier.OnMouseUp+=新热点.MouseEventDelegate(hsmagnifier_OnMouseUp);
热点hsExit=新热点(新矩形(95,20,15,15));
hsExit.OnMouseUp+=新热点.MouseEventDelegate(hsexitonmouseup);
mHotSpots.Add(hsConfiguration);
mHotSpots.Add(hsmagnifier);
mHotSpots.Add(hsExit);
ShowInTaskbar=false;
}
显示时受保护的覆盖无效(事件参数e)
{
基地。昂肖恩(e);
if(mConfiguration.LocationX!=-1&&mConfiguration.LocationY!=-1)
{
位置=新点(mConfiguration.LocationX、mConfiguration.LocationY);
}
}
私有字符串mConfigFileName=“configData.xml”;
私有void GetConfiguration()
{
尝试
{
mConfiguration=(配置)XmlUtility.Deserialize(mConfiguration.GetType(),mConfigFileName);
}
抓住
{
mConfiguration=新配置();
}
}
私有void SaveConfiguration()
{
尝试
{
Serialize(mConfiguration,mConfigFileName);
}
捕获(例外e)
{
Console.WriteLine(“序列化问题:+e.Message”);
}
}
私有void hsu OnMouseMove配置(对象发送方)
{
}
私有void hsu OnMouseUp配置(对象发送方)
{
ConfigurationForm configForm=新的ConfigurationForm(mConfiguration);
configForm.ShowDialog(此);
}
私有void hsu OnMouseDown配置(对象发送方)
{
}
私有void hsmagnifier_OnMouseUp(对象发送方)
{
}
私有void hsmagnifier_OnMouseDown(对象发送方)
{
int x=mLastCursorPosition.x;
int y=mLastCursorPosition.y;
放大镜形式放大镜=新的放大镜形式(mConfiguration,mLastCursorPosition);
放大镜显示();
}
私有void hsmagnifier_OnMouseMove(对象发送方)
{
}
私有void hsexitonmouseup(对象发送方)
{
SaveConfiguration();
Application.Exit();
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
图形g=e.图形;
if(mimage放大镜主控制面板!=null)
{
g、 DrawImage(Mimage放大镜主控制面板,0,0,宽度,高度);
}
}
MouseEventArgs e上的受保护覆盖无效(MouseEventArgs e)
{
int x=e.x;
int y=e.y;