光标后的C#对象... [英] C# Object following the cursor...............
本文介绍了光标后的C#对象...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
This is my code
private void You_MouseDown(object sender, MouseEventArgs e)
{
You.Location = Cursor.Position;
}
问题是它不跟随它,picturebox(You)只是去某个随机位置而不跟随光标,任何人都可以帮忙吗?
Mt problem is that its not following it, the picturebox(You) is just going to some random location and not following the cursor, can anyone help?
推荐答案
这里是起作用的代码:
Here is the code that works:
private void You_MouseDown(object sender, MouseEventArgs e)
{
You.Location = e.Location;
}
或将光标位置转换为客户坐标的以下解决方案:
or this solution that converts the Cursor position to client coordinates:
private void You_MouseDown(object sender, MouseEventArgs e)
{
You.Location = PointToClient(Cursor.Position);
}
在鼠标移动时移动按钮的替代答案:
Alternative answer that moves a button when the mouse moves:
bool m_bMouseDown = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
button1.Location = PointToClient(Cursor.Position);
m_bMouseDown = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (m_bMouseDown)
button1.Location = PointToClient(Cursor.Position);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
m_bMouseDown = false;
}
您可以轻松使用此代码
You can easily use this code
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
PictureBox b = ((PictureBox) sender);
b.Location = new Point(b.Left + e.X, b.Top + e.Y);
}
}
这篇关于光标后的C#对象...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文