设计的时候在界面上拉了个pictureBox的控件,想在运行的时候把pictureBox的边框编程虚线,
  Rectangle rec = new Rectangle();
            rec.Size = new Size(1this.pictureBox1.Width, this.pictureBox1.Height);
            rec.Location = this.PointToClient(new Point(this.pictureBox1.Location.X, this.pictureBox1.Location.Y));
            ControlPaint.DrawReversibleFrame(rec, Color.Red, FrameStyle.Dashed);
运行的时候线的位置不对 是在左上角的貌似。

解决方案 »

  1.   

    矩形框 长宽 最好 比 picbox 的长宽 大一个单位
    之后 再试试
    Location  就是左上角的坐标
      

  2.   

    rect最好在onpaint里构造。因为你如果在某个事件指定了矩形区域,后续的pic的size被修改。你就会出现现在的情况了。
      

  3.   

    Graphics gc = this.CreateGraphics();
                Pen pen = new Pen(Color.Red, 30);
                gc.DrawRectangle(pen, this.pictureBox1.Location.X, this.pictureBox1.Location.Y, pictureBox1.Width, pictureBox1.Height);
      

  4.   

    protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                Rectangle rec = new Rectangle();
                rec.Size = new Size(this.pictureBox1.Width + 200, this.pictureBox1.Height + 200);
                rec.Location = pictureBox1.PointToClient(new Point(this.pictureBox1.Location.X, this.pictureBox1.Location.Y));
                ControlPaint.DrawReversibleFrame(rec, Color.Red, FrameStyle.Dashed);
                
            }
            private void pictureBox1_DoubleClick(object sender, EventArgs e)
            {
     this.Invalidate();}还是不对啊 ,不知道该怎么调用啊。。
      

  5.   

    不知为什么,使用楼主的 ControlPaint.DrawReversibleFrame 方法绘制总是看不到线。
    我在主窗体的Paint事件里实现了绘制线的功能。        private void PictureBoxTest_Paint(object sender, PaintEventArgs e)
            {
                Rectangle rect = new Rectangle();
                rect.Size = new Size(pictureBox1.Width + 1, pictureBox1.Height + 1);
                rect.Location = new Point(pictureBox1.Location.X - 1, pictureBox1.Location.Y - 1);
                
                //ControlPaint.DrawReversibleFrame(rect, Color.Red, FrameStyle.Dashed);            Pen pen = new Pen(Color.Red);
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                e.Graphics.DrawRectangle(pen, rect);
            }
      

  6.   

    你的panel是直接放在Form里的吗?还是说放在其它控件中的?如果是直接在Form里,你看看下面的代码能用不
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                Int32 lineWidth = 5;
                Rectangle rect =
                    new Rectangle(
                        panel1.Location.X - lineWidth + 1,
                        panel1.Location.Y - lineWidth + 1,
                        panel1.Width + lineWidth,
                        panel1.Height + lineWidth
                        );            using (Pen p = new Pen(Color.Black, lineWidth))
                {
                    p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                    p.DashOffset = 2.0f;
                    e.Graphics.DrawRectangle(p, rect);
                }
            }
        }