c# - 如何在设计 View 中为自定义控件添加边框?

标签 c# .net winforms visual-studio

透明图像在 Windows 窗体中是纯粹的邪恶,这就是为什么我创建了一个自定义控件类来处理它们。当它为空时,设计器不显示我的控件。我想添加一个微妙的边框,但只能在设计 View 中添加(并且当用户没有添加边框时)。我该怎么做?

我的类(class)是:

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 }

关于c# - 如何在设计 View 中为自定义控件添加边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34580684/

相关文章:

c# - 在同一 CryptoStream 上使用 BinaryReader 和 BinaryWriter 时如何避免超时?

c# - 尝试使用 ExecuteScalar ,并得到 "Specified cast is not valid "错误

.net - My.Computer.FileSystem.DeleteDirectory() 只读文件?

c# - 如何从 C# 关闭计算机

winforms - 是否对 .exe 和 Windows 窗体应用程序中的窗体使用相同的图标而不重复?

c# - Windows窗体中多对多关系的数据输入

c# - 在 3D 空间中旋转图像的一部分

c# - Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 中存储了哪些文件?

c# - 如何分析程序内部的.exe参数?

C# - System.Transactions.TransactionScope