Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Explorer/Aero style tooltips in C#?

If you use Windows Vista or up, you have probably seen this kind of tooltip, with the coloured text and icon:

enter image description here

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?

like image 446
unrelativity Avatar asked Mar 27 '11 04:03

unrelativity


1 Answers

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.

like image 136
unrelativity Avatar answered Oct 26 '22 14:10

unrelativity