我用的VS2017,C#。在做一個坐標定位的功能時,使用四個TextBox跟隨滑鼠移動顯示坐標,但是在移動時出現嚴重的拖影,在某些配置較低的電腦上甚至出現了行程崩潰,但是我已經按照網上所說的給TextBox所屬的Form開啟了雙緩沖功能,還是不行:
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

我是通過滑鼠移動事件更新的TextBox的location
public void MouseMove()
{
int x = (int)_position.x + 10;
_xTextBox.Location = new Point(x, (int)_position.y + 5);
_yTextBox.Location = new Point(x, _xTextBox.Location.Y + _xTextBox.Height + 5);
_lonTextBox.Location = new Point(x+95, (int)_position.y + 5);
_latTextBox.Location = new Point(x+95, _lonTextBox.Location.Y + _lonTextBox.Height + 5);
_tipLabel.Location = new Point(x, _yTextBox.Location.Y + _yTextBox.Height + 5);
}
求大神解惑!
uj5u.com熱心網友回復:
把這5個控制元件放到一個用戶控制元件中試試,每次只要改用戶控制元件坐標就好了uj5u.com熱心網友回復:
我試了,放到一個panel里也不成,還是拖影……uj5u.com熱心網友回復:
異步處理試, 開執行緒100毫秒執行一次更新位置uj5u.com熱心網友回復:
抽資料,因為隨手晃動移動一下滑鼠,系統就會發送40-50條訊息問題是,我們需要那么多訊息么。所以請抽資料,比如沒10潭訓資料,或者每200ms畫一次
uj5u.com熱心網友回復:
GDI畫圖效率很低的,你是不是可以做一個虛影跟隨滑鼠,滑鼠釋放的時候才真正的重繪。uj5u.com熱心網友回復:
這樣能好一些。不過影像化處理效果更好不會閃爍,就是組件轉影像再paint。
using System.Drawing;
using System.Windows.Forms;
namespace Test_c
{
public partial class Form1 : Form
{
public class PanelEx : Panel
{
public PanelEx()
{
SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.UserPaint,
true
);
UpdateStyles();
}
}
private TextBox[] TBox = new TextBox[4];
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
PanelEx panelEx = new PanelEx() { Dock = DockStyle.Fill };
this.Controls.Add(panelEx);
for (int i=0;i<4;i++)
{
TBox[i] = new TextBox() { Size = new Size(100, 20), Location = new Point(10,10+i*50) };
panelEx.Controls.Add(TBox[i]);
}
panelEx.MouseMove += (s, e) =>
{
Point point = MousePosition;
for (int i = 0; i < 4; i++)
{
TBox[i].Location = new Point(point.X, point.Y + i * 50);
}
};
}
}
}
uj5u.com熱心網友回復:
不行呀,畫成圖我就沒辦法編輯里面的數值了啊轉載請註明出處,本文鏈接:https://www.uj5u.com/net/9842.html
標籤:C#
