在Panel中未绘制TextBox边框

时间:2018-04-01 19:08:55

标签: c# winforms

我几个小时以来一直在搞乱这种Paint方法。这是我能想到的最干净/最接近的解决方案。我甚至试过制作一个自定义TextBox控件,但我认为这超出了我目前的技能组合。仅供参考我是VS和C#的新手,所以请放轻松。

{{1}}

2 个答案:

答案 0 :(得分:0)

您无法看到绘图,因为您的坐标已关闭,因为您混合了两件事:

  • 您在Form.Paint事件中绘图,但是..
  • 您使用Textboxes' Locations,与他们所在的Panel相关。

你可以

  • 使用Panel.Paint事件或..
  • 计算相对于TextBox的{​​{1}}位置。

对于后一种解决方案,您可以使用Form功能。请考虑边框样式;另请注意,Rectangle.Offset(Point)仅为边框内的区域 ..

第一个解决方案还假设ClientSize是透明的,没有其他控件位于PanelPanel之间!

这对我有用:

Form

另请注意,rect = new Rectangle(textBox3.Left - 1, textBox3.Top - 1, textBox3.Width + 1, textBox3.Height + 1); rect.Offset(panel1.Location); 不仅Rectangle.Inflate(a,b) Width增加aa*2增加Lefta增加removeButton3等。< / p>

最后注意事项:请注意,有difference of how DrawRectangle and FillRectangle处理要绘制或填充的大小!

答案 1 :(得分:-1)

https://github.com/vignesh-nethaji/Windows-Custom-Controls/blob/master/Menporul.Windows/Menporul.Windows.Controls/Controls/MenTextBox.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace Menporul.Windows.Controls
{
    /// <summary>
    /// Menporul TextBox
    /// </summary>
    /// <author>Vignesh Nethaji</author>
    public class MenTextBox : TextBox
    {
        /// <summary>
        /// Border Color when textbox focus
        /// </summary>
        private Color _focusBorderColor = Color.Blue;
        /// <summary>
        /// Default Border Color 
        /// </summary>
        private Color _defaultBorderColor = Color.Gray;
        /// <summary>
        /// 
        /// </summary>
        public MenTextBox()
        {


        }
        /// <summary>
        /// On control resize refresh the control for Border rendering based on the control size
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.Refresh();
        }
        /// <summary>
        /// Getting device context for creating graphics for draw border on textbox
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        [DllImport("user32")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);
        /// <summary>
        /// Draw textbox border when its painting 
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0x85 || m.Msg == 0x000F)
            {
                Rectangle rect = new Rectangle(0, 0, Width, Height);
                if (this.Focused)
                {

                    var dc = GetWindowDC(Handle);
                    using (Graphics g = Graphics.FromHdc(dc))
                    {
                        ControlPaint.DrawBorder(g, rect,
                       _focusBorderColor, 2, ButtonBorderStyle.Solid,
                       _focusBorderColor, 2, ButtonBorderStyle.Solid,
                       _focusBorderColor, 2, ButtonBorderStyle.Solid,
                       _focusBorderColor, 2, ButtonBorderStyle.Solid);
                    }
                }
                else
                {

                    var dc = GetWindowDC(Handle);
                    using (Graphics g = Graphics.FromHdc(dc))
                    {
                        ControlPaint.DrawBorder(g, rect,
                       _defaultBorderColor, 1, ButtonBorderStyle.Solid,
                       _defaultBorderColor, 1, ButtonBorderStyle.Solid,
                       _defaultBorderColor, 1, ButtonBorderStyle.Solid,
                       _defaultBorderColor, 1, ButtonBorderStyle.Solid);
                    }
                }
            }

        }
    }
}