您好我有我想要移動我的鼠標,然後將的DragDrop功能Windows窗體應用程序,但我一直在使用鼠標移動鼠標事件做嘗試,但似乎像的DragDrop非常sensitive.So我是什麼詢問是否可以檢測鼠標光標是否與當前光標至少保持一定距離,然後執行拖放代碼。鼠標光標位置改變
Q
鼠標光標位置改變
2
A
回答
2
我知道你有你想要只有當鼠標移動一定的距離,以執行一拖&下降代碼。如果是這樣的:
您可以勾鼠標移動事件一旦用戶進行了初次動作(鼠標向下拖動項目?)。然後比較鼠標移動事件上的鼠標座標,一旦座標差異高於您設置的任意值,就會觸發「拖放代碼」。
private int difference = 10;
private int xPosition;
private int yPosition;
private void item_MouseDown(object sender, MouseEventArgs e)
{
this.MouseMove += new MouseEventHandler(Form_MouseMove);
xPosition = e.X;
yPosition = e.Y;
}
private void Form_MouseMove(object sender, MouseEventArgs e)
{
if (e.X < xPosition - difference
|| e.X > xPosition + difference
|| e.Y < yPosition - difference
|| e.Y > yPosition + difference)
{
//Execute "dragdrop" code
this.MouseMove -= Form_MouseMove;
}
}
當光標移出虛擬正方形10×10的該將執行的DragDrop。
0
我真的不明白你的問題和你試圖達到的效果。 順便說一句,如果我的解釋是正確的,那麼你只有在「拖拽距離」超過一定數量時纔會嘗試做某件事。
這下面的代碼不使用拖動& drop事件,但是鼠標鬆開,鼠標按下鼠標移動和事件。
它跟蹤鼠標在按下左按鈕時的行程。 當距離大於一個固定量時,它改變鼠標光標(在拖動過程中)
當mouseButton被釋放時,如果dstance旅行大於最小偏移量,我們執行我們的假脫機操作。
創建新的Windows窗體項目和替代Form1中自動生成的類下面
希望這有助於之一。
public partial class Form1 : Form
{
private bool isDragging; //We use this to keep track that we are FakeDragging
private Point startPosition; //The start position of our "Fake" dragging action
private double dragDistance = 0; //The distance(absolute) from the drag action starting point
private const int MINIMUM_DRAG_DISTANCE = 100; //minimum FakeDrag distance.
private Label label1 = new Label();
public Form1()
{
#region Support form generation code
InitializeComponent();
this.label1 = new System.Windows.Forms.Label();
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(54, 242);
this.label1.Name = "Distance:";
this.label1.Size = new System.Drawing.Size(35, 13);
this.Controls.Add(label1);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
#endregion
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
//if the mouse button is pressed then
//there's the chanche a dragAction
//is being performed and we take note
//of the position of the click
//(we will use this later on the mouseMove event
//to calculate the distance mouse has traveled
if (e.Button.Equals(MouseButtons.Left))
startPosition = e.Location;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
///at the mouse up event we can execute code that does something
if (isDragging && dragDistance > MINIMUM_DRAG_DISTANCE)
{
MessageBox.Show("we have fakedragged!\nDo something useful here");
//Do your Drag & Drop code here.
}
///but WE MUST signal our system that the Fake Drag has ended
///and reset our support variables.
this.Cursor = Cursors.Default;
isDragging = false;
dragDistance = 0;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
///when the mouse moves we control if the mouse button is still pressed.
///if it is so, we surely are FakeDragging and we set our boolean to true
///(it will be useful in the mouseUP event).
///Then we calculate the absolute distance the mouse has traveld since the
///user has pressed the left mouseButton.
Point currentPosition = e.Location;
if (e.Button.Equals(MouseButtons.Left))
{
isDragging = true;
dragDistance =
Math.Abs(
Math.Sqrt(
(
Math.Pow(
(currentPosition.X - startPosition.X), 2)
+
Math.Pow(
(currentPosition.Y - startPosition.Y), 2)
)));
}
//we set the label text displaying the distance we just calculated
label1.Text =
String.Format(
"Distance: {0}",
dragDistance.ToString());
Application.DoEvents();
///At last, if the distance il greater than our offset, we change the
///mouse cursor(this is not required in a real case scenario)
if (dragDistance > MINIMUM_DRAG_DISTANCE)
this.Cursor = Cursors.Hand;
else
this.Cursor = Cursors.Default;
}
}
相關問題
- 1. 從鼠標光標位置
- 2. firemonkey設置鼠標光標位置
- 3. 鼠標光標在Javascript中的位置?
- 4. PyGame從位圖設置鼠標光標
- 5. 由js改變鼠標的光標
- 6. 鼠標光標改變Javascript - 鏈接DIV
- 7. WM_NCHITTEST不改變鼠標光標
- 8. 改變鼠標移動光標類型
- 9. 鼠標光標位圖
- 10. VIM-snipmate改變結束光標位置
- 11. 光標位置改變事件
- 12. 恢復光標位置改變CONTENTEDITABLE後
- 13. 在textview中改變了光標位置
- 14. 如何根據鼠標位置更改光標?
- 15. 如何更改OpenGL/SFML中鼠標光標的位置?
- 16. 如何在OpenGL/Glut中更改鼠標光標的位置?
- 17. 用鼠標位置改變圖像
- 18. 根據鼠標位置改變圖像
- 19. Qt鼠標光標重置
- 20. 禁用鼠標右鍵單擊並且不改變光標位置在Javascript中
- 21. 更改鼠標位置
- 22. 如何更改鼠標光標圖標?
- 23. C#Winforms - 更改鼠標光標圖標
- 24. 光標位置
- 25. 鼠標位置()
- 26. 在RichTextBox中查找鼠標光標位置的座標
- 27. 如何在C上設置鼠標光標位置?
- 28. 在mac上的visual studio中設置鼠標光標位置
- 29. 如何從Windows中的鼠標鉤子設置光標位置?
- 30. 如何重置鼠標光標位置c#
我會嘗試執行你分享的代碼片段感謝回答我的問題,我認爲它會工作 – pumphandle 2010-11-10 11:33:58