[转]鼠标移到图像上显示激活的例子
原文作者 java_liyi, c#+Gdi,怎么移动和改变已经绘制好的图形位置和大小
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DrawTest { public delegate void DrawMeHandler(Graphics g); public delegate void MouseMoveHandler(Point p); public partial class Canvas : UserControl { RectangleFigure rectangleFigure = new RectangleFigure(); DrawMeHandler drawMeHandler; MouseMoveHandler mouseMoveHandler; public Canvas() { InitializeComponent(); this.DoubleBuffered = true; this.Paint += Canvas_Paint; this.Load += Canvas_Load; this.MouseMove += Canvas_MouseMove; } void Canvas_MouseMove(object sender, MouseEventArgs e) { this.mouseMoveHandler.Invoke(e.Location); this.Refresh(); } void Canvas_Load(object sender, EventArgs e) { rectangleFigure.X = 10; rectangleFigure.Y = 10; rectangleFigure.Width = 100; rectangleFigure.Height = 50; drawMeHandler = new DrawMeHandler(rectangleFigure.DrawMe); mouseMoveHandler = new MouseMoveHandler(rectangleFigure.MouseMove); } void Canvas_Paint(object sender, PaintEventArgs e) { if (drawMeHandler != null) drawMeHandler.Invoke(e.Graphics); } /// <summary> /// 矩形对象 /// </summary> public class RectangleFigure { public int X { get; set; } public int Y { get; set; } public int Height { get; set; } public int Width { get; set; } public bool Actived { get; set; } public bool IsExist(Point p) { Rectangle rectangle = new Rectangle(this.X, this.Y, this.Width, this.Height); return rectangle.Contains(p); } public void MouseMove(Point p) { Actived = IsExist(p); } public void DrawMe(Graphics g) { Pen p = new Pen(Color.Black); g.FillRectangle(p.Brush, this.X, this.Y, this.Width, this.Height); if (Actived) { p.Color = Color.Red; g.DrawRectangle(p, this.X, this.Y, this.Width, this.Height); } } } } }
你们的评论、反馈,及对你们有所用,是我整理材料和博文写作的最大的鼓励和唯一动力。欢迎讨论和关注!
没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。
没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。