简体   繁体   English

我如何计算从pictureBox1的顶部到form1的顶部的距离?

[英]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 = middlecentertext 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窗体,这相对简单:

  1. Dock your label to the top of the form by setting the appropriate property in the Forms designer. 通过在窗体设计器中设置适当的属性,将标签停靠在窗体的顶部。 The property you want to set is Dock and it should be set to Top . 您要设置的属性是Dock ,应将其设置为Top
  2. Change the label's AutoSize property to false . 将标签的AutoSize属性更改为false
  3. Change the label's height as desired. 根据需要更改标签的高度。
  4. Change the label's 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: 我建议以下内容:

  1. First calculate the width of the picturebox (picturebox.Width) 首先计算图片框的宽度(picturebox.Width)
  2. Find the coordinates on the form where the picturebox resides (picturebox.Location) property of the picturebox) 在图片框所在的表单上找到图片框的坐标(picturebox.Location)(图片框的属性)
  3. Then you change the location of your label control --> to Label.Location.X = (picturebox.Width /2) and Label.Location.Y = picturebox.Location.Y ==> now you have the label correctly placed . 然后,将标签控件->的位置更改为Label.Location.X =(picturebox.Width / 2)和Label.Location.Y = picturebox.Location.Y ==>现在已经正确放置了标签。
    1. Next Set the Height of the Label Control to the Top(distance between the edge of the form and picturebox) value of the Picturebox. 将“标签控件”的“高度”设置为“图片框”的“顶部”(窗体边缘和图片框之间的距离)值。

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.

相关问题 从多个类中调用Form1上的pictureBox1的最佳方法是什么? - What is the best way to call pictureBox1 on Form1 from a number of classes? 如何将pictureBox1中的图像替换为pictureBox1矩形区域上绘制的裁剪图像? - How can i replace the image in pictureBox1 with a crop image of a drawn on the pictureBox1 rectangle area? 如何将图片从PictureBox1复制到Excel? - How to copy image from PictureBox1 to excel? 如何在将鼠标移到pictureBox1上时自动在pictureBox2上绘制点? - How can i make that when i move the mouse ober pictureBox1 it will draw points automatic on pictureBox2? 如何将图像从Form1分配给Form2 Picturebox - How to assign a image From Form1 to Form2 Picturebox 如何使计时器向前或向后更改pictureBox1中显示的图像? - How can I make timer that will change the images display in pictureBox1 forward or backward? 如果用户处于鼠标按下事件中,如何检查pictureBox1鼠标离开事件? - How can i check on pictureBox1 mouse leave event if the user is in mouse down event? 我如何从Form2调用参数为Form1的方法并在Form1的图表上绘图 - How can i call a method with parameter of form1 from form2 and plot on chart on form1 我无法在我指定的文件夹中保存从 pictureBox1 拍摄的照片 - I can't save a photo taken from pictureBox1 in the folder I specified 如何在Form2中使用Form1中的变量? - How can I use variables from Form1 in Form2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM