依赖于字体的控制定位

发布于 2024-07-04 18:45:10 字数 822 浏览 4 评论 0原文

我想在 Vista 上使用 Segoe UI 9 pt,在 Windows XP 等上使用 Tahoma 8 pt。 (实际上,我会在两者上使用 Segoe UI,但我的用户可能没有安装它。)但是,这些完全不同,它们确实搞乱了我的表单布局。 那么……有什么好的办法来解决这个问题吗?

举个例子:我有一个 Label,中间有一些空白,我在其中放置了一个 NumericUpDown 控件。 如果我使用 Segoe UI,与使用 Tahoma 时相比,NumericUpDown 距离空白左侧大约 5 个像素左右。 这是一种痛苦; 我不知道在这里做什么。

因此,最具体地说,我的问题是:如何将控件放置在 Label(或 CheckBoxes 等)中的空白区域中间? 最常见的是:有没有一种好的方法来处理 Windows 窗体中的不同字体?

编辑:我认为人们不理解这个问题。 我知道如何根据操作系统改变我的字体。 我只是不知道如何处理由此产生的布局问题

回复ajryan,quick_dry:好的,你们明白这个问题了。 我想 MeasureString 可能会起作用,尽管我有兴趣进一步探索更好的方法来解决这个问题。

拆分控件的问题在 CheckBox 中最为明显。 在那里,如果用户单击 CheckBox 的“后半部分”(我猜这将是一个单独的 Label 控件),CheckBox 不会更改状态。

I'd like to use Segoe UI 9 pt on Vista, and Tahoma 8 pt on Windows XP/etc. (Actually, I'd settle for Segoe UI on both, but my users probably don't have it installed.) But, these being quite different, they really screw up the layout of my forms. So... is there a good way to deal with this?

An example: I have a Label, with some blank space in the middle, into which I place a NumericUpDown control. If I use Segoe UI, the NumericUpDown is about 5 pixels or so to the left of the blank space, compared to when I use Tahoma. This is a pain; I'm not sure what to do here.

So most specifically, my question would be: how can I place controls in the middle of a blank space in my Labels (or CheckBoxes, etc.)? Most generally: is there a good way to handle varying fonts in Windows Forms?

Edit: I don't think people understood the question. I know how to vary my fonts based on OS. I just don't know how to deal with the layout problems that arise from doing so.

Reply to ajryan, quick_dry: OK, you guys understand the question. I guess MeasureString might work, although I'd be interested in further exploration of better ways to solve this problem.

The problem with splitting the control is most apparent with, say, a CheckBox. There, if the user clicks on the "second half" of the CheckBox (which would be a separate Label control, I guess), the CheckBox doesn't change state.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤凫 2024-07-11 18:45:10

问题出在控制装置的放置上吗? 也就是说,您知道字体 X 和 Y 在操作系统 A 和 B 上工作,并为您在这些系统上使用的文本提供您想要的布局?

MeasureString 方法可能有助于以不受特定字体束缚的方式制定布局。

float textWidth = Graphics.MeasureString(someString, someFont).Width;

(文本对齐方式的改变会起作用吗?我也可能误解了这个问题)

is the problem working out the placement of controls? i.e. you know font X and Y work on OS A and B, and give the layout you want with the text you're using on those systems?

MeasureString method might help in working out your layout in a way that you weren't tied to specific fonts.

float textWidth = graphics.MeasureString(someString, someFont).Width;

(would a change in text alignment work? I might be misunderstanding the problem too)

我只土不豪 2024-07-11 18:45:10

需要将一个控件布局在另一个控件中,这很奇怪。 您可能错误地解决了上游问题。 您是否能够将标签拆分为两个标签,其间为上下,并且可能依赖于 Windows 窗体 TableLayout 面板?

如果必须尝试根据字体大小进行定位,则可以使用 Graphics.MeasureString ("String before updown", myLabel.Font)

如果您想要的是依赖于字体的控件定位,那么您可能应该重新命名问题。


[edit] You can handle the click event of the "second half" part of the label and change the checkbox state on that event. The whole thing seems like a hack though. What is the problem being solved by this weird control layout? Why do you need an up-down in the middle of a label?

It's strange to need to layout one control within another. You might be solving an upstream problem wrong. Are you able to split the label into two labels with the updown between and maybe rely on a Windows Forms TableLayout panel?

If it's essential to try to position based on font sizes, you could use Graphics.MeasureString("String before updown", myLabel.Font)

If what you're after is font-dependent control positioning, you should probably retitle the question.


[edit] You can handle the click event of the "second half" part of the label and change the checkbox state on that event. The whole thing seems like a hack though. What is the problem being solved by this weird control layout? Why do you need an up-down in the middle of a label?

锦爱 2024-07-11 18:45:10

首先,您可以通过 OperatingSystem.Platform 属性。

其次,您可以将字体设置放在资源文件中,并根据某些条件(例如您的操作系统版本)确定使用哪个资源文件。

但就我个人而言,我认为让您的用户确定他们喜欢的字体而不是您希望他们使用的字体会很好。

最后,您可能想看看WPF,因为这是它旨在解决的问题空间之一。

First of all, you can find out which version of Windows you are using with the OperatingSystem.Platform property in the System library.

Second, it is possible that you may put your font settings in Resource files, and determine which resource file to use depending on certain conditions (e.g., your operating system version).

Personally though, I think it would be nice to let your user determine the fonts that they prefer as opposed to the font that you want for them to use.

Finally, you might want to take a look at WPF as this is one of the problem spaces that it was designed to solve.

热鲨 2024-07-11 18:45:10

我赞同将 TableLayoutPanel 用于单行内联控件。

我通常在设计器中将每一列和第一行设置为 AutoSize,并将每个子控件的 Dock 属性设置为 Fill。 这使得水平布局能够正确显示。
要使文本在标签/文本框之间对齐,请将 TextAlign 属性设置为 MiddleLeft。

如果您的文本流到下一行,则没有简单的解决方案。 使用 Graphics.MeasureString/TextRenderer.MeasureText 和一些奇特的包装逻辑是你最好的选择:(

I second the usage of TableLayoutPanel for single-line inline controls.

I usually set each column and the first row to AutoSize and set each child control's Dock property to Fill in the designer. That gets the horizontal layout to display properly.
To make the the text line up between labels/textboxes, set the TextAlign property to MiddleLeft.

If your text flows onto to the next line there's no easy solution. Using Graphics.MeasureString/TextRenderer.MeasureText and some fancy wrapping logic is your best bet :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文