【问题标题】:Mouse cursor disappears when textbox at ToolStripDropDown editingToolStripDropDown 编辑文本框时鼠标光标消失
【发布时间】:2022-01-20 05:54:45
【问题描述】:

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

【问题讨论】:

    标签: c# mouse-cursor toolstripdropdown


    【解决方案1】:

    当您开始编辑 TextBox 或 RichTextBox 时,鼠标光标隐藏是正常行为。当您移动鼠标时,它应该会恢复。您还应该注意,如果您尝试在 TextChanged 或类似的方式中调用 Cursor.Show,则不会产生任何效果。

    【讨论】:

    • 感谢您的请求。是的,它恢复了,但只是超出了窗体显示矩形。因此,当鼠标悬停在我的表单上时,光标会消失。我需要单击我的表单以显示光标。奇怪的是,当我从 ContextMenuStrip 项目单击中显示相同的工具条下拉菜单(相同的代码)时,一切正常,并且当我移动鼠标时光标出现在表单上。只有当我从 ToolBar 按钮调用 toolstripdropdown 时才会出现问题。看起来焦点停留在 ToolStripButton。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多