Click here to Skip to main content
12,697,178 members (25,927 online)
Click here to Skip to main content
Add your own
alternative version

Stats

96.9K views
833 downloads
42 bookmarked
Posted

How to make a transparent WinForm like a Vista form in VS2005

, 27 Aug 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
We can modify a WinForm's opacity in .NET, but we can not make a form like Vista form, whose header is transparent, but the rest of the form is not.

Sample Image - How_to_make_VistaForm.jpg

Introduction

This is article about how to make a transparent form in VS2005 like a Vista form. When I saw the Vista form the very first time, I started thinking about how to make a form like that. I searched a lot of web sites for a good solution, but didn't find an answer. Some guys told me to make a form using three different forms, but I think that is a crazy idea. We could do that, but I don't know how we cold then use it in a dialog model. I tried several ways to implement this form, and the solution I provide here is a good and simple one. I'm not really making the form transparent. The code follows. The most important part is the Graphics.CopyFromScreen method.

Source Code

e.Graphics.CopyFromScreen(this.PointToScreen(new Point(0, 0)), 
           new Point(0, 0), new Size(this.Width, 23));
e.Graphics.CopyFromScreen(this.PointToScreen(new Point(0, 0)), 
           new Point(0, 0), new Size(3, this.Height));
e.Graphics.CopyFromScreen(this.PointToScreen(new Point(this.Width - 4, 0)), 
           new Point(this.Width - 4, 0), new Size(3, this.Height));
e.Graphics.CopyFromScreen(this.PointToScreen(new Point(0, 
           this.Height - 4)), new Point(0, this.Height - 4), 
           new Size(this.Width, 3));

Key Point

public GlassForm()
{
    InitializeComponent();
    //FocusForm = this;
    base.Padding = new Padding(4, 24, 5, 4);
    //this property is very important to make form repaint
    base.Opacity = 0.99;
    this.FormBorderStyle = FormBorderStyle.None;
    SetStyle(ControlStyles.UserPaint, true);
    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    SetStyle(ControlStyles.ResizeRedraw, true);
}
public new double Opacity
//we make a new Opcity property to stop any modify 
//of this property from outside
{
    get
    {
        return 1;
    }
}

protected override void OnPaint(PaintEventArgs e)
{ 
    base.OnPaint(e);
    e.Graphics.Clear(System.Drawing.Color.White);
    e.Graphics.CopyFromScreen(this.PointToScreen(new Point(0, 0)), 
                              new Point(0, 0), new Size(this.Width, 23));
    e.Graphics.CopyFromScreen(this.PointToScreen(new Point(0, 0)), 
                              new Point(0, 0), new Size(3, this.Height));
    e.Graphics.CopyFromScreen(this.PointToScreen(new Point(this.Width - 4, 0)), 
                              new Point(this.Width - 4, 0), 
                              new Size(3, this.Height));
    e.Graphics.CopyFromScreen(this.PointToScreen(new Point(0, 
                              this.Height - 4)), 
                              new Point(0, this.Height - 4), 
                              new Size(this.Width, 3));
    if (FocusForm==this)
    {
        e.Graphics.FillRectangle(new SolidBrush(
                   this.ColorSetting.ActiveHeaderColor),
        new Rectangle(new Point(0, 0), this.Size));
    }
    else
    {
        e.Graphics.FillRectangle(new SolidBrush(
                   this.ColorSetting.HeaderColor),
        new Rectangle(new Point(0, 0), this.Size));
    }

    //.HighQuality;
    e.Graphics.SmoothingMode = 
         System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
    System.Drawing.Drawing2D.GraphicsPath path = 
           new System.Drawing.Drawing2D.GraphicsPath();

    path.AddLine(2, 0, this.Width - 4, 0);
    path.AddLine(this.Width - 4, 0, this.Width - 2, 2);
    path.AddLine(this.Width - 2, 2, this.Width - 2, this.Height - 4);
    path.AddLine(this.Width - 2, this.Height - 4, 
                 this.Width - 4, this.Height - 2);
    path.AddLine(this.Width - 4, this.Height - 2, 2, this.Height - 2);
    path.AddLine(2, this.Height - 2, 0, this.Height - 4);
    path.AddLine(0, this.Height - 4, 0, 2);
    path.AddLine(0, 2, 2, 0);

    System.Drawing.Drawing2D.GraphicsPath pathFrame = 
         new System.Drawing.Drawing2D.GraphicsPath();

    pathFrame.AddLine(2, 0, this.Width - 3, 0);
    pathFrame.AddLine(this.Width - 3, 0, this.Width - 1, 2);
    pathFrame.AddLine(this.Width - 1, 2, this.Width - 1, this.Height - 3);
    pathFrame.AddLine(this.Width - 1, this.Height - 4, 
                      this.Width - 4, this.Height - 1);
    pathFrame.AddLine(this.Width - 4, this.Height - 1, 2, this.Height - 1);
    pathFrame.AddLine(2, this.Height - 1, 0, this.Height - 4);
    pathFrame.AddLine(0, this.Height - 4, 0, 2);
    pathFrame.AddLine(0, 2, 2, 0);

    this.Region = new Region(pathFrame);
    //e.Graphics.FillPath(new SolidBrush(Color.FromArgb(30,
    //                    this.HeaderColor)), pathFrame);
    this.BackColor = this.ColorSetting.BackColor;

    if (FocusForm == this)
        e.Graphics.DrawPath(new 
             Pen(this.ColorSetting.ActiveBorderColor), path);
    else
        e.Graphics.DrawPath(new Pen(this.ColorSetting.BorderColor), path);
    Rectangle clientRegion = new Rectangle(3, 23, 
                             this.Width - 8, this.Height - 28);
    e.Graphics.FillRectangle(new SolidBrush(
               this.ColorSetting.BackColor), clientRegion);

    if (FocusForm == this)
        e.Graphics.DrawRectangle(new 
            Pen(this.ColorSetting.ActiveBorderColor), clientRegion);
    else
        e.Graphics.DrawRectangle(new 
            Pen(this.ColorSetting.BorderColor), clientRegion);
    if(this.BackgroundImage!=null)
        e.Graphics.DrawImage(this.BackgroundImage, clientRegion);
    if(this.Icon!=null)
        e.Graphics.DrawIcon(this.Icon, new Rectangle(3, 3, 16, 16));

    Font f = new Font("ArialBlack", (float)9,FontStyle.Bold);
    e.Graphics.DrawString(this.Text, f, Brushes.White, 21, 3);
    e.Graphics.DrawString(this.Text, f, Brushes.White, 23, 3);
    e.Graphics.DrawString(this.Text, f, Brushes.White, 22, 2);
    e.Graphics.DrawString(this.Text, f, Brushes.White, 22, 4);
    e.Graphics.DrawString(this.Text, f, Brushes.Black, 22, 3);

    if (this.IsMinOn)
    {
        e.Graphics.DrawImage(
            global::Sayes.Controls.Vista.Properties.Resources.MinHigh,
            this.MinRegion.GetBounds(e.Graphics));
    }
    else
    {
        e.Graphics.DrawImage(
            global::Sayes.Controls.Vista.Properties.Resources.Min,
            this.MinRegion.GetBounds(e.Graphics));
    }
    if (this.IsCloseOn)
    {
        e.Graphics.DrawImage(
            global::Sayes.Controls.Vista.Properties.Resources.CloseHigh,
            this.CloseRegion.GetBounds(e.Graphics));
    }
    else
    {
        e.Graphics.DrawImage(
            global::Sayes.Controls.Vista.Properties.Resources.Close,
            this.CloseRegion.GetBounds(e.Graphics));
    }
    if (this.IsMaxOn)
    {
        e.Graphics.DrawImage(
            global::Sayes.Controls.Vista.Properties.Resources.MaxHigh,
            this.MaxRegion.GetBounds(e.Graphics));
    }
    else
    {
        e.Graphics.DrawImage(
            global::Sayes.Controls.Vista.Properties.Resources.Max,
            this.MaxRegion.GetBounds(e.Graphics));
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

WillCaptain
China China
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
liusvelss3-Feb-11 20:28
memberliusvelss3-Feb-11 20:28 
General还是我们中国人厉害啊 哈哈 Pin
iwanna_20102-Feb-10 15:25
memberiwanna_20102-Feb-10 15:25 
GeneralRe: 还是我们中国人厉害啊 哈哈 Pin
WillCaptain2-Feb-10 15:32
memberWillCaptain2-Feb-10 15:32 
GeneralRe: 还是我们中国人厉害啊 哈哈 Pin
xuechengang@gmail.com14-Aug-14 22:48
memberxuechengang@gmail.com14-Aug-14 22:48 
Generallearned it!!! Pin
iwanna_20102-Feb-10 15:24
memberiwanna_20102-Feb-10 15:24 
Generalgreat! Pin
francis36-Oct-09 17:18
memberfrancis36-Oct-09 17:18 
GeneralGerat Article, Well Done Pin
seanco6-Oct-07 6:05
memberseanco6-Oct-07 6:05 
GeneralBasic version [modified] Pin
charlievs9-Dec-06 19:41
membercharlievs9-Dec-06 19:41 
GeneralRe: Basic version Pin
CaptainZhang10-Dec-06 3:29
memberCaptainZhang10-Dec-06 3:29 
GeneralRe: Basic version Pin
NianTzerLow14-Jan-07 7:48
memberNianTzerLow14-Jan-07 7:48 
GeneralCan't you just set the form's opacity level less than 100%... Pin
Jun Du7-Nov-06 11:47
memberJun Du7-Nov-06 11:47 
GeneralRe: Can't you just set the form's opacity level less than 100%... Pin
CaptainZhang10-Dec-06 3:28
memberCaptainZhang10-Dec-06 3:28 
GeneralRe: Can't you just set the form's opacity level less than 100%... Pin
NianTzerLow14-Jan-07 7:42
memberNianTzerLow14-Jan-07 7:42 
GeneralResize Pin
shubudubi1-Sep-06 5:25
membershubudubi1-Sep-06 5:25 
GeneralImpossible to read Pin
DABBee29-Aug-06 13:13
memberDABBee29-Aug-06 13:13 
GeneralRe: Impossible to read Pin
CaptainZhang31-Aug-06 0:15
memberCaptainZhang31-Aug-06 0:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 27 Aug 2006
Article Copyright 2006 by WillCaptain
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid