panel边框设置成SingleLine后只有一种颜色黑色,我想换成别的颜色,但又不想用第三方控件,请教如何实现?

解决方案 »

  1.   


    设置this.panel1.BorderStyle = BorderStyle.None;private void panel1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.DrawRectangle(new Pen(Brushes.Red), new Rectangle(0, 0, this.panel1.Width - 1, this.panel1.Height - 1));
            }
      

  2.   

    这样有个问题,当panel有滚动条,滑动滚动条时,就会重绘在错误的位置,多出很多条竖线来
      

  3.   


    public class MyPanel : Panel
        {
            Color _BorderColor = Color.Blue;        public Color BorderColor
            {
                get
                {
                    return _BorderColor;
                }
                set
                {
                    _BorderColor = value;
                }
            }        [DllImport("user32.dll")]
            static extern IntPtr GetWindowDC(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);        protected override void WndProc(ref Message m)
            {
                if (m.Msg == 0x85)
                {
                    IntPtr hdc = GetWindowDC(Handle);
                    Graphics g = Graphics.FromHdc(hdc);
                    Pen pen = new Pen(_BorderColor, 2);
                    try
                    {
                        Rectangle rect = Bounds;
                        rect.Offset(-rect.Location.X, -rect.Location.Y);
                        g.DrawRectangle(pen, rect);
                    }
                    finally
                    {
                        g.Dispose();
                        ReleaseDC(Handle, hdc);
                    }
                }
                else
                    base.WndProc(ref m);
            }
        }