1

在多层窗口中,我可以点击工作,但在我的窗口中我的背景图片后面仍然有一些灰色,我该如何删除它?我查看了堆栈溢出和代码项目,但找不到解决方案。

这是我的代码:

private Margins marg;
    internal struct Margins 
    {
        public int Left, Right, Top, Bottom;
    }

    public enum GWL
    {
        ExStyle = -20
    }

    public enum WS_EX
    {
        Transparent = 0x20,
        Layered = 0x80000
    }

    public enum LWA
    {
        ColorKey = 0x1,
        Alpha = 0x2
    }

    const Int32 HTCAPTION = 0x02;
    const Int32 WM_NCHITTEST = 0x84;
    const byte AC_SRC_OVER = 0x00;
    const byte AC_SRC_ALPHA = 0x01;

    [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")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetLayeredWindowAttributes(IntPtr hwnd, int crKey, byte bAlpha, LWA dwFlags);

    [DllImport("dwmapi.dll")]
    static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMargins);

    public Form1() {
        InitializeComponent();

        //BackColor = Color.Aqua;
        //TransparencyKey = Color.Aqua;

        StartPosition = FormStartPosition.CenterScreen;
        BackgroundImage = new Bitmap(mypicture);
        BackgroundImageLayout = ImageLayout.Stretch;
        this.Size = mypicture.Size;
        this.FormBorderStyle = FormBorderStyle.None;

        TopMost = true;
        Visible = true;

        int initialsytle = (int) GetWindowLong(this.Handle,-20);
        initialsytle = initialsytle |0x80000 | 0x20;
        IntPtr pointer = (IntPtr) initialsytle;
        SetWindowLong(this.Handle, -20, pointer);
        SetLayeredWindowAttributes(this.Handle, 0, 128, LWA.ColorKey);



    }

    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);

        marg.Left = 0;
        marg.Top = 0;
        marg.Right = this.Width;
        marg.Bottom = this.Height;

        DwmExtendFrameIntoClientArea(this.Handle, ref marg);

    }
}
4

0 回答 0