2021年1月1日星期五

Tooltip Text going off screen (WinForms)

I've drawn on a Tool Tip control - The code is below.

private void toolTip1_Popup(object sender, PopupEventArgs e)          {              ToolTip tt = (sender as ToolTip);              string toolTipText = tt.GetToolTip(e.AssociatedControl);              e.ToolTipSize = TextRenderer.MeasureText(toolTipText, new Font("Arial", 16.0f));          }  
private void toolTip1_Draw(object sender, DrawToolTipEventArgs e) => DrawToolTip(sender, e);            [System.Runtime.InteropServices.DllImport("User32.dll")]          static extern bool MoveWindow(IntPtr h, int x, int y, int width, int height, bool redraw);          private void DrawToolTip(object sender, DrawToolTipEventArgs e)          {              ToolTip tt = (sender as ToolTip);              string toolTipText = tt.GetToolTip(e.AssociatedControl);              Font f = new Font("Arial", 16.0f);              e.DrawBackground();              e.DrawBorder();              toolTipText = e.ToolTipText;              e.Graphics.DrawString(e.ToolTipText, f, Brushes.Black, new PointF(2, 2));          }  

And then to set the ToolTip text:

toolTip1.SetToolTip(btnLogin, "Some text.....");   

Additionally, here is an image of what is happening.

It looks alright, but it has come to my attention that if the text is a certain length, the tooltip will go off-screen. Is there anyway to prevent that? I would rather not have to add Environment.NewLine(); or \n etc, since I would need to do that for MANY strings.

https://stackoverflow.com/questions/65526197/tooltip-text-going-off-screen-winforms January 01, 2021 at 07:14AM

没有评论:

发表评论