桌面上方的浮动图标 [英] Floating icon above desktop

查看:48
本文介绍了桌面上方的浮动图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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.

  • 将表单FormBorderStyle设置为None
  • 将表单 TopMost 设置为 true
  • 您还可以将 ShowInTaskbar 设置为 false.
  • 将图像设置为 BackgroundImage 并将 BackgroundImageLayout 设置为 Center
  • 为表单设置合适的BackColor,例如,如果您的BackGroundImage 周围有Magenta 颜色,请设置BackColor> 形式为 Magenta.
  • 将表单的TransparencyKey设置为您为BackColor
  • 选择的颜色
  • 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);
    }
}

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

这是使用的图像:

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

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

要获得边缘更平滑的高质量图标,请查看这篇文章:

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

这篇关于桌面上方的浮动图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆