在释放拖动时如何防止位图闪烁?

时间:2019-06-06 21:46:46

标签: c# image winforms bitmap panel

我正在Winforms中创建自己的应用程序,我希望能够在其中创建地图并在其上放置图标。单击这些图标将显示信息。该地图是一个简单的图像,图标是位图。用户可以将图标拖动到地图上,然后再次将其拖动或删除。

问题是,当我拖动图标(位图)并释放它时,它会闪烁片刻。不管速度如何,都会发生这种情况,无论它是图标的第一位置。

我的代码仓库:https://github.com/Foxion7/MapMarker

面板,图标等的屏幕截图:https://imgur.com/a/pKMJ4lX您可以单击右侧的图标将新图标拖到地图上。

我已经尝试过使用具有透明度的图像(用于与闪烁无关的问题的努力工作),双缓冲,检查是否过度使用了Invalidate()。

除了位图和多个面板之外,我还希望能通过其他方式来绕过该问题,但到目前为止,我已经走得很远了。

下面是图标在面板内移动的摘录。

    // Selecting an existing icon. 
    private void mapPictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        Icon selectedIcon = map.CheckCollision(e.Location);
        if (selectedIcon != null)
        {
            SelectIcon(selectedIcon);
            currentOffset = new Point((e.X - selectedIcon.center.X) * -1, (e.Y - selectedIcon.center.Y) * -1);
            holdingAnIcon = true;
        }
    }

    // Only hides icon on old position when you start dragging to prevent flashing.
    private void mapPictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (holdingAnIcon)
        {
            if(oneTimePerMoveActionsDone)
            {
                selectedIcon.hidden = true;
                mapPictureBox.Invalidate();
                oneTimePerMoveActionsDone = false;
            }
            var dragImage = ResizeImage(selectedIcon.bitmap, iconWidth, iconHeight);
            IntPtr icon = dragImage.GetHicon();
            Cursor.Current = new Cursor(icon);
        }
    }

    // Releasing after dragging an existing icon.
    private void mapPictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        if (holdingAnIcon)
        {
            selectedIcon.location = mapPanel.PointToClient(new Point(e.X - (int)(iconWidth / 3.75) + currentOffset.X, e.Y + iconHeight / 5 + currentOffset.Y));
            selectedIcon.UpdateCenterPosition();
            selectedIcon.hidden = false;
            holdingAnIcon = false;
            oneTimePerMoveActionsDone = true;

            mapPictureBox.Invalidate();
        }
    }

闪烁(几乎总是)在释放鼠标按钮时发生。有时很少出现闪烁的可能性很小。

0 个答案:

没有答案