gpt4 book ai didi

c# - 打字时显示类似 Intellisense 的窗口(无法获得正确的坐标)

转载 作者:行者123 更新时间:2023-11-30 22:17:17 25 4
gpt4 key购买 nike

我试图在文本最后一个字符的正下方显示一个类似智能感知的小窗口(它是一个小窗体),但在我当前的代码中,它的坐标似乎是随机的并且不是很准确。我们将不胜感激。

当我按下 Alt+1(在输入时显示类似智能的窗口,我调用这段代码:

shortcuts.Location = GetPoint((TextBox)tabControl1.SelectedTab.Controls[0]);
shortcuts.Show(this);

获取坐标的coed是:

private Point GetPoint(TextBox textBoxControl)
{
Graphics graphics = Graphics.FromHwnd(textBoxControl.Handle);
SizeF size = graphics.MeasureString(textBoxControl.Text.Substring(0,
textBoxControl.SelectionStart), textBoxControl.Font);
Point coord = new Point((int)size.Width + textBoxControl.Location.X,
(int)size.Height + 200 + textBoxControl.Location.Y);
return coord;
}

工作代码

我承认可能有更好的方法,但这对我来说非常有效:

private Point GetPoint(TextBox textBoxControl)
{
Graphics graphics = Graphics.FromHwnd(textBoxControl.Handle);
SizeF size = graphics.MeasureString(textBoxControl.Text.Substring(0,
textBoxControl.SelectionStart), textBoxControl.Font);
Point coord = new Point(this.Left + (int)size.Width + textBoxControl.Location.X + 5,
this.Location.Y + 25 + toolStrip1.Height + tabControl1.Top + (int)size.Height + tabControl1.Top + textBoxControl.Location.Y + 25);
return coord;
}

然而,由于某种原因,第一次调用此方法时,它总是出现在某个随机位置——尽管每次对该方法的后续调用都会导致窗口被放置在它应该放置的位置;就在当前文本行的下方并水平对齐,这样窗口就在文本框中的最后一个字符之后。

最佳答案

试试这个:

TextBox tb = (TextBox)tabControl1.SelectedTab.Controls[0];
Point thePoint = GetPoint(tb);
shortcuts.Location = tb.PointToClient(tabControl1.PointToScreen(thePoint)); //assuming "this" is the form itself

在 GetPoint() 方法中,将“200 +”替换为 tabControl1.GetTabRect(0).Height。我还必须添加 5 作为 X 和 Y 的填充,因为 tabControl 有一些间距。

关于c# - 打字时显示类似 Intellisense 的窗口(无法获得正确的坐标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16835763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com