繁体   English   中英

如何制作跟随鼠标移动的图片?

[英]How to make a picture that follow the mouse movement?

我正在用 C# 2.0 编写一个窗口应用程序。
我已经添加了一张图片作为项目的参考。

我不能做的是:
我需要鼠标移动后图片移动,这意味着屏幕上鼠标的标志旁边会有我的图片随它移动。

我想我应该使用MouseMove的功能,但我看不出究竟是如何使用的。

任何帮助都会有用:) 谢谢!

为了将位图保存到图片框以便显示,您可以简单地设置如下:

this.pictureBox.Image = yourBitmapImage;

要设置MouseMove功能,请右键单击 VisualStudios 中的表单并转到属性。 根据您的版本,您可能会在小窗口中看到一道闪电。 然后您可以定义或分配类似于 Gabe 所说的MouseMove函数。

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    this.pictureBox1.Location = new Point(e.X, e.Y);
}

如果您的代码将参数作为EventArgs e ,则可以将其转换为MouseEventArg ,如下所示:

private void Form1_MouseMove(object sender, EventArgs e)
{
    var me = (MouseEventArgs)e;
    this.pictureBox1.Location = new Point(me.X, me.Y);
}

您必须处理MouseMove事件并根据新的鼠标位置更改表单上的图片位置。

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
   this.pictureBox1.Location = new Point(e.X, e.Y);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM