简体   繁体   中英

How to show kind of notification (toast message) within Winform?

Let's take an example from android. Assume you send a message to someone and after message was send you can see (for a few seconds) kind of notification on the screen like Your message was send .

在此处输入图像描述

That is exactly what I would like to find in Winform. In my Winform app user click on the button and I would like to make kind of UI response, show him a message for a few sec, like Button clicked .

How to do it?

PS Actually I tried to find out how to do it, but everything that I found is kind of notification on the screen at the right bottom corner. It is not actually what I am looking for. I need something like you can see on screenshot. This text should appear in the form, not on the corner of the screen.

PS 2 Tooltip also not what I am looking for. Tooltip is something that binded close to the button(view). I need kind of general UI response. User click on buttons and instead to show him a dialog that force user to move his mouse and close the dialog, I need kind of softy message that disappear after a few sec.

I need kind of softy message that disappear after a few sec.

I think the tooltips are what you are looking for. The idea is you can programmatically control where to show a tooltip and when to hide it.

Please start a new WinForms project. Add three buttons, ToolTip and Timer to the form. Write the next event handlers (and bind them to the corresponding components):

    private void button_Click(object sender, EventArgs e)
    {            
        toolTip1.Show(((Button)sender).Text + " is pressed", this, 300, 300);
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Enabled = false;
        toolTip1.Hide(this);
    }

After demo starting you'll see a tooltip with certain text appearing at the same position for the 1 second.

@Miamy's solution not a bad one, however you have to center text in notification box, and you don't need to use timer. Here the custom tooltip class which centers the tooltip text location and you can see the output below:

在此处输入图像描述

Moreover I added some coloring features along with default timer implementation.

class CustomToolTip : ToolTip
{

    public int SIZE_X = 500;
    public int SIZE_Y = 50;

    public CustomToolTip()
    {
        this.OwnerDraw = true;
        this.Popup += new PopupEventHandler(this.OnPopup);
        this.Draw += new DrawToolTipEventHandler(this.OnDraw);
    }

    string m_EndSpecialText;
    Color m_EndSpecialTextColor = Color.Black;

    public Color EndSpecialTextColor
    {
        get { return m_EndSpecialTextColor; }
        set { m_EndSpecialTextColor = value; }
    }

    public string EndSpecialText
    {
        get { return m_EndSpecialText; }
        set { m_EndSpecialText = value; }
    }

    private void OnPopup(object sender, PopupEventArgs e) // use this event to set the size of the tool tip
    {
        e.ToolTipSize = new Size(SIZE_X, SIZE_Y);
    }


    private void OnDraw(object sender, DrawToolTipEventArgs e) // use this event to customise the tool tip
    {
        Graphics g = e.Graphics;

        LinearGradientBrush b = new LinearGradientBrush(e.Bounds,
            Color.AntiqueWhite, Color.LightCyan, 45f);

        g.FillRectangle(b, e.Bounds);

        g.DrawRectangle(new Pen(Brushes.Black, 1), new Rectangle(e.Bounds.X, e.Bounds.Y,
            e.Bounds.Width - 1, e.Bounds.Height - 1));


        System.Drawing.Size toolTipTextSize = TextRenderer.MeasureText(e.ToolTipText, e.Font);

        g.DrawString(e.ToolTipText, new Font(e.Font, FontStyle.Bold), Brushes.Black,
            new PointF((SIZE_X - toolTipTextSize.Width)/2, (SIZE_Y - toolTipTextSize.Height) / 2)); 

        b.Dispose();
    }
}

You can call this tooltip as below code:

                CustomToolTip notifyError = new CustomToolTip();
                notifyError.Show("Please enter a number.", this, 100, 100, 2000);

Above code creates notification box at 100, 100 location for 2 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM