2013-05-10 68 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); 
相关问题