C#从右到左调整面板大小

时间:2012-09-06 10:16:41

标签: c# resize panel picturebox mousemove

我花了几个小时在互联网上搜索我的面板并从左侧抓住并将其向左拉。我发现了许多来源,我试图根据我的需要改变它,但它总是从左到右。我目前的代码是:

bool allowResize = false;

private void PanelResize_MouseUp(object sender, MouseEventArgs e)
    {
        allowResize = false;            
    }

    private void PanelResize_MouseMove(object sender, MouseEventArgs e)
    {
        if (allowResize)
        {
            FavoritesPanel.Width = PanelResize.Left + e.X;
        }
    }

    private void PanelResize_MouseDown(object sender, MouseEventArgs e)
    {
        allowResize = true;
    }

“PanelResize”是一个推到面板左侧的图片框。 “FavoritesPanel”是面板。两者都固定在顶部,底部和右侧。

我的整体问题是,如何更正我的代码以从右向左拖动我的面板?

1 个答案:

答案 0 :(得分:0)

问题是更改某些面板的宽度的默认设置意味着它使面板的宽度更大,从而右侧向右移动。 您需要两件事。这是您要的(调整宽度),其次是只要将整个面板的宽度增加,就需要将整个面板向左侧移动。

我在这里有类似的代码。我编写了允许您向上滚动面板上方的代码。因此,我不得不再次做两件事:使面板高度更高,并使整个面板向上移动。这是我的代码:

public partial class Form1 : Form
{
    private int actualCursorY;
    private int lastCursorY;
    private bool isDragged;

    public Form1()
    {
        InitializeComponent();
    }

    private void barRadPanel_MouseDown(object sender, MouseEventArgs e)
    {
        lastCursorY = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)).Y;
        isDragged = true;
    }

    private void barRadPanel_MouseUp(object sender, MouseEventArgs e)
    {
        isDragged = false;
    }

    private void barRadPanel_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDragged)
        {
            actualCursorY = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)).Y;
            mainRadPanel.Location = new Point(mainRadPanel.Location.X, actualCursorY);

            if (lastCursorY != actualCursorY)
            {
                mainRadPanel.Height -= actualCursorY - lastCursorY;
                lastCursorY = actualCursorY;
            }
        }
    }
}

我尝试使用

e.Y

代替

PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)).Y

但是它犯了一些错误。

这就是它的作用:

enter image description here