还是位置跟随的问题,我想要的是倒三角的尖端跟随,而不是左上角坐标跟随

就是在picturebox中加一个picturebox里面是个倒三角,父picturebox放大,子picturebox大小不变,位置也能跟随,但是只是子picturebox的left和top跟随,就是坐标确实跟随,但是我想要倒三角的尖端位置跟随,这可咋办呢?

参考GPT和自己的思路,要让倒三角的尖端跟随父PictureBox的尺寸变化,可以使用Anchor属性和重写子PictureBox的Paint事件来实现。具体步骤如下:

1 在父PictureBox中添加一个子PictureBox,将子PictureBox的大小设置为固定值,例如20x20,将Anchor属性设置为左上角和右上角(也就是上边框左右两个点)。

2 在子PictureBox的Paint事件中绘制倒三角形状。可以使用Graphics对象的DrawPolygon方法来绘制三角形,将三个顶点设置为尖端位置、左下角位置和右下角位置。

3 在父PictureBox的SizeChanged事件中重新计算尖端位置,并将位置赋值给子PictureBox的Location属性。

下面是一个C#的示例代码,可以参考实现:

private void Form1_Load(object sender, EventArgs e)
{
    // 添加父PictureBox和子PictureBox
    PictureBox parentPictureBox = new PictureBox();
    parentPictureBox.BackColor = Color.White;
    parentPictureBox.BorderStyle = BorderStyle.FixedSingle;
    parentPictureBox.Dock = DockStyle.Fill;
    parentPictureBox.SizeChanged += ParentPictureBox_SizeChanged;
    this.Controls.Add(parentPictureBox);

    PictureBox childPictureBox = new PictureBox();
    childPictureBox.BackColor = Color.Transparent;
    childPictureBox.Size = new Size(20, 20);
    childPictureBox.Anchor = AnchorStyles.Top | AnchorStyles.Right;
    parentPictureBox.Controls.Add(childPictureBox);
}

private void ParentPictureBox_SizeChanged(object sender, EventArgs e)
{
    // 获取父PictureBox和子PictureBox
    PictureBox parentPictureBox = (PictureBox)sender;
    PictureBox childPictureBox = (PictureBox)parentPictureBox.Controls[0];

    // 计算尖端位置
    int triangleX = childPictureBox.Location.X;
    int triangleY = parentPictureBox.Height - childPictureBox.Height - 1;

    // 设置尖端位置
    Point trianglePoint1 = new Point(triangleX, triangleY);
    Point trianglePoint2 = new Point(triangleX + childPictureBox.Width, triangleY);
    Point trianglePoint3 = new Point(triangleX + childPictureBox.Width / 2, parentPictureBox.Height - 1);
    Point[] trianglePoints = { trianglePoint1, trianglePoint2, trianglePoint3 };
    childPictureBox.Location = trianglePoint1;

    // 重绘子PictureBox
    childPictureBox.Invalidate();
}

private void ChildPictureBox_Paint(object sender, PaintEventArgs e)
{
    // 绘制倒三角形状
    PictureBox childPictureBox = (PictureBox)sender;
    Point trianglePoint1 = new Point(0, 0);
    Point trianglePoint2 = new Point(childPictureBox.Width, 0);
    Point trianglePoint3 = new Point(childPictureBox.Width / 2, childPictureBox.Height);
    Point[] trianglePoints = { trianglePoint1, trianglePoint2, trianglePoint3 };
    e.Graphics.FillPolygon(Brushes.Black, trianglePoints);
}

注意,上述代码中子PictureBox的BackColor属性应该设置为透明,以便显示倒三角形状。另外,应该在子PictureBox的Paint事件中实现绘制倒三角形状的代码。最后,为了触发尺寸变化事件,需要将父PictureBox的BorderStyle属性设置为FixedSingle或Fixed3D。

您可以通过计算父PictureBox和子PictureBox的尺寸和位置,以及倒三角的尖端位置,来实现倒三角尖端的位置跟随。

假设您的父PictureBox名称为parentPictureBox,子PictureBox名称为childPictureBox,倒三角名称为trianglePictureBox。以下是一些代码示例,帮助您实现所需的效果:

当父PictureBox的大小更改时,更新子PictureBox的位置:

private void parentPictureBox_Resize(object sender, EventArgs e)
{
    // 将子PictureBox的位置设置为相对于父PictureBox的位置
    childPictureBox.Left = (parentPictureBox.Width - childPictureBox.Width) / 2;
    childPictureBox.Top = (parentPictureBox.Height - childPictureBox.Height) / 2;

    // 计算倒三角的尖端位置
    int triangleTipX = trianglePictureBox.Left - childPictureBox.Left + trianglePictureBox.Width / 2;
    int triangleTipY = trianglePictureBox.Top - childPictureBox.Top + trianglePictureBox.Height;

    // 将倒三角的尖端位置设置为相对于父PictureBox的位置
    trianglePictureBox.Left = triangleTipX + parentPictureBox.Left;
    trianglePictureBox.Top = triangleTipY + parentPictureBox.Top;
}

在初始化时设置子PictureBox和倒三角的位置:

private void Form1_Load(object sender, EventArgs e)
{
    // 将子PictureBox的位置设置为相对于父PictureBox的位置
    childPictureBox.Left = (parentPictureBox.Width - childPictureBox.Width) / 2;
    childPictureBox.Top = (parentPictureBox.Height - childPictureBox.Height) / 2;

    // 计算倒三角的尖端位置
    int triangleTipX = trianglePictureBox.Left - childPictureBox.Left + trianglePictureBox.Width / 2;
    int triangleTipY = trianglePictureBox.Top - childPictureBox.Top + trianglePictureBox.Height;

    // 将倒三角的尖端位置设置为相对于父PictureBox的位置
    trianglePictureBox.Left = triangleTipX + parentPictureBox.Left;
    trianglePictureBox.Top = triangleTipY + parentPictureBox.Top;
}


请注意,这只是一些示例代码,您需要根据实际情况进行调整和修改。
参考chatgpt

该回答引用ChatGPT

要让倒三角的尖端位置跟随父 PictureBox 的放大缩小,可以使用相对位置和比例因子计算子 PictureBox 的位置。具体来说,可以使用以下步骤实现:

1、计算倒三角顶点在子 PictureBox 中的相对位置。假设倒三角是一个等腰直角三角形,可以将其顶点设为 (0, 0),底边中点设为 (0.5, 1),然后计算倒三角顶点在子 PictureBox 中的相对坐标。如果子 PictureBox 的大小是 w x h,那么倒三角顶点的相对坐标为 (0.5 * w, h)。

2、计算父 PictureBox 的宽度和高度相对于子 PictureBox 的比例因子。假设父 PictureBox 的大小为 W x H,子 PictureBox 的大小为 w x h,那么比例因子为 (W / w, H / h)。

3、使用比例因子和相对位置计算子 PictureBox 的绝对位置。子 PictureBox 的左上角位置可以通过以下公式计算:

left = parent_left + relative_left * scale_x
top = parent_top + relative_top * scale_y

4、其中,parent_left 和 parent_top 是父 PictureBox 的左上角坐标,relative_left 和 relative_top 是倒三角顶点在子 PictureBox 中的相对位置,scale_x 和 scale_y 是宽度和高度的比例因子。

5、将子 PictureBox 的位置设置为计算得到的绝对位置,并且保持子 PictureBox 的大小不变。

下面是一个示例代码,可以在父 PictureBox 的 SizeChanged 事件中调用来更新子 PictureBox 的位置:


private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
    const int triangleWidth = 30;
    const int triangleHeight = 20;

    PictureBox parent = (PictureBox)sender;
    PictureBox child = pictureBox2;

    // 计算相对位置
    int relativeLeft = child.Width / 2 - triangleWidth / 2;
    int relativeTop = child.Height - triangleHeight;

    // 计算比例因子
    float scaleX = (float)parent.Width / child.Width;
    float scaleY = (float)parent.Height / child.Height;

    // 计算绝对位置
    int left = (int)(parent.Left + relativeLeft * scaleX);
    int top = (int)(parent.Top + relativeTop * scaleY);

    // 更新子 PictureBox 的位置
    child.Location = new Point(left, top);
}

其中,triangleWidth 和 triangleHeight 分别是倒三角的宽度和高度,可以根据实际情况进行调整。

你好,请参考以下代码可以实现:

private void Form1_Load(object sender, EventArgs e)
{
    //设置PictureBox大小和位置
    pictureBox1.Size = new Size(400, 400);
    pictureBox1.Location = new Point(50, 50);

    //创建一个Bitmap对象
    Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);

    //绘制倒三角形
    using (Graphics g = Graphics.FromImage(bmp))
    {
        // 设置倒三角的尺寸和位置
        int triangleSize = 50;
        int triangleX = (pictureBox1.Width - triangleSize) / 2;
        int triangleY = (pictureBox1.Height - triangleSize) / 2;

        // 绘制倒三角形
        Point[] points = new Point[3];
        points[0] = new Point(triangleX, triangleY);
        points[1] = new Point(triangleX + triangleSize, triangleY);
        points[2] = new Point(triangleX + (triangleSize / 2), triangleY + triangleSize);
        g.FillPolygon(Brushes.Red, points);
    }

    //将Bitmap对象设置为子PictureBox的图像
    pictureBox2.Image = bmp;

    //子PictureBox的位置始终保持在父PictureBox的中心
    pictureBox2.Location = new Point((pictureBox1.Width - pictureBox2.Width) / 2, (pictureBox1.Height - pictureBox2.Height) / 2);

    //绑定父PictureBox的SizeChanged事件,以便子PictureBox始终保持在父PictureBox的中心
    pictureBox1.SizeChanged += PictureBox1_SizeChanged;
}

private void PictureBox1_SizeChanged(object sender, EventArgs e)
{
    //子PictureBox的位置始终保持在父PictureBox的中心
    pictureBox2.Location = new Point((pictureBox1.Width - pictureBox2.Width) / 2, (pictureBox1.Height - pictureBox2.Height) / 2);
}

创建了一个Bitmap对象,并在其上绘制了一个倒三角形。使用FillPolygon方法绘制倒三角形,并使用Brushes.Red来填充三角形的颜色。

然后,将这个Bitmap对象设置为子PictureBox的图像,并将子PictureBox的位置设置为父PictureBox的中心。还绑定了父PictureBox的SizeChanged事件,以便在父PictureBox的大小更改时,子PictureBox始终保持在父PictureBox的中心。

最终的效果是,当您调整父PictureBox的大小时,倒三角形的尖端始终保持在父PictureBox的中心。

回答不易,还请能够采纳!!!