簡體   English   中英

自定義 PictureBox 控件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM