我已經使用這個代碼移動圖片框上pictureBox_MouseMove
事件如何在C#中移動PictureBox?
pictureBox.Location = new System.Drawing.Point(e.Location);
,但是當我嘗試執行圖片框閃爍和確切位置不能被識別。你們可以幫我嗎?我想要的圖片框平穩...
我已經使用這個代碼移動圖片框上pictureBox_MouseMove
事件如何在C#中移動PictureBox?
pictureBox.Location = new System.Drawing.Point(e.Location);
,但是當我嘗試執行圖片框閃爍和確切位置不能被識別。你們可以幫我嗎?我想要的圖片框平穩...
您想在鼠標移動量移動控制:
Point mousePos;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
mousePos = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
int dx = e.X - mousePos.X;
int dy = e.Y - mousePos.Y;
pictureBox1.Location = new Point(pictureBox1.Left + dx, pictureBox1.Top + dy);
}
}
注該代碼確實不是而是更新了MouseMove中的mousePos變量。因爲移動控制器必須改變鼠標光標的相對位置。
,@ Dor,@ olivier我曾嘗試過您的想法,但在MouseMove活動期間,圖片框被分割成4個盒子,有一些想法。 – SHiv 2012-03-22 06:59:56
你要做的幾件事情
註冊的移動操作開始在MouseDown
並記住鼠標的起始位置。
在MouseMove
看看你是否真的在移動圖片。通過保持與圖片框左上角相同的偏移量移動,即在移動時,鼠標指針應始終指向圖片框內的相同點。這使得圖片框與鼠標指針一起移動。
在MouseUp
中註冊移動操作的結束。
private bool _moving;
private Point _startLocation;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
_moving = true;
_startLocation = e.Location;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
_moving = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_moving) {
pictureBox1.Left += e.Location.X - _startLocation.X;
pictureBox1.Top += e.Location.Y - _startLocation.Y;
}
}
關於閃爍:我不能肯定,但或許[雙緩衝(http://msdn.microsoft.com/en-us/library/b367a457.aspx「'雙緩衝圖形'在MSDN上「)可以幫助在這種情況下。 – stakx 2012-07-20 15:55:48