除非要顯示等待光標,否則這很難做到。一個特殊情況,由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);