基线对齐线在自定义的WinForms控件 [英] Baseline snaplines in custom Winforms controls
问题描述
我上有一个文本框自定义用户控件,我想公开(文本框中的文本)的基线对齐线的自定义控制范围之外。我知道你创建一个设计师(自ControlDesigner继承),并覆盖对齐线来获得访问对齐线,但我不知道怎么弄,我已经通过我的自定义用户控件曝光控制的文本基线。
I have a custom user control with a textbox on it and I'd like to expose the baseline (of the text in the textbox) snapline outside of the custom control. I know that you create a designer (inherited from ControlDesigner) and override SnapLines to get access to the snaplines, but I'm wondering how to get the text baseline of a control that I have exposed by my custom user control.
推荐答案
我也有类似的需求,我解决了它是这样的:
I just had a similar need, and I solved it like this:
public override IList SnapLines
{
get
{
IList snapLines = base.SnapLines;
MyControl control = Control as MyControl;
if (control == null) { return snapLines; }
IDesigner designer = TypeDescriptor.CreateDesigner(
control.textBoxValue, typeof(IDesigner));
if (designer == null) { return snapLines; }
designer.Initialize(control.textBoxValue);
using (designer)
{
ControlDesigner boxDesigner = designer as ControlDesigner;
if (boxDesigner == null) { return snapLines; }
foreach (SnapLine line in boxDesigner.SnapLines)
{
if (line.SnapLineType == SnapLineType.Baseline)
{
snapLines.Add(new SnapLine(SnapLineType.Baseline,
line.Offset + control.textBoxValue.Top,
line.Filter, line.Priority));
break;
}
}
}
return snapLines;
}
}
此方式,它实际上是建立一个临时的子设计的子控件,以便找出真正的基准线弹线就是。
This way it's actually creating a temporary sub-designer for the subcontrol in order to find out where the "real" baseline snapline is.
这似乎在测试高性能合理的,但如果PERF成为一个问题(如果内部文本框不动),那么这个最code可以提取到初始化方法。
This seemed reasonably performant in testing, but if perf becomes a concern (and if the internal textbox doesn't move) then most of this code can be extracted to the Initialize method.
这还假定该文本框的用户控件的直接子。如果存在方式与其它布局影响控制则偏移计算变得更加复杂。
This also assumes that the textbox is a direct child of the UserControl. If there are other layout-affecting controls in the way then the offset calculation becomes a bit more complicated.
这篇关于基线对齐线在自定义的WinForms控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!