开发者

Windows Explorer/Aero style tooltips in C#?

If you use Windows Vista or开发者_如何学Python up, you have probably seen this kind of tooltip, with the coloured text and icon:

Windows Explorer/Aero style tooltips in C#?

I've searched using various keywords e.g. Explorer, Aero, Windows, tooltips, and haven't come across any useful information on how to achieve this.

Preferably, I'd like the solution to be for WinForms. Has anyone had any luck?


This blog post on wyDay has the solution.

It links to a 3 part series called "Shell Style Drag and Drop in .NET":

  • Shell Style Drag and Drop in .NET (WPF and WinForms)
  • Shell Style Drag and Drop in .NET - Part 2
  • Shell Style Drag and Drop in .NET - Part 3

The archive linked in part 3 is no longer available, but there appears to be copy of its contents here. Note that in order to compile, you may need to set the DragDropLib and WpfDragDropLib projects to allow unsafe code.

There's samples inside, but for convenience, here's an extract:

#region Drop target accepting FileDrop

private void textBox2_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        e.Effect = e.AllowedEffect & DragDropEffects.Copy;
        DropTargetHelper.DragEnter(textBox2, e.Data, new Point(e.X, e.Y), e.Effect, "Copy to %1", "Here");
    }
    else
    {
        e.Effect = DragDropEffects.None;
        DropTargetHelper.DragEnter(textBox2, e.Data, new Point(e.X, e.Y), e.Effect);
    }
}

private void textBox2_DragOver(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = e.AllowedEffect & DragDropEffects.Copy;
    else
        e.Effect = DragDropEffects.None;
    DropTargetHelper.DragOver(new Point(e.X, e.Y), e.Effect);
}

private void textBox2_DragLeave(object sender, EventArgs e)
{
    DropTargetHelper.DragLeave(textBox2);
}

private void textBox2_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = e.AllowedEffect & DragDropEffects.Copy;
    else
        e.Effect = DragDropEffects.None;
    DropTargetHelper.Drop(e.Data, new Point(e.X, e.Y), e.Effect);

    if (e.Effect == DragDropEffects.Copy)
        AcceptFileDrop(textBox2, e.Data);
}

#endregion // Drop target accepting FileDrop

From my experimentation it seems that I can just write e.Effect = DragDropEffects.Copy; instead of e.Effect = e.AllowedEffect & DragDropEffects.Copy;; though I currently don't understand what the & is there for, so someone might be able to help me with that. As well as that, it seems that the text drop type won't show the description tooltip.

Otherwise, I'm definitely very happy with this.

Hope this helps anyone with this issue as well.


Which technology do you want to use? WPF or WinForms? If you use WPF you could implement your own tooltip using a Popup control and implement a custom tooltip yourself.

You will have to attach your tooltip popup to mouse move events and place the control relatively to the mouse cursor.

This method requires only small effort and you can customize your tooltip using everything WPF has to offer - Images, Animations, etc.

Resources WPF:

  • http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.aspx
  • http://dotnet.dzone.com/articles/understanding-wpf-popups
  • http://www.c-sharpcorner.com/UploadFile/mahesh/WPFPopup08172008075339AM/WPFPopup.aspx

Maybe WinForms has a popup like control. Implementing one yourself should not be very hard, too.

Take a look how to position a Control in WinForms not using the designer (make sure your control is positioned on top of all other controls). If you got this glue its position to your mouse cursor via mouse move events and you are done.

Resources WinForms:

  • http://andrusdevelopment.blogspot.com/2007/10/implementing-custom-tooltip-in-c.html
  • http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=Custom+tooltip+C%23
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜