[Tip: cursor]小Cursor大学问

I worked on a task: when user clicks a command in the application, the cursor can be that command specific. Some gains:

1. Cursor can only accept cur file with only black-white color. It's really a big limitation. Trying to load a colorful cur file into Cursor will lead to a exception thrown.

2. In order to get a colorful Cursor, the only choice is to load a icon/bitmap to get a Cursor. The key points of the solution:

  a. To generate a Cursor is easy. Icon.Handle or Bitmap.GetHicon() will be enough to compose a Cursor.

  b. But it's not enough. By default, the hotspot is in the center of the icon/bitmap. Two ways to set the hot spot:

    1. Enlarge the image size to get the center point moved. Tricky way.

    2. Set the hot spot after getting the icon/bitmap handle. See http://www.switchonthecode.com/tutorials/csharp-tutorial-how-to-use-custom-cursors for detail. Very good practice.

Learn in C# to use resources: Always add the icon/bitmap/cursor file into project by .Net Resoure Editor. It will automatically expose a function to get that resource loaded.

 


 

  

posted @ 2010-03-30 16:20  能巴  阅读(177)  评论(0编辑  收藏  举报