首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ToolStripDropDown编辑textbox时,鼠标光标消失

在ToolStripDropDown编辑textbox时,鼠标光标消失
EN

Stack Overflow用户
提问于 2011-08-29 12:54:59
回答 2查看 1.3K关注 0票数 0

我有一个带有RichTextBox和两个按钮的用户控件。我正试图在ToolStripDropDown的ToolStripButton click上展示这一点。我正在使用ToolStripControlHost将我的控制权放在ToolStripDrowDown上。当我在窗体工具栏上单击ToolStripButton时,我在某个位置显示了dropdown,并将焦点放在ToolStripControlHost控件上。鼠标指针停留在ToolStripButton上方,光标位于RichTextBox上。但是当我开始编辑RichTextBox时,鼠标指针消失了,只有当它不是矩形的时候我才能看到它。我怎么才能修复它?

下面是我的代码:

代码语言:javascript
复制
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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-30 14:31:46

我找到了解决方案。不是最好的,但它是有效的。我只是像这样用WinApi在shown上模拟点击:

代码语言:javascript
复制
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)
        {...}
 }
票数 0
EN

Stack Overflow用户

发布于 2011-08-29 13:01:47

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7226212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档