2013-05-10 67 views
0

我知道,這樣做拖放時,我可以這樣做改變光標形狀有複製光標形狀

private void Form_DragOver(object sender, DragEventArgs e) 
{ 
    e.Effect = DragDropEffects.Copy; 
} 

使光標有加形象意義副本。我只是想知道,如果我不做拖放(例如,當用戶單擊某個特定位置時,光標變爲此樣式,直到用戶單擊其他位置),是否可以執行此操作。我嘗試使用Cursor = Cursors.<style>,但它不包含此內容。有任何想法嗎 ?

回答

2

除非要顯示等待光標,否則這很難做到。一個特殊情況,由Application.UseWaitCursor屬性處理。問題在於每個控件本身都會影響光標形狀,如其Cursor屬性所選。例如,一個TextBox會堅持將形狀改爲I-bar。

你稍微領先一點,只希望在兩次點擊之間做到這一點。在這種情況下可能會有一些技巧,您可以在單擊按鈕時捕獲鼠標,以便光標形狀完全由按鈕控制。當用戶再次單擊鼠標時,需要進行破解,該點擊將轉到同一按鈕,而不是點擊任何控件。這需要通過合成另一次點擊來解決。此示例代碼完成此操作:

bool CustomCursorShown; 

    private void button1_MouseUp(object sender, MouseEventArgs e) { 
     if (button1.DisplayRectangle.Contains(e.Location)) { 
      this.BeginInvoke(new Action(() => { 
       CustomCursorShown = true; 
       button1.Cursor = Cursors.Help; // Change this to the cursor you want 
       button1.Capture = true; 
      })); 
     } 
    } 

    private void button1_MouseDown(object sender, MouseEventArgs e) { 
     if (CustomCursorShown) { 
      var pos = this.PointToClient(button1.PointToScreen(e.Location)); 
      var ctl = this.GetChildAtPoint(pos); 
      if (ctl != null && e.Button == MouseButtons.Left) { 
       // You may want to alter this if a special action is required 
       // I'm just synthesizing a MouseDown event here... 
       pos = ctl.PointToClient(button1.PointToScreen(e.Location)); 
       var lp = new IntPtr(pos.X + pos.Y << 16); 
       // NOTE: taking a shortcut on wparam here... 
       PostMessage(ctl.Handle, 0x201, (IntPtr)1, lp); 
      }     
     } 
     button1.Capture = false; 
    } 

    private void button1_MouseCaptureChanged(object sender, EventArgs e) { 
     if (!button1.Capture) { 
      CustomCursorShown = false; 
      button1.Cursor = Cursors.Default; 
     } 
    } 

    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    private extern static IntPtr PostMessage(IntPtr hwnd, int msg, IntPtr wp, IntPtr lp); 
相關問題