[求助!急!]C#自定义鼠标样式,请大家帮忙看看
奇点码农 2019-01-22 11:03:08 在写一个项目遇到一个需求:根据选择选择不同radioButton生成不同的自定义鼠标样式,但是当鼠标在datagridview范围内的时候,鼠标的样式还是小箭头,偶尔会闪烁称我定义的图片,但是一闪就又变成小箭头了,我测了一下如果出了datagridview的范围是可以的,是不是datagridview需要设置什么属性啊!请大神指教,感谢感谢!
代码如下:
private void dgv_MicrPlate_Layout_MouseHover(object sender, EventArgs e)
{
try
{
Bitmap bitmap = (Bitmap)GetImage(Application.StartupPath + "\\MicrPlate\\" + Cursor_Path + ".png");
SetCursor(bitmap, new Point(0, 0));
//this.Cursor= Cursors.Hand;
}
catch (Exception ex)
{
LogHelper.WriteErrorLog(ex);
throw ex;
}
}
private object GetImage(string path_temp)
{
System.IO.FileStream fs = new System.IO.FileStream(path_temp, System.IO.FileMode.Open);
try
{
System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
return image;
}
catch (Exception ex)
{
LogHelper.WriteErrorLog(ex);
throw ex;
}
finally
{
fs.Close();
}
}
public void SetCursor(Bitmap cursor, Point hotPoint)
{
int hotX = hotPoint.X;
int hotY = hotPoint.Y;
Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
Graphics g = Graphics.FromImage(myNewCursor);
g.Clear(Color.FromArgb(0, 0, 0, 0));
g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width,
cursor.Height);
this.Cursor = new Cursor(myNewCursor.GetHicon());
g.Dispose();
myNewCursor.Dispose();
}