桌面上方的浮动图标

我正在编写一个C#应用程序,我希望它在桌面上有一个浮动图标(就像移动设备中的Facebook Messenger)。

我一直在网上搜索,但找不到任何有用的东西。 有文章吗? 想法?

您需要创建没有标题栏和边框的表单,并使用图像作为表单的背景。 还可以使图像周围的区域透明。 然后使表格可移动。

  • FormBorderStyleforms设置为None
  • 将表单TopMost设置为true
  • 您还可以将ShowInTaskbar设置为false。
  • 将图像设置为BackgroundImage并将BackgroundImageLayout设置为Center
  • 为表单设置合适的BackColor ,例如,如果BackGroundImage周围有Magenta ,请将表单的BackColor设置为Magenta
  • 将窗体的TransparencyKey设置为您为BackColor选择的颜色

这样您就会有一个形状的形状,例如圆形(如果您的背景图像是圆形)。

然后通过用鼠标左键拖动来移动表单,写下以下代码:

 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); } } 

并且不要忘记using System.Runtime.InteropServices;添加using System.Runtime.InteropServices;

这是使用的图像:

在此处输入图像描述

正如您在下面的结果中看到的,现在我们在其他窗口上方有一个浮动图标:

在此处输入图像描述

要获得具有更光滑边缘的高质量图标,请查看此post:

  • C#Windows窗体透明背景图像