0

在 For1 我有这个代码:

private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {     
                this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Load(file_array_satellite[file_indxs_satellite]);
                file_indxs_satellite = file_indxs_satellite - 1;
                if (file_indxs_satellite < 0)
                {
                    file_indxs_satellite = file_array_satellite.Length - 1;
                }
            }
            catch
            {
                timer1.Enabled = false;
            }
        }

        private void satellitesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            file_array_satellite = Directory.GetFiles(UrlsPath, "RainImage*.*");
            if (file_array_satellite.Length > 0)
            {
                DateTime[] creationTimes8 = new DateTime[file_array_satellite.Length];
                for (int i = 0; i < file_array_satellite.Length; i++)
                    creationTimes8[i] = new FileInfo(file_array_satellite[i]).CreationTime;
                Array.Sort(creationTimes8, file_array_satellite);
                file_indxs_satellite = 0;
                file_indxs_satellite = file_array_satellite.Length - 1;
                timer1.Enabled = true;
            }
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox1.Size = new Size(500, 500);
            pictureBox1.Location = new Point(this.Bounds.Width / 2,
                            this.Bounds.Height / 2);
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.BringToFront();
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {

                this.pictureBox1.Size = new Size(100, 100);
                pictureBox1.Location = new Point(12,
                                27);

        }

在原始图片中,picturebox1 的大小为 100x100,我拉伸每个图像以适合图片框。当它是 100x100 时,一切正常,我在图片框中看到每个图像的动画。

现在我做了一个事件,当我用鼠标进入图片框区域时,它应该移动到表单的中心,将大小调整为 500x500 拉伸图像并显示相同的动画。当我离开图片框区域时,它应该恢复到原来的大小和位置。

当我用鼠标进入pictureBox1 区域时,pictureBox 就消失了,一旦我离开pictureBox 区域,我在任何地方都看不到它,我在它原来的位置和大小上看到它100x100。

为什么当我用鼠标进入 pictureBox1 区域时它消失了,我在尺寸为 500x500 的表格中心看不到它?

file_array_satellite 是 string[] 并且 file_indxs_satellite 是 int。

RainImage*.* 是下载后硬盘上的文件名。

这个想法不是每次我进入或离开时都转换/更改硬盘上的文件大小,所以我希望一旦我进入pictureBox1区域,它将拉伸pictureBox中的当前图像并显示它。它在 100x100 时有效,但在 500x500 时无效。

4

3 回答 3

3

当您将鼠标悬停在 PictureBox 上并将其移动到窗体的中心时,您正在将其从鼠标光标下移出。这会导致 MouseLeave 事件立即触发,这会将其再次置于鼠标光标下,从而导致 MouseEnter 事件再次触发,等等。

你可以这样做:

    bool suppressMouseLeave;
    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {
        suppressMouseLeave = true;
        this.pictureBox1.Size = new Size(500, 500);
        pictureBox1.Location = new Point(this.Bounds.Width / 2,
                        this.Bounds.Height / 2);
        this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox1.BringToFront();
        //point the cursor to the new Position so that it's still kept on the pictureBox1
        //This is important because it makes your idea acceptable.
        //Otherwise you have to move your mouse onto your pictureBox and leave the 
        //mouse from it then to restore the pictureBox
        Cursor.Position = PointToScreen(new Point(pictureBox1.Left + 250, pictureBox1.Top + 250));
        suppressMouseLeave = false;
    }

    private void pictureBox1_MouseLeave(object sender, EventArgs e)
    {
        if(suppressMouseLeave) return;
        this.pictureBox1.Size = new Size(100, 100);
        pictureBox1.Location = new Point(12, 27);
    }
于 2013-10-10T17:22:03.047 回答
0

我冒昧地猜测一下this.Bounds.Widththis.Bounds.Height这并不是您期望的那样,因此 PictureBox 并没有消失,您只是将其设置到屏幕外/表单外的某个位置。在调试模式下运行 Visual Studio 并在该行周围放置一个断点,看看this.Bounds等于什么。这可能会为您提供有关需要设置的正确位置的线索。

于 2013-10-10T17:17:44.503 回答
0

像这样“就地”缩放怎么样?

    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {
        Rectangle rc = pictureBox1.Bounds;
        rc.Inflate(200, 200);
        pictureBox1.Bounds = rc;
        pictureBox1.BringToFront();
    }

    private void pictureBox1_MouseLeave(object sender, EventArgs e)
    {
        Rectangle rc = pictureBox1.Bounds;
        rc.Inflate(-200, -200);
        pictureBox1.Bounds = rc;
    }
于 2013-10-10T17:42:07.717 回答