【问题标题】:Dragging textbox in C# winforms application stuttering在 C# winforms 应用程序中拖动文本框口吃
【发布时间】:2015-08-07 03:23:05
【问题描述】:

我有一个允许用户在面板中拖动文本框的 winforms 应用程序。

单击时,我设置了一个布尔标志“moveMode”,然后我将位置设置为鼠标拖动时的位置。

问题是它卡顿且不流畅。下图是一个被拖动的文本框对象

我认为可能是重绘速度不够快,无法拖动?有没有办法提高重绘率?

这是我的文本框 mouseMove 事件代码:

    void textBox1_mouseMove(object sender, MouseEventArgs e)
    {
        if (draggingTxtBox && moveMode)
        {
            Point newLocation = new Point();
            newLocation.X = e.Location.X - dragOffset.X;
            newLocation.Y = e.Location.Y - dragOffset.Y;
            txtBox.Location = newLocation;
            dragOffset = e.Location;
            dragOffset.X -= txtBox.Location.X;
            dragOffset.Y -= txtBox.Location.Y;

            txtBox.SelectionLength = 0;
            this.Refresh();
            txtBox.Refresh();
        }

            Point newPos = new Point();
            newPos.X = e.X + offset.X;
            newPos.Y = e.Y + offset.Y;}

【问题讨论】:

  • 请检查此link 可能会有所帮助。

标签: c# winforms draggable


【解决方案1】:

试试这个link

Point dragOffset;

protected override void OnMouseDown(MouseEventArgs e) {
    base.OnMouseDown(e);
    if (e.Button == MouseButtons.Left) {
        dragOffset = this.PointToScreen(e.Location);
        var formLocation = FindForm().Location;
        dragOffset.X -= formLocation.X;
        dragOffset.Y -= formLocation.Y;
    }
}

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

    if (e.Button == MouseButtons.Left) {
        Point newLocation = this.PointToScreen(e.Location);
        newLocation.X -= dragOffset.X;
        newLocation.Y -= dragOffset.Y;
        FindForm().Location = newLocation;
    }
}

【讨论】:

    猜你喜欢
    • 2012-01-05
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 2011-01-25
    • 2011-06-05
    • 2022-10-21
    相关资源
    最近更新 更多