如何限制拖动面板内的图像?

时间:2016-11-06 19:23:41

标签: c# .net

晚上好。我目前正在开发一个交互式地图编辑器。我试图通过使用此代码来阻止地图图像被拖出视图

sentence.getWords()

但是我无法将其拖动到Size-mapSize的限制。 currentPosition对应于图像的左上角位置。这是该类的完整代码

        if (currentPosition.X > 0)
            currentPosition.X = 0;
        else if (currentPosition.X <  -mapSize.Width + Size.Width )
            currentPosition.X =  -mapSize.Width + Size.Width ;
        if (currentPosition.Y > 0)
            currentPosition.Y = 0;
        else if (currentPosition.Y < -mapSize.Height + Size.Height)
             currentPosition.Y = -mapSize.Height + Size.Height;

}

1 个答案:

答案 0 :(得分:0)

好吧,除了我使用Panel的Size而不是DisplayRectangle(Size.Width == DisplayRectangle.Width-2)之外,似乎没有错误的逻辑。整个问题是我使用的图像是从Photoshop导出的,分辨率为72dpi而不是96dpi:相同尺寸,但在使用DrawImage时,它们被放大了。这是我的解决方案。仍然有这个要纠正,但这是我原来问题的解决方案。还改变了一些变量名称。

namespace GameMaps
{
public class MapPanel : Panel
{
    private Map sourceMap;

    private Bitmap mapTexture;
    private Size mapSize;
    private Point mapPosition;
    private float dpiX, dpiY;

    private Point initialPosition;
    private Point initialMousePosition;

    private bool dragging;

    /* Constructors */
    public MapPanel() : base()
    {
        this.DoubleBuffered = true;

        this.sourceMap = null;
        this.dpiX = 96.0F; this.dpiY = 96.0F; // For now, this Application won't be DPI aware
    }

    public MapPanel(Panel targetPanel) : this()
    {
        // TO DO
    }

    /* Show a Map */
    public void ShowMap(Map map)
    {
        if (map == null)
            return;
        sourceMap = map;

        mapTexture = new Bitmap(map.MapTexture);
        mapTexture.SetResolution(dpiX, dpiY);
        mapSize = mapTexture.Size; // ?
        mapPosition = new Point(0, 0);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // base.OnPaint(e);

        e.Graphics.PageUnit = GraphicsUnit.Pixel; // ?
        e.Graphics.PageScale = 1.0F; // ?

        if (sourceMap == null)
        {
            base.OnPaint(e);
            return;
        }
        e.Graphics.DrawImage(mapTexture, mapPosition.X, mapPosition.Y);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        // base.OnMouseDown(e);

        if (sourceMap == null)
        {
            base.OnMouseDown(e);
            return;
        }

        if (dragging)
        {
            dragging = false;
            return;
        }
        else dragging = true;

        initialPosition.X = mapPosition.X;
        initialPosition.Y = mapPosition.Y;

        initialMousePosition.X = e.Location.X;
        initialMousePosition.Y = e.Location.Y;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        //base.OnMouseMove(e);

        if (!dragging) return;

        mapPosition.X = e.X - initialMousePosition.X + initialPosition.X;
        mapPosition.Y = e.Y - initialMousePosition.Y + initialPosition.Y;

        if (mapPosition.X > 0)
            mapPosition.X = 0;
        else if (mapPosition.X < DisplayRectangle.Width - mapSize.Width)
            mapPosition.X = DisplayRectangle.Width - mapSize.Width;

        if (mapPosition.Y > 0)
            mapPosition.Y = 0;
        else if (mapPosition.Y < DisplayRectangle.Height - mapSize.Height)
            mapPosition.Y =  DisplayRectangle.Height - mapSize.Height;

        Invalidate(); // Force Repaint
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        // base.OnMouseLeave(e);

        dragging = false;
        // TO DO: Correct this
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        //base.OnMouseUp(e);

        dragging = false;
    }
}
}