简体   繁体   中英

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?

"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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM