Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating icon above desktop

Tags:

c#

winforms

wpf

I'm writing a C# application and I want it to have a floating icon over the desktop (like the Facebook messenger in mobile).

I've been searching the internet but couldn't find anything useful. Any articles? Ideas?

like image 804
Noa Barki Avatar asked Dec 07 '25 10:12

Noa Barki


1 Answers

You need to create a form without title bar and border and use an image as background of your form. Also make area around the image, transparent. Then make the form movable.

  • Set the form FormBorderStyle to None
  • Set the form TopMost to true
  • You can also set ShowInTaskbar to false.
  • Set an image as BackgroundImage and set BackgroundImageLayout to Center
  • Set a suitable BackColor for form, for example if your BackGroundImage has Magenta color around, set the BackColor of form to Magenta.
  • Set the TransparencyKey of form to the color you choose for BackColor

This way you will have a shaped form, for example a circle form (if your background image was circle shape).

Then to make the form move by dragging with left mouse button, write this code:

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

protected override void OnMouseDown(MouseEventArgs e)
{
    base.OnMouseDown(e);
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
}

And don't forget to add using System.Runtime.InteropServices;

Here is the image used:

enter image description here

And as you see in the result below, now we have a floating icon above other windows:

enter image description here

To have a high quality Icon with more smooth edges take a look at this post:

  • C# Windows Form Transparent Background Image
like image 118
Reza Aghaei Avatar answered Dec 09 '25 22:12

Reza Aghaei