Sunday, August 22, 2010

Creating C# Glass Form

I’ll show you how to convert your forms background to glass. Glass effect is only for Windows 7 and Vista. Glass Effect wont work with Windows XP. First, we’ll call the required DLL’s.
[System.Runtime.InteropServices.DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();

[System.Runtime.InteropServices.DllImport("DwmApi.dll")]
public static extern int DwmExtendFrameIntoClientArea(
IntPtr hwnd,
ref MARGINS pMarInset);
Then we'll write the method that determines the usage of glass effect on our form.
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cybottomHeight;
};


After that we'll write a method

void glassform()
{

MARGINS margins = new MARGINS();
margins.cxLeftWidth = -1; //-1 means all the form
margins.cxRightWidth = -1;
margins.cyTopHeight = -1;
margins.cyBottomHeight = -1;

if (DwmIsCompositionEnabled() == true)
{
IntPtr hWnd = this.Handle;
int result = DwmExtendFrameIntoClientArea(hWnd, ref margins);
}
else
{
this.BackColor = System.Drawing.Color.White;
}


}

And last we’ll call the glassform method from form_load


here’s the result


5 comments:

  1. does not works for me

    ReplyDelete
  2. This code, just remove the border from the inside of form!

    ReplyDelete
  3. Well done
    this doesn't "just remove the border from the inside of form!"
    its glass extension

    ReplyDelete
  4. not working for me either

    ReplyDelete
    Replies
    1. DwmExtendFrameIntoClientArea render black as glass so you should set form back color to black

      Delete

Followers