[英]Custom PictureBox Control
有人可以告诉我一个如何基于图片框创建自定义控件的示例吗?
我只想要这个:如果图片框被点击( OnKeyDown
),图像应该向下移动 3 个像素,向右移动 3 个像素。 之后在OnKeyUp
事件中我想恢复原始图像。
有人可以告诉我该怎么做吗?
“被点击”是OnMouseX
,而不是OnKeyX
。
public partial class UserControl1 : PictureBox
{
public UserControl1()
{
InitializeComponent();
}
private bool shifted = false;
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && this.Image != null)
{
this.shifted = true;
this.Invalidate();
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && this.Image != null)
{
this.shifted = false;
this.Invalidate();
}
base.OnMouseUp(e);
}
protected override void OnPaint(PaintEventArgs pe)
{
if (this.shifted)
{
pe.Graphics.TranslateTransform(3, 3, System.Drawing.Drawing2D.MatrixOrder.Append);
}
base.OnPaint(pe);
}
}
我知道这是一个旧帖子,但我找到了它并且非常有用。 但是我花了大约一个工作日来解决一个问题,即我的 DrawRectangle 被绘制在我加载的图像下方。 解决方案是移动base.OnPaint(pe);
OnPaint
方法开头的方法。
希望这可以帮助。
亚当
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.