C#学习教程:C#Winforms中的自定义游标我有一个.cur和.png光标。我试过将它作为资源添加到我的项目中,并且我试过将它作为文件包含在项目中。理想情况下,我想嵌入它,但我只是想让它工作。当我使用Cursorcur=newCursor("mycursor.cur")时,我得到“无效的图像格式。图像文件可能已损坏”。我试过这个http://mahesg.wordpress.com/2008/02/09/embedding-cursor/但它没有用。使用WinForm1.Properties.Resources.mycursor返回一个byte[],我不知道如何转换为Cursor类型。出于某种原因,游标类对其读取的内容非常挑剔。您可以使用WindowsAPI自己创建句柄,然后将其传递给游标类。C#://(在类中)publicstaticCursorActuallyLoadCursor(Stringpath){returnnewCursor(LoadCursorFromFile(path))}[System.Runtime.InteropServices.DllImport("user32.dll")]privatestaticexternIntPtrLoadCursorFromFile(字符串文件名);VB.Net:'(在一个类中)'公共共享函数ActuallyLoadCursor(pathAsString)AsCursorReturnNewCursor(LoadCursorFromFile(path))EndFunctionPrivateSharedFunctionLoadCursorFromFile(fileNameAsString)AsIntPtrEndFunctionWritenewCursor(newMemoryStream(Properties.Resources.mycursor))要在C#中向光标添加自定义图标:将图标文件添加到项目资源(例如:Processing.ico),并在图像中将“BuildAction”切换为“Cursorcur=newCursor(Properties.Resources.**Imagename**.Handle);this.Cursor=cur;例如:Cursorcur=newCursor(Properties.Resources.Processing.Handle);this.Cursor=cur;目标:改变cursortoacustomcursorwhentheuserneedstoperformacutactivityintheexamplewinformsUI这样就可以了更多C#学习教程,希望大家多多关注——图标文件(如cut.ico)添加到项目现在将图标添加到项目资源要添加到资源右键单击项目->属性->资源现在从资源中删除项目文件夹中的ico文件(U添加到项目文件夹点1)这段代码应该可以解决问题System.Windows.Forms.Cursor_customCutCursor=newSystem.Windows.Forms.Cursor(Properties.Resources.cut.Handle);点击右侧联系管理员删除。如需转载请注明出处:
