我们如何在编辑框中垂直对齐文本?

2022-01-12 00:00:00 c++ mfc

我创建了一个编辑框:

m_EditWnd.Create(m_hWnd, rect, NULL, ES_LEFT | ES_AUTOHSCROLL | WS_CHILD |
                                     WS_OVERLAPPED | WS_VISIBLE,
                 WS_EX_CLIENTEDGE | WS_EX_LEFT | WS_EX_LTRREADING);

横向对齐的样式有ES_CENTER,文本横向对齐,但我们可以将文本垂直居中对齐吗?

There is a style for horizontal alignment as ES_CENTER for horizontal text alignment, but can we align text vertically centered?

推荐答案

我没有足够的声誉来发表评论,所以这里有一个非常古老的问题的可能有用的片段!

I don't have enough reputation to make a comment, so here's a possibly useful snippet to a very old question!

如果您提供 WS_BORDER 样式,则文本会根据要求自动垂直居中.由于 OP 使用的是 WS_EX_CLIENTEDGE,所以会绘制一个边框无论如何,因此添加这种样式应该不是问题.

If you supply the WS_BORDER style then the text gets automatically vertically centred, as requested. Since the OP is using WS_EX_CLIENTEDGE, a border is drawn anyway, so adding in this style shouldn't be a problem.

但是,WS_BORDER |WS_EX_CLIENTEDGE 仅实现部分"垂直居中.

However, WS_BORDER | WS_EX_CLIENTEDGE only achieves a "partial" vertical centring.

使用 WS_BORDER |WS_EX_STATICEDGE 实现了正确的垂直居中,尽管显然我们的视觉风格与客户端边缘风格略有不同.对我来说,我可以接受这个小小的权衡,而不是编写一个自定义编辑控件.

Using WS_BORDER | WS_EX_STATICEDGE achieves the proper, vertical centring, although obviously then we have a slightly different visual style to the client-edged style. For me, I could live with this minor trade-off, rather that write a custom edit control.

相关文章