1

In the multilayered window I have the click through working but I still see some gray behind my background picture in my window how do I remove that? I have looked on stack overflow and codeproject but I can't find a solution.

This is my code:

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);

    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
linmic
  • 139
  • 2
  • 10
  • Your transparency key appears to be black. You'll get gray when the image has partially transparent pixels. Typically used at the edge of shapes, Photoshop is prone to do that. Those pixels alter the color from black to a shade of gray. Which no longer matches the transparency key so you'll see them. You'll need a better image. – Hans Passant Mar 01 '13 at 13:50
  • have the same thing if i have no shadows in my picture – linmic Mar 01 '13 at 19:48

0 Answers0