[英]How can i calculate the distance from the top of pictureBox1 and almost the top of form1?
I have a label with text inside i can change the label size or the label font size each time and check many times but maybe there is a way to calculate it: 我有一个带有文本的标签,我可以每次更改标签大小或标签字体大小并进行多次检查,但是也许有一种计算方法:
label18.Text = "מכם מזג האוויר איננו פעיל כרגע";
This is how i see the text now: 这就是我现在看到的文本:
The text in red is in hebrew this is the text i want to change it's size and also to put it in the middle according to the picturebox1 top not on the left like it is now. 红色文本是希伯来语,这是我要更改其大小并根据picturebox1顶部(而不是现在的左侧)放置在中间的文本。
And i did a black circle just to show what i mean by " the distance from the top of pictureBox1 and almost the top of form1 ". 我做了一个黑圈,目的是显示“从pictureBox1的顶部到form1的顶部的距离”的含义。 I mean this gray area from the above the pictureBox1 and the form1 white area on the top only this gray area i want to make the text in this height and in the middle.
我的意思是从pictureBox1上方的灰色区域和顶部的form1白色区域,只有此灰色区域我想在此高度和中间制作文本。 How can i calculate this two values ?
我如何计算这两个值?
I tried this but it's not in the exact middle: 我试过了,但它不在中间:
SizeF size = label18.CreateGraphics().MeasureString(label18.Text, label18.Font);
label18.Left = (pictureBox1.Width / 2) - (((int)size.Width) / 2) + pictureBox1.Left;
label18.Top = pictureBox1.Top - 20;
You don't need graphics or to measure anything. 您不需要图形或进行任何测量。 Just set in designer
text align = middlecenter
and autosize = true
只需在设计器
text align = middlecenter
设置text align = middlecenter
和text align = middlecenter
autosize = true
label18.Location = new Point(pictureBox1.Location.X + (pictureBox1.Width / 2 - label18.Width / 2,
pictureBox1.Location.Y - label18.Height);
To center a label you need it get it actual size, then to center it using another control use some simple math to get the coordinate for the control (see below Example 1). 要使标签居中,您需要获取标签的实际大小,然后使用另一个控件使它居中,请使用一些简单的数学运算来获取控件的坐标(请参见下面的示例1)。 I don't know what control the grey bar is but you could center in that by using the size.Width property and doing the same type of calculation.
我不知道灰色条是什么控件,但是您可以通过使用size.Width属性并进行相同类型的计算来居中。
If you want to fill the grey bar I have added Example 2. 如果要填充灰色栏,请添加示例2。
Example 1: 范例1:
private void CenterLabel()
{
//get the size of the text (you could do this before hand if needed)
SizeF size = label18.CreateGraphics().MeasureString(label18.Text, label18.Font);
//center over picture box control and slightly above
label18.Left = (pictureBox1.Width / 2) - (((int)size.Width) / 2) + pictureBox1.Left;
label18.Top = pictureBox1.Top - 20;
}
Example 2 例子2
private void CenterLabel()
{
int fontHeightPixels = (int)(greyBar.Height * .85);
Font font = new System.Drawing.Font("Arial", fontHeightPixels, FontStyle.Regular, GraphicsUnit.Pixel);
string text = "I am centered";
//get the size of the text (you could do this before hand if needed)
SizeF size = label18.CreateGraphics().MeasureString(text, font);
label18.Font = font;
label18.Text = text;
//center over picture box control and slightly above
label18.Left = (pictureBox1.Width / 2) - (((int)size.Width) / 2) + pictureBox1.Left;
label18.Top = (greyBar.Height / 2) - (((int)size.Height) / 2) + greyBar.Top;
}
This is relatively simple with Windows forms: 对于Windows窗体,这相对简单:
Dock
and it should be set to Top
. Dock
,应将其设置为Top
。 AutoSize
property to false
. AutoSize
属性更改为false
。 TextAlign
property to MiddleCentre
. TextAlign
属性更改为MiddleCentre
。 That should do it. 那应该做。
There's more then one way to achieve this goal. 实现此目标的方法不止一种。
I would suggest the following: 我建议以下内容:
No visual studion from where i am typing so cannot do full code example. 我键入的地方没有Visual Studio,因此无法执行完整的代码示例。
You're done. 你完成了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.