如何在C#中对齐控件和ToolTip消息的右边缘
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在C#中对齐控件和ToolTip消息的右边缘,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1508字,纯文字阅读大概需要3分钟。
内容图文
![如何在C#中对齐控件和ToolTip消息的右边缘](/upload/InfoBanner/zyjiaocheng/825/15360965ed1b4609b60ace8a9a961fee.jpg)
我想在TextBox下面显示一个ToolTip消息,但也希望它们是正确对齐的.
我能够将ToolTip消息放在文本框的右边缘,所以我尝试按消息长度移动消息.
所以我尝试使用TextRenderer.MeasureText()来获取字符串长度,但是位置有点偏,如下所示.
private void button1_Click(object sender, EventArgs e)
{
ToolTip myToolTip = new ToolTip();
string test = "This is a test string.";
int textWidth = TextRenderer.MeasureText(test, SystemFonts.DefaultFont, textBox1.Size, TextFormatFlags.LeftAndRightPadding).Width;
int toolTipTextPosition_X = textBox1.Size.Width - textWidth;
myToolTip.Show(test, textBox1, toolTipTextPosition_X, textBox1.Size.Height);
}
我尝试在MeasureText()函数中使用不同的标志,但它没有帮助,因为ToolTip消息有填充,我去了TextFormatFlags.LeftAndRightPadding.
要清楚,这就是我想要实现的目标:
解决方法:
您可以将ToolTip的OwnerDraw属性设置为true.然后,您可以通过以下方式控制Draw事件中工具提示的外观:
[System.Runtime.InteropServices.DllImport("User32.dll")]
static extern bool MoveWindow(IntPtr h, int x, int y, int width, int height, bool redraw);
private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
{
e.DrawBackground();
e.DrawBorder();
e.DrawText();
var t = (ToolTip)sender;
var h = t.GetType().GetProperty("Handle",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var handle = (IntPtr)h.GetValue(t);
var c = e.AssociatedControl;
var location = c.Parent.PointToScreen(new Point(c.Right - e.Bounds.Width, c.Bottom));
MoveWindow(handle, location.X, location.Y, e.Bounds.Width, e.Bounds.Height, false);
}
内容总结
以上是互联网集市为您收集整理的如何在C#中对齐控件和ToolTip消息的右边缘全部内容,希望文章能够帮你解决如何在C#中对齐控件和ToolTip消息的右边缘所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。