简体   繁体   English

如何将图片框的边框置于前面?

[英]How to Bring the Border of a Picturebox to Front?

I am making a border around a picture box by simply drawing a rectangle around it. 我通过在图片框周围绘制一个矩形来在图片框周围制作边框。 However since there is a panel behind the picturebox, I cannot see the border around the picturebox (despite the fact that I have drawn the border around the picture. Here's the code: 但是,由于在图片框后面有一个面板,所以我看不到图片框周围的边框(尽管我已经在图片周围绘制了边框。这是代码:

private void Form1_Load(object sender, EventArgs e)
    {
        this.WindowState = FormWindowState.Maximized;
        Graphics objGraphics = null;
        objGraphics = this.CreateGraphics();
        objGraphics.Clear(SystemColors.Control);
        objGraphics.DrawRectangle(Pens.Blue,
             ileriresmi.Left - 1, ileriresmi.Top - 1,
              ileriresmi.Width + 1, ileriresmi.Height + 1);
        objGraphics.Dispose();
    }

There are a couple things wrong here. 这里有几处错误。 First, you're doing your drawing on the form, which is covered by the panel you mention, and second, you're only drawing the border once, in the Load event, rather than every time the form receives a WM_PAINT message. 首先,您要在窗体上进行绘制,该窗体由您提到的面板覆盖,其次,您只在Load事件中绘制了一次边框,而不是每次窗体收到WM_PAINT消息时都绘制了边框。

See here for an explanation of why the latter is wrong. 请参阅此处以解释为什么后者是错误的。

As for getting the border drawn in the right place, why not just set the BackColor of the panel that holds the PictureBox to Color.Blue and give that panel a nonzero value for Padding ? 至于在正确的位置绘制边框,为什么不只是将保存PictureBox的面板的BackColor设置为Color.Blue并为该面板赋予非零Padding值呢? (Or, if the panel contains other controls, add an intermediate panel just for the border.) (或者,如果面板包含其他控件,则为边框添加一个中间面板。)

you may try this..but it will draw inside the picture box 你可以试试这个..但是它会画在图片框里面

 private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {


            e.Graphics.DrawRectangle(new Pen(Color.Red, 2f),0,0,pictureBox1.Size.Width-2, pictureBox1.Size.Height-2);


        }

Complete solution will be like this. 完整的解决方案将是这样。 add this class into your application and build application. 将此类添加到您的应用程序并构建应用程序。

  public class PicExt : PictureBox
    {
        private Color _borderColor;
        private int _borderWidth;
        [Browsable(true)]
        public Color BorderColor { 
            get { return _borderColor; } 
            set { _borderColor = value; this.Invalidate(); } 
        }
        [Browsable(true)]
        public int BorderWidth {
            get { return _borderWidth; }
            set { _borderWidth = value; this.Invalidate(); }
        }

        public PicExt()
        {
            _borderColor = Color.Red;
            _borderWidth = 3;

        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            pe.Graphics.DrawRectangle(new Pen(BorderColor, BorderWidth), BorderWidth, BorderWidth, this.Size.Width - (BorderWidth*2), this.Size.Height - (BorderWidth*2));
        }
    }

after building application you will see new control "PicExt" in your toolbox. 构建应用程序后,您将在工具箱中看到新的控件“ PicExt”。 replace pictureBox with PicExt Control and subscribe the click event like this. 用PicExt Control替换pictureBox并订阅这样的click事件。

 private void button1_Click(object sender, EventArgs e)
        {
            //set the color here
            picExt1.BorderColor = Color.Red;
            //and frame width 
            picExt1.BorderWidth = 5;
        }

it should work like exactly you want. 它应该像您想要的那样工作。

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

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