桌面上方的浮动图标
我正在编写一个C#应用程序,我希望它在桌面上有一个浮动图标(就像移动设备中的Facebook Messenger)。
我一直在网上搜索,但找不到任何有用的东西。 有文章吗? 想法?
您需要创建没有标题栏和边框的表单,并使用图像作为表单的背景。 还可以使图像周围的区域透明。 然后使表格可移动。
- 将
FormBorderStyle
forms设置为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窗体透明背景图像