桌面上方的浮动图标

时间:2015-12-27 15:41:20

标签: c# wpf winforms

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

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

1 个答案:

答案 0 :(得分:3)

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

  • 将表单FormBorderStyle设置为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;

以下是使用的图像:

enter image description here

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

enter image description here

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