[英]Custom PictureBox Control
Could someone please show me a example of how to create a custom control based on a picturebox?有人可以告诉我一个如何基于图片框创建自定义控件的示例吗?
I just want this: If the picturebox gets clicked ( OnKeyDown
) the image should be moved by 3 pixels down and 3px to the right.我只想要这个:如果图片框被点击(
OnKeyDown
),图像应该向下移动 3 个像素,向右移动 3 个像素。 Afterwards at an OnKeyUp
event I want to restore the original image.之后在
OnKeyUp
事件中我想恢复原始图像。
Can someone tell me how to do this?有人可以告诉我该怎么做吗?
"Gets clicked" is OnMouseX
, not OnKeyX
. “被点击”是
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);
}
}
I know it is an old post but I have found it and was very usefull.我知道这是一个旧帖子,但我找到了它并且非常有用。 But I spent about one working day with solving an issue that my DrawRectangle was drawn below image that I loaded.
但是我花了大约一个工作日来解决一个问题,即我的 DrawRectangle 被绘制在我加载的图像下方。 The solution was to move
base.OnPaint(pe);
解决方案是移动
base.OnPaint(pe);
method on the begining of OnPaint
method. OnPaint
方法开头的方法。
Hope this helps.希望这可以帮助。
Adam亚当
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.