怎样实现Vista半透窗体

How to make a Transparent WinForm like VistaForm in VS2005

Introduction

This is article about how to make a transparent form in VS2005 like VistaForm. When I saw the VistaForm at my very first time. I was thinking how to make a form like that. I searched lots fo web sites for a good solution, but I didn't find an answer. Some guys told me to make such a form with 3 forms, I think it's a crasy idea, we can do that, but i don't know how we can use the form in Dialog model. I tried serval ways to implement this form and now the solution i provide maybe a good and simply idea. I'm not really making the form to be tansparent, the code is as following. The most important code is Graphics.CopyFromScreen

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;
    }
}
Collapse
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));
    }
}
 
 以上源码,可以到这里下载:http://download.csdn.net/source/511107
也可以到出处下载: http://www.codeproject.com/KB/aspnet/How_to_make_VistaForm.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值