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. Afterwards at an On开发者_开发问答KeyUp
event I want to restore the original image.
Can someone tell me how to do this?
"Gets clicked" is OnMouseX
, not 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.
The solution was to move base.OnPaint(pe);
method on the begining of OnPaint
method.
Hope this helps.
Adam
精彩评论