04 October 2007

How to get Windows Vista glass effect in your C# applications ?

This cool feauture of Windows Vista is easy to got in all program languages. For today's example I will show you how to get the glass effect using C# and draw some text on it. Enjoy it.

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™

За BG юзърите

Страницата е още в процес на развитие. В бъдеще тя ще претърпи промени които ще са от значение за програмистите занимаващи се с Visual Basic и C# ( а може би и не само за тях ).

03 October 2007

Welcome at Developer's Place (created on 03.10.2007)

You're welcome to my personal web page. In future you must expect to view my newest publications here in the blog. You will see mainly topics for Windows XP/Vista, Visual Studio 2005 ( Visual Basic & C# ) and also from Visual Basic 6.0 Pro.

PS: Thanks to all Skype users that see my blog...
=====================================================================
Добре дошли в моята персонална уеб страница. В бъдеще очаквайте да видите моите най-нови публикации тук. Ще виждате главно теми за Windows XP/Vista, Visual Studio 2005 ( Visual Basic & C# ) и също информация за Visual Basic 6.0 Pro.

ПС: Благодаря на всички Skype потребители които гледат тази страница...

Enjoy