如何使用自定义颜色为 MaskedTextBox 绘制边框?
How to draw border for MaskedTextBox using custom color?
我正在尝试创建一个带有边框颜色的蒙版文本框。
我尝试了下面的代码来实现它:
public class MaskedTextBoxWithBorder : UserControl
{
MaskedTextBox maskedtextBox;
public MaskedTextBoxWithBorder()
{
maskedtextBox = new MaskedTextBox()
{
BorderStyle = BorderStyle.FixedSingle,
Location = new Point(-1, -1),
Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right
};
Control container = new ContainerControl()
{
Dock = DockStyle.Fill,
Padding = new Padding(-1)
};
container.Controls.Add(maskedtextBox);
this.Controls.Add(container);
DefaultBorderColor = SystemColors.ControlDark;
FocusedBorderColor = Color.Red;
BackColor = DefaultBorderColor;
Padding = new Padding(1);
Size = maskedtextBox.Size;
}
public Color DefaultBorderColor { get; set; }
public Color FocusedBorderColor { get; set; }
public override string Text
{
get
{
return maskedtextBox.Text;
}
set
{
maskedtextBox.Text = value;
}
}
protected override void OnEnter(EventArgs e)
{
BackColor = FocusedBorderColor;
base.OnEnter(e);
}
protected override void OnLeave(EventArgs e)
{
BackColor = DefaultBorderColor;
base.OnLeave(e);
}
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width, maskedtextBox.PreferredHeight, specified);
}
}
但问题是它没有屏蔽文本框的所有功能,如设置屏蔽类型等
所以我像这样更改了我的代码:
public class MaskedTextBoxWithBorder : UserControl
现在我拥有蒙版文本框的所有功能,但边框颜色不受影响。
有没有什么方法可以扩展 Masked 文本框以获得边框样式而不丢失类似这样的功能,这是不可能的。
public class MaskedTextBoxWithBorder : UserControl, MaskedTestBox
TextBox 和 MaskedTextBox 控件只是 Win32 TextBox 控件的包装器,因此所有者绘制它们(用于自定义边框、叠加层或其他任何内容)比正常情况下要复杂一些。这是您应该做的事情,以实现您想要做的事情。
- 从 MaskedTextBox 派生:
public class MaskedTextBoxWithBorder : MaskedTextBox
- 获得对 Win32 TextBox 消息流的访问权限(它会自行绘制以响应多条消息,因此您需要捕获所有消息,而不仅仅是标准 WM_PAINT 消息)。
- 获取设备上下文句柄并将其转换为托管图形对象以绘制边框。
查看下面详细解释每个步骤的文章:Adding an Icon or Control to a TextBox or ComboBox
尽管本文讨论了基本的 TextBox 控件,但这并不重要。 TextBox 和 MaskedTextBox 都派生自 TextBoxBase class,它实现了我们感兴趣的所有重要部分。
绘制 MaskedTextBox
you should override WndProc
and handle WM_NCPAINT
消息的边框。然后获取控件的 window dc 并从该 dc 创建一个 Graphics
对象,然后绘制边框 control.This 解决方案也已在 ToolStripTextBox
中使用。该解决方案也可以应用于 TextBox
。
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyMaskedTextBox : MaskedTextBox
{
public const int WM_NCPAINT = 0x85;
[DllImport("user32.dll")]
public 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)
{
base.WndProc(ref m);
if (m.Msg == WM_NCPAINT)
{
var hdc = GetWindowDC(this.Handle);
using (var g = Graphics.FromHdcInternal(hdc))
{
g.DrawRectangle(Pens.Blue, new Rectangle(0, 0, Width - 1, Height - 1));
}
ReleaseDC(this.Handle, hdc);
}
}
}
嗯,通常在给定的应用程序中,您只需更改一些 属性,因此您可以简单地向用户控件添加一些额外的 属性 以获取您希望能够更改的那些属性。
public class MaskedTextBoxWithBorder : UserControl
{
MaskedTextBox maskedtextBox;
// Other existing code...
public string Mask
{
get { return maskedtextBox.Mask; }
set { maskedtextBox.Mask = value; }
}
// Do same thing for other properties you want to change...
}
如果您真的想更改很多属性,那么其他解决方案可能更合适...
我正在尝试创建一个带有边框颜色的蒙版文本框。
我尝试了下面的代码来实现它:
public class MaskedTextBoxWithBorder : UserControl
{
MaskedTextBox maskedtextBox;
public MaskedTextBoxWithBorder()
{
maskedtextBox = new MaskedTextBox()
{
BorderStyle = BorderStyle.FixedSingle,
Location = new Point(-1, -1),
Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right
};
Control container = new ContainerControl()
{
Dock = DockStyle.Fill,
Padding = new Padding(-1)
};
container.Controls.Add(maskedtextBox);
this.Controls.Add(container);
DefaultBorderColor = SystemColors.ControlDark;
FocusedBorderColor = Color.Red;
BackColor = DefaultBorderColor;
Padding = new Padding(1);
Size = maskedtextBox.Size;
}
public Color DefaultBorderColor { get; set; }
public Color FocusedBorderColor { get; set; }
public override string Text
{
get
{
return maskedtextBox.Text;
}
set
{
maskedtextBox.Text = value;
}
}
protected override void OnEnter(EventArgs e)
{
BackColor = FocusedBorderColor;
base.OnEnter(e);
}
protected override void OnLeave(EventArgs e)
{
BackColor = DefaultBorderColor;
base.OnLeave(e);
}
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width, maskedtextBox.PreferredHeight, specified);
}
}
但问题是它没有屏蔽文本框的所有功能,如设置屏蔽类型等
所以我像这样更改了我的代码:
public class MaskedTextBoxWithBorder : UserControl
现在我拥有蒙版文本框的所有功能,但边框颜色不受影响。
有没有什么方法可以扩展 Masked 文本框以获得边框样式而不丢失类似这样的功能,这是不可能的。
public class MaskedTextBoxWithBorder : UserControl, MaskedTestBox
TextBox 和 MaskedTextBox 控件只是 Win32 TextBox 控件的包装器,因此所有者绘制它们(用于自定义边框、叠加层或其他任何内容)比正常情况下要复杂一些。这是您应该做的事情,以实现您想要做的事情。
- 从 MaskedTextBox 派生:
public class MaskedTextBoxWithBorder : MaskedTextBox
- 获得对 Win32 TextBox 消息流的访问权限(它会自行绘制以响应多条消息,因此您需要捕获所有消息,而不仅仅是标准 WM_PAINT 消息)。
- 获取设备上下文句柄并将其转换为托管图形对象以绘制边框。
查看下面详细解释每个步骤的文章:Adding an Icon or Control to a TextBox or ComboBox
尽管本文讨论了基本的 TextBox 控件,但这并不重要。 TextBox 和 MaskedTextBox 都派生自 TextBoxBase class,它实现了我们感兴趣的所有重要部分。
绘制 MaskedTextBox
you should override WndProc
and handle WM_NCPAINT
消息的边框。然后获取控件的 window dc 并从该 dc 创建一个 Graphics
对象,然后绘制边框 control.This 解决方案也已在 ToolStripTextBox
中使用。该解决方案也可以应用于 TextBox
。
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyMaskedTextBox : MaskedTextBox
{
public const int WM_NCPAINT = 0x85;
[DllImport("user32.dll")]
public 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)
{
base.WndProc(ref m);
if (m.Msg == WM_NCPAINT)
{
var hdc = GetWindowDC(this.Handle);
using (var g = Graphics.FromHdcInternal(hdc))
{
g.DrawRectangle(Pens.Blue, new Rectangle(0, 0, Width - 1, Height - 1));
}
ReleaseDC(this.Handle, hdc);
}
}
}
嗯,通常在给定的应用程序中,您只需更改一些 属性,因此您可以简单地向用户控件添加一些额外的 属性 以获取您希望能够更改的那些属性。
public class MaskedTextBoxWithBorder : UserControl
{
MaskedTextBox maskedtextBox;
// Other existing code...
public string Mask
{
get { return maskedtextBox.Mask; }
set { maskedtextBox.Mask = value; }
}
// Do same thing for other properties you want to change...
}
如果您真的想更改很多属性,那么其他解决方案可能更合适...