1. Open Microsoft Visual Studio
2. Create a Windows Application project and set him some name
3. Now you are ready to start adding some code to make it works. In the current state of the program if you start it you will see only one dafault form generated by Visual Studio.
In the next steps we will make more glass in the client area by using the new cool feature gaved us from Windows Vista.
4. In the code editor type below code after class declaration:
[System.Runtime.InteropServices.DllImport("dwmapi.dll")]
public extern static int DwmIsCompositionEnabled(ref int en);
[System.Runtime.InteropServices.DllImport("dwmapi.dll")]
public extern static int DwmExtendFrameIntoClientArea(IntPtr hwnd,
ref MARGINS margin);
public struct MARGINS
{
public int m_Left;
public int m_Right;
public int m_Top;
public int m_Buttom;
}
With this code you activating Interop Services to import the needed by us library to get the glass. Also you declarating DwmExtendFrameIntro and DwmIsCompositionEnabled. With the first declaration you extending the glass into client area and with the second you check if this options is enabled ( to get glass effect ). Afterthat you create a struct MARGINS for the margins of the glass. It can be positioned everywhere on the form.
Also you must add little more code after InitializeComponent();
int en = 0;
MARGINS mg = new MARGINS();
mg.m_button = -1;
mg.m_left = -1;
mg.m_right = -1;
mg.m_top = -1;
//make sure that you using Windows Vista
if (System.Environment.OSVersion.Version.Major >= 6)
{
DwmIsCompositionEnabled(ref en);
//check if desktop compositon is enabled
if (en > 0)
{
DwmExtendFrameIntoClientArea(this.Handle, ref mg);
}
else
{
MessageBox.Show("Desktop Composition isn't enabled. Try to enable it first and run application again.)";
}
}
else
{
MessageBox.Show("Please run this on Windows Vista or high os.");
}
this.BackColor = Color.Black;
Note: Don't forget that the margins are in pixels then twips or something else.
If you trying to use label on the glass and type some text into it you will see that it looks ugly. To fix this problem you will need to type the next code in button's click event or form's paint event.
Graphics g = this.CreateGraphics();
GraphicsPath fP = new GraphicsPath();
fP.AddString("Hello Vista", new FontFamily("Tahoma"),
(int)FontStyle.Regular, 26, new Point(0, 0),
StringFormat.GenericDefault);
g.SmoothingMode = SmoothingMode.HighQuality;
g.FillPath(Brushes.Black, fP);
g.Dispose();
fP.Dispose()
Note: To works all fine in the beginning of the Form1.cs you must set Drawing2D. To make it write the code below:
using System.Drawing.Drawing2D;
5. Now you are ready to start your application and see what you do.
Go to Debug menu and click Start Without Debugging and you will see the result. It must look likes below picture.
© 2007 Chris FAKAR™