当文本框在ToolStripDropDown编辑时鼠标光标消失
本文关键字:鼠标 光标 消失 编辑 ToolStripDropDown 文本 | 更新日期: 2023-09-27 18:06:48
我有一个用户控件与RichTextBox和两个按钮。我试图在ToolStripDropDown上显示在ToolStripButton点击。我使用ToolStripControlHost把我的控制在ToolStripDrowDown。当我在表单工具栏上单击ToolStripButton时,我在某些位置显示下拉,并将焦点放在ToolStripControlHost控件上。鼠标指针停留在ToolStripButton上方,光标位于RichTextBox。但是,当我开始编辑RichTextBox鼠标指针消失,我可以看到它只有当它的形式矩形。我该怎么修理它?
下面是我的代码:private void toolBtnNote_Click(object sender, EventArgs e)
{
dropDownClosed = false;
noteChanged = false;
tsdd = new ToolStripDropDown();
this.tsdd.Opened += new EventHandler(tsdd_Opened);
this.tsdd.AutoSize = true;
NoteEdit ne = new NoteEdit();
ne.NoteText = note ?? "";
// appears when user clicks first button at my control
ne.OkClick += new NoteEdit.NoteEditEvent(ne_OkClick);
// appears when user clicks second button at my control
ne.CancelClick += new NoteEdit.NoteEditEvent(ne_CancelClick);
this.tbh = new ToolStripControlHost(ne, "noteEdit");
this.tbh.Padding = new Padding(0);
this.tbh.AutoSize = false;
this.tbh.Size = ne.Size;
this.tsdd.Items.Add(tbh);
this.tsdd.Padding = new Padding(0);
this.tsdd.Closing += new ToolStripDropDownClosingEventHandler(tsdd_Closing);
// show toolstripdrowdown at specific position at DataGridView
this.tsdd.Show(dgvMarks, cellRect.Location + new Size(0, cellRect.Height));
while (!this.dropDownClosed)
{
Application.DoEvents();
}
if(noteChanged) {...}
}
void ne_CancelClick()
{
tsdd.Close();
}
void ne_OkClick()
{
noteChanged = true;
tsdd.Close();
}
void tsdd_Opened(object sender, EventArgs e)
{
tbh.Focus();
}
void tsdd_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
dropDownClosed = true;
}
当您开始编辑TextBox或RichTextBox时,鼠标光标隐藏是正常的行为。当你移动鼠标时,它应该会恢复。您还应该注意调用Cursor。如果你试图在TextChanged或类似的东西中调用Show,它将没有效果。
我找到了解决方案。不是最好的,但很有效。我只是用WinApi模拟点击显示的RichTextBox:
public class WinApiHelper
{
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
[DllImport("user32.dll")]
private static extern void mouse_event(
UInt32 dwFlags, // motion and click options
UInt32 dx, // horizontal position or change
UInt32 dy, // vertical position or change
UInt32 dwData, // wheel movement
IntPtr dwExtraInfo // application-defined information
);
public static void SendClick(Point location)
{
Cursor.Position = location;
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}
}
private void toolBtnNote_Click(object sender, EventArgs e)
{
dropDownClosed = false;
noteChanged = false;
dgvMarks.Focus();
tsdd = new ToolStripDropDown();
this.tsdd.Opened += new EventHandler(tsdd_Opened);
this.tsdd.AutoSize = true;
NoteEdit ne = new NoteEdit();
ne.NoteText = note ?? "";
ne.OkClick += new NoteEdit.NoteEditEvent(ne_OkClick);
ne.CancelClick += new NoteEdit.NoteEditEvent(ne_CancelClick);
this.tbh = new ToolStripControlHost(ne, "noteEdit");
this.tbh.Padding = new Padding(0);
this.tbh.AutoSize = false;
this.tbh.Size = ne.Size;
this.tsdd.Items.Add(tbh);
this.tsdd.Padding = new Padding(0);
this.tsdd.Closing += new ToolStripDropDownClosingEventHandler(tsdd_Closing);
this.tsdd.Show(dgvMarks, cellRect.Location + new Size(0, cellRect.Height));
// emulate click on richtextbox at dropdown
WinApiHelper.SendClick(tsdd.Location + new Size(ne.Rtb.Location) + new Size(5, 5));
while (!this.dropDownClosed)
{
Application.DoEvents();
}
if (noteChanged)
{...}
}