工具提示气泡显示位置(用于错误通知)

4

我之前问过一个与此相关的问题:替代通知用户错误的方法

简而言之,我试图找到一种快速简便的方法,在不使用弹出窗口的情况下通知用户错误。

现在我已经使用工具提示气球实现了这一点。问题是,即使我给它一个常规位置,气泡的小尖部分也会根据消息大小而改变位置(见附加的图片)。通常,我会使用SetToolTip()将其分配给控件,以便它始终指向该控件。但是该控件是状态栏中的标签或图像。

private void ShowTooltipBalloon(string title, string msg)
{
    if (this.InvokeRequired)
    {
        this.BeginInvoke(new EventHandler(delegate { ShowTooltipBalloon(title,msg); }));
    }
    else
    {
        ToolTip tt = new ToolTip();
        tt.IsBalloon = true;
        tt.ToolTipIcon = ToolTipIcon.Warning;
        tt.ShowAlways = true;
        tt.BackColor = Color.FromArgb(0xFF, 0xFF, 0x90);
        tt.ToolTipTitle = title;

        int x = this.Width - lblLeftTarget.Width - lblVersion.Width - toolStripStatusLabel8.Width - 10;
        int y = this.Height - lblLeftConnectImg.Height - 60;
        tt.Show(msg, this, x, y, 5000);
    }
}

这超出了需求范围,但我的老板非常注重细节,所以除了解决这个问题,我还要快速解决它。我需要一个相对容易实现的东西,不会“动摇”当前即将发布的软件。话虽如此,当然我会听取任何建议,无论是否可行。至少我可能会学到一些东西。*编辑:看起来我的图片没有显示。我不知道是不是只有我的电脑有这个问题。哦,算了...

1
如果您不创建自己的工具提示表单,您将需要控制行长度(即在50个字符后使用 Environment.NewLine)以确保标准宽度。 - John M
1个回答

1

我知道这是一个相当老的问题,我想我错过了你的交货期限将近4年...但我相信这可以解决你遇到的问题:

private void ShowTooltipBalloon(string title, string msg)
{
    if (this.InvokeRequired)
    {
        this.BeginInvoke(new EventHandler(delegate { ShowTooltipBalloon(title, msg); }));
    }
    else
    {
        // the designer hooks up to this.components
        // so lets do that as well...
        ToolTip tt = new ToolTip(this.components);

        tt.IsBalloon = true;
        tt.ToolTipIcon = ToolTipIcon.Warning;
        tt.ShowAlways = true;
        tt.BackColor = Color.FromArgb(0xFF, 0xFF, 0x90);
        tt.ToolTipTitle = title;

        // Hookup this tooltip to the statusStrip control
        // but DON'T set a value 
        // because if you do it replicates the problem in your image
        tt.SetToolTip(this.statusStrip1, String.Empty); 

        // calc x
        int x = 0;
        foreach (ToolStripItem tbi in this.statusStrip1.Items)
        {
            // find the toolstrip item
            // that the tooltip needs to point to
            if (tbi == this.toolStripDropDownButton1)  
            {
                break;
            }
            x = x + tbi.Size.Width;
        }

        // guestimate y 
        int y = -this.statusStrip1.Size.Height - 50;
        // show it using the statusStrip control 
        // as owner
        tt.Show(msg, this.statusStrip1, x, y, 5000);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接