nr1
nr1

Reputation: 777

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 OnKeyUp event I want to restore the original image.

Can someone tell me how to do this?

Upvotes: 0

Views: 4249

Answers (2)

Adam
Adam

Reputation: 1

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

Upvotes: 0

GSerg
GSerg

Reputation: 78155

"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);
    }
}

Upvotes: 2

Related Questions