c# - 如何拥有可以点击的透明表格

标签 c# winforms

我下面的是一个 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);

    }

}
}

最佳答案

试试这个

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
static class Program
{
    [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
    private static extern IntPtr CreateRoundRectRgn
    (
        int nLeftRect, // x-coordinate of upper-left corner
        int nTopRect, // y-coordinate of upper-left corner
        int nRightRect, // x-coordinate of lower-right corner
        int nBottomRect, // y-coordinate of lower-right corner
        int nWidthEllipse, // height of ellipse
        int nHeightEllipse // width of ellipse
     );

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        string PicLocal = @"C:\Projects\Screenshot_1.bmp";

        Form f = new Form() {  FormBorderStyle = FormBorderStyle.None, StartPosition = FormStartPosition.CenterScreen};
        f.BackgroundImage = new Bitmap(PicLocal);
        f.Size = new Size(f.BackgroundImage.Size.Width, f.BackgroundImage.Size.Height);
        f.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, 200, 200, 2000, 2000));
        f.Click += (s, e) => { System.Windows.Forms.MessageBox.Show("Clicked"); };

        Application.Run(f);

    }
}

我在这里找到了一个更简单的解决方案 Make a form's background transparent

你不必使用 Region 属性和 CreateRoundRectRgn ,只要确保你有一个圆形透明图像

 f.BackColor = Color.Magenta;
 f.TransparencyKey = Color.Magenta;

关于c# - 如何拥有可以点击的透明表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24816360/

相关文章:

c# - 有没有更好的方法来格式化这个字符串?

c# - 使用 Entity Framework 查找

c# - 通过异步同步避免死锁并防止 UI 响应

c# - VC# 多鼠标

C#/异步网络和GUI之间的通信

c# - 最好有递减循环?

c# - 在 Xamarin Forms 中的应用程序之间共享数据

c# - 以图形方式模板化 .NET winforms 应用程序

c# - 禁用 TreeView 节点焦点提示

c# - .NET 4.0 中的动态语言运行时有哪些限制?