要在C#中绘制鼠标,可以使用System.Windows.Forms命名空间中的Mouse类。以下是一个简单的示例代码:
using System;
using System.Drawing;
using System.Windows.Forms;
public class MouseDrawingForm : Form
{
public MouseDrawingForm()
{
InitializeComponent();
this.DoubleBuffered = true;
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ClientSize = new Size(800, 600);
this.Name = "MouseDrawingForm";
this.Text = "Mouse Drawing";
this.Paint += new PaintEventHandler(MouseDrawingForm_Paint);
this.ResumeLayout(false);
}
private void MouseDrawingForm_Paint(object sender, PaintEventArgs e)
{
// 获取鼠标当前位置
Point mousePosition = this.PointToClient(Cursor.Position);
// 绘制鼠标
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Red, 2);
g.DrawLine(pen, mousePosition.X - 10, mousePosition.Y, mousePosition.X + 10, mousePosition.Y);
g.DrawLine(pen, mousePosition.X, mousePosition.Y - 10, mousePosition.X, mousePosition.Y + 10);
}
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MouseDrawingForm());
}
}
这段代码创建了一个继承自Form的自定义窗体MouseDrawingForm。在窗体的Paint事件处理程序中,获取鼠标当前位置,并使用Graphics类的DrawLine方法绘制一个十字形的鼠标图标。最后,通过Application.Run方法运行窗体。运行代码后,将会显示一个窗体,鼠标的十字形图标将会随着鼠标的移动而更新。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: c#相对路径设置的方法是什么