簡體   English   中英

如何在設計視圖中為自定義控件添加邊框?

[英]How to add a border to a custom control in the design view?

透明圖像在Windows窗體中是純粹的邪惡,這就是為什么我創建了一個自定義控件類來處理它們。 設計師在空的時候不顯示我的控制權。 我想添加一個微妙的邊框,但只在設計視圖中(當用戶沒有添加邊框時)。 我該怎么做?

我的班級是:

class TransparentImage : Control
{
    public Image Image { get; set; }

    protected Graphics graphics;

    public string FilePath { get; set; }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT

            return cp;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        // Don't paint background
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // Update the private member so we can use it in the OnDraw method
        this.graphics = e.Graphics;

        // Set the best settings possible (quality-wise)
        this.graphics.TextRenderingHint =
            System.Drawing.Text.TextRenderingHint.AntiAlias;
        this.graphics.InterpolationMode =
            System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
        this.graphics.PixelOffsetMode =
            System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        this.graphics.SmoothingMode =
            System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        if (Image != null)
        {
            // Sets the images' sizes and positions
            var width = Image.Size.Width;
            var height = Image.Size.Height;
            var size = new Rectangle(0, 0, width, height);

            // Draws the two images
            this.graphics.DrawImage(Image, size);
        }
    }
}

檢查OnPaint if (this.DesignMode)並調用DrawRectangle

您可能想要使用new Pen(SystemColors.ActiveBorder) { DashStyle = DashStyle.Dot }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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