3

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?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Noa Barki
  • 51
  • 4
  • You need to create a form with `FormBorderStyle` set to `None`. Also if you want to use an image for your form, Set the `BackgroundImage` and the set the `BackColor` and `TransparencyKey` of form. – Reza Aghaei Dec 27 '15 at 16:51

1 Answers1

5

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:

Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Good practice, however I lost the mouse left click events how to avoid this? – SHADOW.NET Aug 07 '19 at 19:25
  • @SHADOW.NET As an option, you can start dragging on mouse move, and make sure the distance of the current mouse position and start point is bugger than a specific value, for example 32. – Reza Aghaei Aug 20 '19 at 03:34