2011-07-24 21 views
2

我有這個問題:我創建了一個自定義控件(C#,WinForms,框架4.0),其中我需要改變光標時,用戶按下某個鍵(這工作);退出控制我想恢復到以前的光標..但這不起作用:退出光標保持當前一個。怎麼了?光標不改變自定義控制出口

protected override void OnMouseEnter(EventArgs e) 
{ 
    oldCursor = Cursor; 
    base.OnMouseEnter(e); 
} 

protected override void OnMouseLeave(EventArgs e) 
{ 
    Cursor = oldCursor; 
    base.OnMouseLeave(e); 
} 

當按下按鈕,我做的事:

this.Cursor = NewCursor.CreateCursor(
    Properties.Resources.cur_ZoomIn, 14, 9, Color.White); 

其中

public static Cursor CreateCursor(
    Bitmap bmp_parm, int xHotSpot, int yHotSpot, Color? transparent) 
{ 
    Image img = bmp_parm; 
    Bitmap bmp = new Bitmap(img, new Size(img.Width, img.Height)); 
    if (transparent.HasValue) bmp.MakeTransparent(transparent.Value); 

    if (cursor != IntPtr.Zero) 
     DestroyIcon(cursor); 

    IntPtr ptr = bmp.GetHicon(); 
    IconInfo tmp = new IconInfo(); 
    GetIconInfo(ptr, ref tmp); 
    tmp.xHotspot = xHotSpot; 
    tmp.yHotspot = yHotSpot; 
    tmp.fIcon = false; 
    cursor = CreateIconIndirect(ref tmp); 

    if (tmp.hbmColor != IntPtr.Zero) DeleteObject(tmp.hbmColor); 
    if (tmp.hbmMask != IntPtr.Zero) DeleteObject(tmp.hbmMask); 
    if (ptr != IntPtr.Zero) DestroyIcon(ptr); 

    return new Cursor(cursor); 
} 

我用Google搜索周圍(例如here和其他地方)和我的代碼似乎是正確的......

回答

1

執行此操作時:

oldCursor = Cursor; 

您只需將對Cursor字段的引用傳遞給您。之後,你改變這個領域:

this.Cursor = NewCursor.CreateCursor(
    Properties.Resources.cur_ZoomIn, 14, 9, Color.White); 

這也改變了oldCursor領域,如引用類型的對象。 所以你應該改變你保存的方式oldCursor

+0

是@VMAtm,你是對的(我已經考慮過了),謝謝你的回答!問題是我沒有保存和恢復遊標的經驗... – Marco

+0

@Marco沒問題,歡迎您。感謝良好的問題質量和祝你好運:) – VMAtm

+0

@Marco或者你仍然有問題呢? – VMAtm