简体   繁体   English

如何在设计视图中为自定义控件添加边框?

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

Transparent images are pure evil in Windows Forms, that why I've created a custom control class to handle them. 透明图像在Windows窗体中是纯粹的邪恶,这就是为什么我创建了一个自定义控件类来处理它们。 The designer doesn't show my control when it's empty. 设计师在空的时候不显示我的控制权。 I would like to add a subtle border, but only in the design view (and when no border is added by the user). 我想添加一个微妙的边框,但只在设计视图中(当用户没有添加边框时)。 How would I do this? 我该怎么做?

My class is: 我的班级是:

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

Check if (this.DesignMode) in your OnPaint and call DrawRectangle . 检查OnPaint if (this.DesignMode)并调用DrawRectangle

You may want to use a new Pen(SystemColors.ActiveBorder) { DashStyle = DashStyle.Dot } 您可能想要使用new Pen(SystemColors.ActiveBorder) { DashStyle = DashStyle.Dot }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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