Click here to Skip to main content
15,881,687 members
Articles / Desktop Programming / Windows Forms

Drawing smooth text and pictures on the extended glass area of your WinForm in Windows Vista

Rate me:
Please Sign up or sign in to vote.
4.71/5 (60 votes)
10 Jul 2008GPL34 min read 474.1K   7K   162  
This article tells you how to draw text and pictures correctly on your Vista form's extended glass area.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D ;
using System.Text;
using System.Windows.Forms;

namespace textonglass
{
    public partial class Form1 : Form
    {
        int en;
        GraphicsPath fontpath;
        GraphicsPath glowpath;
        PathGradientBrush pathbrush;
        Graphics gph;

        public Form1()
        {
            InitializeComponent();

            if (System.Environment.OSVersion.Version.Major >= 6)  //make sure you are not on a legacy OS 
            {
                en = 0;
                MARGINS mg = new MARGINS();
                mg.m_Buttom = 0;
                mg.m_Left = 0;
                mg.m_Right = 0;
                mg.m_Top = pictureBox1.Height;
                 

                    DwmIsCompositionEnabled(ref en);  //check if the desktop composition is enabled
                    if (en > 0)
                    {
                        DwmExtendFrameIntoClientArea(this.Handle, ref mg);


                    }
                    else
                    {
                        MessageBox.Show("Desktop composition is disabled");
                    }
 
            }
            else
            {
                //MessageBox.Show("Why not try this on Vista?");
            }
            this.Paint += new PaintEventHandler(Form1_Paint);
            this.Shown += new EventHandler(Form1_Shown);
            

        }

        void Form1_Shown(object sender, EventArgs e)
        {
      
        }

        void Form1_Paint(object sender, PaintEventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
            GlassText glasstxt = new GlassText();
            glasstxt.FillBlackRegion(e.Graphics, pictureBox1.ClientRectangle);
            //Bitmap membitmap = new Bitmap(pictureBox1.ClientRectangle.Width,
                //pictureBox1.ClientRectangle.Height);
            //membitmap.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb;
            //membitmap.RawFormat = System.Drawing.Imaging.ImageFormat.MemoryBmp;
            e.Graphics.DrawImage(Properties.Resources.ksokoban,0,0);
            
        }

        




        private void button1_Click(object sender, EventArgs e)
        {

            this.Refresh();  //clear the glass

            gph = this.CreateGraphics();
            gph.DrawImage(textonglass.Properties.Resources.ksokoban, new Point(0, 0));
            gph.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Refresh();  //clear the glass


            gph = this.CreateGraphics();
            Rectangle rc = new Rectangle(0, 0, 140, 30);
            glowpath = new GraphicsPath();
            fontpath = new GraphicsPath();


            glowpath.AddEllipse(rc);
            fontpath.AddString("Hello Vista", new FontFamily("Tahoma"), 
                (int)FontStyle.Regular, 26, new Point(0, 0), StringFormat.GenericDefault); //Add the string to the path
            pathbrush = new PathGradientBrush(glowpath);                                    //init the glowpathbrush
            Color[] cr ={ Color.Transparent };
            gph.SmoothingMode = SmoothingMode.HighQuality;   //must set, or the text smoothing will not work
            pathbrush.CenterColor = Color.White;
            pathbrush.SurroundColors = cr;


            gph.FillEllipse(pathbrush, rc);         //draw the "glow"
            gph.FillPath(Brushes.Black, fontpath);  //draw the black text

            //dispose all objects
            pathbrush.Dispose();
            fontpath.Dispose();
            glowpath.Dispose();
            gph.Dispose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Refresh();  //clear the glass

            gph = this.CreateGraphics();
            fontpath = new GraphicsPath();

            fontpath.AddString("Hello Vista", new FontFamily("Tahoma"),
                (int)FontStyle.Regular, 26, new Point(0, 0), StringFormat.GenericDefault);   //Add the string to the path

            gph.SmoothingMode = SmoothingMode.HighQuality;                                    //must set, or the text smoothing will not work
            gph.FillPath(Brushes.White , fontpath);                                           //draw the font with white color


            //dispose all objects
            gph.Dispose();
            fontpath.Dispose();


        }

        #region DWM API
         
        public struct MARGINS
        {
            public int m_Left;
            public int m_Right;
            public int m_Top;
            public int m_Buttom;
        };


        [System.Runtime.InteropServices.DllImport("dwmapi.dll")]
        public extern static int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margin);


        [System.Runtime.InteropServices.DllImport("dwmapi.dll")]
        public extern static int DwmIsCompositionEnabled(ref int en);



        #endregion

        private void button4_Click(object sender, EventArgs e)
        {
            GlassText glasstxt = new GlassText();
            glasstxt.FillBlackRegion(this.pictureBox1.CreateGraphics(), pictureBox1.ClientRectangle);
            glasstxt.DrawTextOnGlass(this.Handle, lblText.Text, lblText.Font, lblText.Bounds,10 );
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
China China
Pang is experienced in developing IM system, distributed backend caching & storage system for large scale internet services. He also has a broad interest in playing around all kinds of programming stuff, like hacking, UI programming. He's current project is MSNPSharp, a C# implementation of MSNP API library which allows you to develop your own MSN client and bot. Please see http://code.google.com/p/msnp-sharp/

Comments and Discussions