很多人都會遇到一個問題,當我們執行一個複雜的操作是,介面會卡住。使用者體驗會感覺不好。那如果可以添加一個滑鼠的忙碌狀態的話就會好很多了。
以下是很簡單的代碼,用於控制滑鼠的狀態。
1 public static class Cursor
2 {
3 [DllImport("coredll.dll")]
4 public static extern IntPtr LoadCursor(IntPtr hInstance, CursorType lpCursorName);
5 [DllImport("coredll.dll")]
6 public static extern IntPtr SetCursor(IntPtr pCursor);
7
8 public delegate void doing();
9
10 public enum CursorType : uint
11 {
12 IDC_ARROW = 32512U,
13 IDC_WAIT = 32514U
14 }
15
16 public static void Wait(){
17 SetCursor(LoadCursor(IntPtr.Zero, CursorType.IDC_WAIT));
18 }
19
20 public static void Default()
21 {
22 SetCursor(LoadCursor(IntPtr.Zero, CursorType.IDC_ARROW));
23 }
24
25 public static void DoSomething(doing something)
26 {
27 Wait();
28 something();
29 Default();
30 }
31
32 }
其實方法很簡單,在複雜耗時的操作之前調用Cursor.Wait()方法,在方法執行完之後再調用Cursor.Default()。而每次都要進行這兩次操作會比較麻煩,所以我加入了DoSomething方法,用委託去實現,則可以直接調用Curosr. DoSomething(委託)去實現。當然委託還可以自己拓展,用於滿足自己程式的需要。