一个图片框问题 [英] A PictureBox Problem

查看:23
本文介绍了一个图片框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题:

我有 3 个带有 3 个不同图像的图片框如图像

I have 3 picture boxes with 3 different images as in Image

我可以将什么设置为 pictureBox3 以便两个图像看起来相同.....

what can i set to pictureBox3 so both images look same.....

我想在pictureBox2上移动pictureBox3,

EDITED: I want to move pictureBox3 on pictureBox2,

因此没有将它们合并为单个图像的选项

So there is no Option to merge them to single image

推荐答案

我将添加另一个示例,根据更新的要求允许移动图像3.
为了让它工作,在 Resources ransp.png
中放置一个透明的图像这对所有三个图像使用相同的图像,但您可以简单地将 image1 和 image2 的 transparentImg 替换为合适的图像.

I'll add another example that according to the updated requirement allows for moving image3.
To get it working, put an image with transparency in Resources ransp.png
This uses the same image for all three images, but you can simply replace transparentImg for image1 and image2 to suitable images.

演示开始后,中间的图像可以在表单周围拖放.

Once the demo is started the middle image can be dragged-dropped around the form.

public partial class Form1 : Form
{
    private readonly Image transparentImg; // The transparent image
    private bool isMoving = false;         // true while dragging the image
    private Point movingPicturePosition = new Point(80, 20);   // the position of the moving image
    private Point offset;   // mouse position inside the moving image while dragging
    public Form1()
    {
        InitializeComponent();

        // 
        // pictureBox1
        // 
        this.pictureBox1.Location = new System.Drawing.Point(0, 0);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(231, 235);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
        this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
        this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
        this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
        this.Controls.Add(this.pictureBox1);
        transparentImg = Image.FromFile("..\..\Resources\transp.png");
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        var g = e.Graphics;
        g.DrawImageUnscaled(transparentImg, new Point(20, 20));      // image1
        g.DrawImageUnscaled(transparentImg, new Point(140, 20));     // image2
        g.DrawImageUnscaled(transparentImg, movingPicturePosition);  // image3
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        var r = new Rectangle(movingPicturePosition, transparentImg.Size);
        if (r.Contains(e.Location))
        {
            isMoving = true;
            offset = new Point(movingPicturePosition.X - e.X, movingPicturePosition.Y - e.Y);
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (isMoving)
        {
            movingPicturePosition = e.Location;
            movingPicturePosition.Offset(offset);
            pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        isMoving = false;
    }
}

这篇关于一个图片框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆