以下是我的一个dll,当提供pos、pic和透明度时,它会在屏幕上显示图片,而该图片将成为最顶部的对象,您也可以通过该图片进行点击。但问题在于,当使用圆形图片时,圆形的边缘上有白色的边框使其变成正方形。我希望去除那些白色的边框,只留下最上面的圆形,并且也能够进行点击。以下代码中的所有内容都可行,除了当f.TransparencyKey = BackColor;时,我不能再通过图片进行点击,但它确实将图片变成了圆形。现在该怎么做才能既让图片变成圆形又可以进行点击?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace pic
{
public class Class1
{
[DllImport("user32.dll", SetLastError = true)]
private static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll")]
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
public const int GWL_EXSTYLE = -20;
public const int WS_EX_LAYERED = 0x80000;
public const int WS_EX_TRANSPARENT = 0x20;
public const int LWA_ALPHA = 0x2;
public const int LWA_COLORKEY = 0x1;
public void t(int LocalX, int LocalY, string PicLocal, byte transparency)
{
Bitmap bitmap;
Form f = new Form();
f.BackColor = Color.White;
f.FormBorderStyle = FormBorderStyle.None;
f.Bounds = Screen.PrimaryScreen.Bounds;
f.TopMost = true;
bitmap = new Bitmap(PicLocal);
f.Size = new Size(bitmap.Size.Width, bitmap.Size.Height);
f.StartPosition = FormStartPosition.Manual;
f.SetDesktopLocation(LocalX, LocalY);
Application.EnableVisualStyles();
SetWindowLong(f.Handle, GWL_EXSTYLE,
(IntPtr)(GetWindowLong(f.Handle, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT));
// set transparency to 50% (128)
SetLayeredWindowAttributes(f.Handle, 0, transparency, LWA_ALPHA);
f.BackgroundImage = Bitmap.FromFile(PicLocal);
//f.AllowTransparency = true;
//Color BackColor = Color.White;
// Make the background color of form display transparently.
//f.TransparencyKey = BackColor;
Application.Run(f);
}
}
}
^
),只需使用普通的OR(|
)。因此,请将代码更改为:GetWindowLong(f.Handle, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT)
。 - Cody Gray