上面的内容是从以下这个图片拖动类中摘取。
/* Copyright (c) 2008 黑色珊瑚::Tsorgy.Utils, Reserved.
* Filename: @(#)DragPictureBox.cs
* Create by: TsOrgY
* Email: [email protected]
* Date: 2008/12/24 0:27:04
* Classname: DragPictureBox
* Description: 可拖动图片的图片框
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;namespace Tsorgy.Utils
{
/// 可拖动图片的图片框.
[DefaultProperty("Image")]
public class DragPictureBox : Panel
{
// 当前图像显示坐标.
private int _x, _y;
// 临时偏移量.
private int t_x, t_y; /// 获取或设置显示在 <see cref="Tsorgy.Utils.DragPictureBox"/> 上的图片.
[Category("Appearance")]
[Description("显示在 DragPictureBox 上的图片")]
[Localizable(true)]
[DefaultValue(null)]
public Image Image
{
get { return _image; }
set
{
_image = value;
this.Invalidate();
}
}
private Image _image; /// 初始化 <see cref="Tsorgy.Utils.DragPictureBox"/> 类的新实例.
public DragPictureBox() : base()
{
_image = null;
// 设置双缓冲.
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
} /// 引发 <see cref="E:System.Windows.Forms.Control.Paint"></see> 事件.
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs"></see>.</param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (_image != null)
{
Graphics g = e.Graphics;
g.DrawImage(_image, _x, _y, _image.Width, _image.Height);
}
} /// 引发 <see cref="E:System.Windows.Forms.Control.MouseMove"></see> 事件.
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.MouseEventArgs"></see>.</param>
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_image != null && e.Button == MouseButtons.Left)
{
_x = e.X - t_x;
_y = e.Y - t_y;
// 防止图片拖出边缘.
if (_x > 0)
_x = 0;
else if (_x < this.Width - _image.Width)
_x = this.Width - _image.Width;
if (_y > 0)
_y = 0;
else if (_y < this.Height - _image.Height)
_y = this.Height - _image.Height;
// 重绘.
this.Invalidate(false);
}
} /// 引发 <see cref="E:System.Windows.Forms.Control.MouseDown"></see> 事件.
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.MouseEventArgs"></see>.</param>
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (_image != null)
{
t_x = e.X - _x;
t_y = e.Y - _y;
}
}
}
}
/* Copyright (c) 2008 黑色珊瑚::Tsorgy.Utils, Reserved.
* Filename: @(#)DragPictureBox.cs
* Create by: TsOrgY
* Email: [email protected]
* Date: 2008/12/24 0:27:04
* Classname: DragPictureBox
* Description: 可拖动图片的图片框
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;namespace Tsorgy.Utils
{
/// 可拖动图片的图片框.
[DefaultProperty("Image")]
public class DragPictureBox : Panel
{
// 当前图像显示坐标.
private int _x, _y;
// 临时偏移量.
private int t_x, t_y; /// 获取或设置显示在 <see cref="Tsorgy.Utils.DragPictureBox"/> 上的图片.
[Category("Appearance")]
[Description("显示在 DragPictureBox 上的图片")]
[Localizable(true)]
[DefaultValue(null)]
public Image Image
{
get { return _image; }
set
{
_image = value;
this.Invalidate();
}
}
private Image _image; /// 初始化 <see cref="Tsorgy.Utils.DragPictureBox"/> 类的新实例.
public DragPictureBox() : base()
{
_image = null;
// 设置双缓冲.
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
} /// 引发 <see cref="E:System.Windows.Forms.Control.Paint"></see> 事件.
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs"></see>.</param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (_image != null)
{
Graphics g = e.Graphics;
g.DrawImage(_image, _x, _y, _image.Width, _image.Height);
}
} /// 引发 <see cref="E:System.Windows.Forms.Control.MouseMove"></see> 事件.
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.MouseEventArgs"></see>.</param>
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_image != null && e.Button == MouseButtons.Left)
{
_x = e.X - t_x;
_y = e.Y - t_y;
// 防止图片拖出边缘.
if (_x > 0)
_x = 0;
else if (_x < this.Width - _image.Width)
_x = this.Width - _image.Width;
if (_y > 0)
_y = 0;
else if (_y < this.Height - _image.Height)
_y = this.Height - _image.Height;
// 重绘.
this.Invalidate(false);
}
} /// 引发 <see cref="E:System.Windows.Forms.Control.MouseDown"></see> 事件.
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.MouseEventArgs"></see>.</param>
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (_image != null)
{
t_x = e.X - _x;
t_y = e.Y - _y;
}
}
}
}
有人使用 Attribute,它就有意义。因此你理解了“哪一个程序需要读取特定 Attribute,才能理解 Atribute。Attribute是类型中的配置信息,用来代替一堆离散的 xml 配置文件,Attribute的方式远比后者更好用。
{
get { return _image; }
set
{
_image = value;
this.Invalidate();
}
}
private Image _image;
protperty 就是如此的,具体意义就是这个可执行的get、set两个调用方法逻辑。这没有太多可说的,唯有你自己动手去设置断点去进行调试、然后领会。