C# 我怎样才能找到黄线和绿线之间的距离?

C# 我怎样才能找到黄线和绿线之间的距离?,c#,.net,winforms,C#,.net,Winforms,黄线表示menuStrip1控件的下边框 绿色表示pictureBox1控件的顶部 我想计算两个控件之间的空间(与黄色和绿色两行之间的空间相同) 这就是我在pictureBox1绘画活动中所做的: private void pictureBox1_Paint(object sender, PaintEventArgs e) { int distance; float penWidth = 15F; Pen m

黄线表示menuStrip1控件的下边框

绿色表示pictureBox1控件的顶部

我想计算两个控件之间的空间(与黄色和绿色两行之间的空间相同)

这就是我在pictureBox1绘画活动中所做的:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            int distance;
            float penWidth = 15F;
            Pen myPen = new Pen(Brushes.Green, (int)penWidth);
            Pen myPen1 = new Pen(Brushes.Red, (int)penWidth);
            e.Graphics.DrawRectangle(myPen, 0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1);
            e.Graphics.DrawLine(myPen1, 0, pictureBox1.Height, pictureBox1.Width, pictureBox1.Height);
            e.Graphics.DrawLine(myPen1, 0,0,0,pictureBox1.Height);
            e.Graphics.DrawLine(myPen1, pictureBox1.Width, 0, pictureBox1.Width, pictureBox1.Height);
            distance = menuStrip1.Height - pictureBox1.Height;
        }
private void menuStrip1_Paint(object sender, PaintEventArgs e)
        {
            float penWidth = 15F;
            Pen myPen1 = new Pen(Brushes.Yellow, (int)penWidth);
            e.Graphics.DrawLine(myPen1, 0, menuStrip1.Height, menuStrip1.Width, menuStrip1.Height);
        }
这是menuStrip1绘画活动:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            int distance;
            float penWidth = 15F;
            Pen myPen = new Pen(Brushes.Green, (int)penWidth);
            Pen myPen1 = new Pen(Brushes.Red, (int)penWidth);
            e.Graphics.DrawRectangle(myPen, 0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1);
            e.Graphics.DrawLine(myPen1, 0, pictureBox1.Height, pictureBox1.Width, pictureBox1.Height);
            e.Graphics.DrawLine(myPen1, 0,0,0,pictureBox1.Height);
            e.Graphics.DrawLine(myPen1, pictureBox1.Width, 0, pictureBox1.Width, pictureBox1.Height);
            distance = menuStrip1.Height - pictureBox1.Height;
        }
private void menuStrip1_Paint(object sender, PaintEventArgs e)
        {
            float penWidth = 15F;
            Pen myPen1 = new Pen(Brushes.Yellow, (int)penWidth);
            e.Graphics.DrawLine(myPen1, 0, menuStrip1.Height, menuStrip1.Width, menuStrip1.Height);
        }
结果是:


在pictureBox1绘制事件和menuStrip1绘制事件中,我用红绿色和黄色绘制的线条显示的是什么:宽度或高度、左侧或右侧、顶部或底部?我画了红线手册,也画了绿色和黄色,但我怎么知道底线是底部,是右边还是左边?

如果你想计算两个对象之间的空间,逻辑很简单,你必须访问对象的y位置,然后在两个位置picturebox.y-menustrip.y之间进行分割

我刚刚为您编写了简单的代码

        int x = menuStrip1.Location.Y;
        int p = pictureBox1.Location.Y;
        int result = p - x;
        MessageBox.Show(result.ToString());

我刚刚测试了这些代码,效果很好祝你好运伙计

两个控件之间的距离是:
PictureBox1.Top-MenuStrip1.Bottom
。你在找不同的东西吗?