ToolStripDropDown 编辑文本框时鼠标光标消失

发布于 2024-12-02 03:28:41 字数 1732 浏览 2 评论 0原文

我有一个带有 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;
}

I have a user control with RichTextBox and two buttons on it. I'm trying to show that at ToolStripDropDown on ToolStripButton click. I'm using ToolStripControlHost to put my control at ToolStripDrowDown. When I click ToolStripButton at form toolbar, I show dropdown at some position and make focus at ToolStripControlHost control. Mouse pointer stays above ToolStripButton and cursor is at RichTextBox. But when I start editing RichTextBox mouse pointer disappears and I can see it only when it's out of form rectangle. How can I fix it?

Here is my code:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烙印 2024-12-09 03:28:41

当您开始编辑 TextBox 或 RichTextBox 时,鼠标光标隐藏是正常现象。当您移动鼠标时它应该会恢复。您还应该注意,调用 Cursor.Show 将不会产生任何效果。

It is normal behavior for the mouse cursor to hide when you start to edit a TextBox, or RichTextBox. It should be restored when you move the mouse. You should also note that calling Cursor.Show will not have an effect if you try to call it in TextChanged, or something similar.

貪欢 2024-12-09 03:28:41

我找到了解决方案。不是最好的,但它有效。我只是用 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)
        {...}
 }

I found the solution. Not best but it works. I just simulate click at shown RichTextBox with WinApi like that:

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)
        {...}
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文