1

可能重复:
创建自定义工具提示 C#

Does anyone know of a way to make a box 'popup' when the user cursors over a certain item?

For example, I want to have a PictureBox on a C# forms application and when the user cursors over it, a box of text will pop up.

I'm aware of ToolTip however I was thinking of something more customisable; in my mind I'm thinking of the kind of popup boxes you see in World of WarCraft when you cursor over an item in your inventory (obviously it doesn't have to be THAT flashy, but at least one where the text colour, background colour, text etc. are all modifiable).

4

3 回答 3

3

您可以使用ToolStripControlHost来托管控件(例如面板)并添加所需的内容。然后使用 Items 集合将该控件添加到ToolStripDropDown,并使用Show(Control,Point)方法显示该控件。


以为我会添加一个例子

public class Form1 {
    public Form1() {
        ToolStripDropDown customToolTip = new ToolStripDropDown();
        customToolTip.Items.Add(new CustomPopupControl("Hello", "world"));
        MouseMove += (o, e) => {
            Point location = e.Location;
            location.Offset(0, 16);
            customToolTip.Show(this, location);
        };
    }

    class CustomPopupControl : ToolStripControlHost {
        public CustomPopupControl(string title, string message)
            : base(new Panel()) {
            Label titleLabel = new Label();
            titleLabel.BackColor = SystemColors.Control;
            titleLabel.Text = title;
            titleLabel.Dock = DockStyle.Top;

            Label messageLabel = new Label();
            messageLabel.BackColor = SystemColors.ControlLightLight;
            messageLabel.Text = message;
            messageLabel.Dock = DockStyle.Fill;

            Control.MinimumSize = new Size(90, 64);
            Control.Controls.Add(messageLabel);
            Control.Controls.Add(titleLabel);
        }
    }
}
于 2012-10-23T22:01:03.440 回答
1

我的意思是,如果它是按钮或图像按钮,您可以添加诸如 MouseHover 操作之类的内容,然后显示您的消息

private void button1_MouseHover(object sender, System.EventArgs e) 
{
MessageBox.Show("yourmessage"); 

} 
于 2012-10-23T21:40:39.360 回答
0

您需要自定义工具提示。参考 http://www.codeproject.com/Articles/98967/A-ToolTip-with-Title-Multiline-Contents-and-Image

那里还有其他一些文章,但这一篇对我来说很好。

您可能需要根据您的要求添加代码。

于 2012-10-23T21:57:28.037 回答