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

2024-03-16

不久前我问了一个与此密切相关的问题:通知用户错误的替代方法 https://stackoverflow.com/questions/2878043/alternative-way-to-notify-the-user-of-an-error

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

现在我已经使用工具提示气球实现了这一点。问题是,即使我给它一个大致位置,气泡的小尖头部分也会根据消息的大小改变位置(参见附图)。通常,我会使用 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);
    }
}

这非常超出了要求的范围,但我的老板是一个注重细节的人,所以除了解决这个问题之外,我还必须快速解决它。我需要一些相对容易实现的东西,不会“破坏”我即将发布的当前软件。

That being said, of course i'll listen to any advice, whether it's implementable or not. At least i might learn something. alt text

*编辑:看来我的图像没有显示。不知道是不是我电脑的问题那好吧...


我知道这是一个相当老的问题,我想我错过了您的交货截止日期近 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);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

工具提示气球显示位置(用于错误通知) 的相关文章

随机推荐