×

C#轻松打造vista透明窗口(xp系统无任何vista主题)(汤贞营作品)

Kalet Kalet 发表于2009-03-20 12:00:14 浏览372 评论0

抢沙发发表评论

自vista出来之后,.vista的areo透明效果取得了消费者的好评.因此vista的areo特效成为了程序员青睐的一种特效.


但要在vista系统上才能运行透明窗体.但这就说明在XP系统就不能实现areo了吗?


回答是否定的.在xp早期的版本下一样可以运行areo透明特效!

C#轻松打造vista透明窗口(xp系统无任何vista主题)(汤贞营作品)

哈,不信你可要看清楚了!!!!!!如下图::



是的.认真看它不是vista系统下的窗口.哈比vista的还先进!!


源代码好长.只给出核心代码!!!


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace VistaForm
{
    public partial class GlassForm : Form
    {
        //private GlassForm vistaForm = null;
        public GlassForm()
        {
            InitializeComponent();
            //FocusForm = this;
            base.Padding = new Padding(7, 24, 7, 7);
            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
        {
            get
            {
                return 1;
            }
        }
        public new Padding Padding
        {
            get
            {
                return base.Padding;
            }
        }


        private Sayes.Controls.Vista.Config.Color _ColorSetting = new Sayes.Controls.Vista.Config.Color();


        private void EnableAllControls()
        {
            if (this==null || this.Controls == null) return;
            foreach (Control ctl in this.Controls) ctl.Enabled = true;
        }
        private void DisableAllControls()
        {
            foreach (Control ctl in this.Controls) ctl.Enabled = false;
        }
        internal Sayes.Controls.Vista.Config.Color ColorSetting
        {
            get { return _ColorSetting; }
        }
        private Region MinRegion = new Region(), MaxRegion = new Region(), CloseRegion = new Region();
       
        private bool IsMinOn = false, IsMaxOn = false, IsCloseOn = false;
        private static GlassForm FocusForm = null;
        protected override void OnDeactivate(EventArgs e)
        {
            base.OnDeactivate(e);
            this.timerBackGround.Start();
            this.DisableAllControls();
        }
        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);


            FocusForm = this;
        
            this.timerBackGround.Stop();
            this.Invalidate();
            this.EnableAllControls();
        }
        protected override void OnLocationChanged(EventArgs e)
        {
            base.OnLocationChanged(e);
            this.Invalidate();
        }
     
        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, 30));//顶
            e.Graphics.DrawImage(global::VistaForm.Properties.Resources.hear, 0, 0, this.Width, 30);
            e.Graphics.CopyFromScreen(this.PointToScreen(new Point(0, 0)), new Point(0, 0), new Size(7, this.Height));//左
            e.Graphics.DrawImage(global::VistaForm.Properties.Resources.wi, 0,30);
           e.Graphics.CopyFromScreen(this.PointToScreen(new Point(this.Width - 7, 0)), new Point(this.Width - 7, 0), new Size(7, this.Height));//右
           e.Graphics.DrawImage(global::VistaForm.Properties.Resources.wi, this.Width - 7, 30);
            e.Graphics.CopyFromScreen(this.PointToScreen(new Point(0, this.Height - 11)), new Point(0, this.Height - 11), new Size(this.Width, 11));//底
            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));
            }
          
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;//.HighQuality;


            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);
            //
            //Pen mypen = new Pen(Color.PaleTurquoise);
            ////Pen mypen = new Pen(Color.White );
            //Graphics myline = this.CreateGraphics();
            //myline.DrawLine(mypen, 2, this.Height - 1, 0, this.Height - 4);
            //


           
            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(6, 28, this.Width - 15, this.Height - 37);//设置边框
            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::VistaForm.Properties.Resources.MinHigh,
                    this.MinRegion.GetBounds(e.Graphics));
            }
            else
            {
                e.Graphics.DrawImage(global::VistaForm.Properties.Resources.Min,
                    this.MinRegion.GetBounds(e.Graphics));
            }
            if (this.IsCloseOn)
            {
                e.Graphics.DrawImage(global::VistaForm.Properties.Resources.CloseHigh,
                    this.CloseRegion.GetBounds(e.Graphics));
            }
            else
            {
                e.Graphics.DrawImage(global::VistaForm.Properties.Resources.Close,
                    this.CloseRegion.GetBounds(e.Graphics));
            }C#轻松打造vista透明窗口(xp系统无任何vista主题)(汤贞营作品)
            if (this.IsMaxOn)
            {
                e.Graphics.DrawImage(global::VistaForm.Properties.Resources.MaxHigh,
                    this.MaxRegion.GetBounds(e.Graphics));


            }
            else
            {
                e.Graphics.DrawImage(global::VistaForm.Properties.Resources.Max,
                    this.MaxRegion.GetBounds(e.Graphics));
            }
        }
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            try
            {
                this.MinRegion = new Region(new Rectangle(this.Width - 110, 1, 30, 20));//三个按钮位置大小等设置
                this.MaxRegion = new Region(new Rectangle(this.Width - 79, 1, 30, 20));
                this.CloseRegion = new Region(new Rectangle(this.Width - 49, 1, 40, 20));
            }
            catch { }
        }
        private Point StartPoint = new Point(0, 0);
        private bool IsMoveClick = false;
        private bool IsClick = false;
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.StartPoint = new Point(e.X, e.Y);


            if (e.Y < 23)
            {
                if (!this.MinRegion.IsVisible(new Point(e.X, e.Y)) && !this.MaxRegion.IsVisible(new Point(e.X, e.Y)) &&
                    !this.CloseRegion.IsVisible(new Point(e.X, e.Y))) this.IsMoveClick = true;
            }
            else this.IsClick = true;
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.Cursor = Cursors.Default;
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            this.Cursor = Cursors.Default;
            if (this.MinRegion.IsVisible(new Point(e.X, e.Y)) && !this.IsMinOn)
            {
                this.IsMinOn = true;
                this.IsMaxOn = false;
                this.IsCloseOn = false;
                this.Cursor = Cursors.Hand;
                this.Invalidate(this.MinRegion);
                this.Invalidate(this.CloseRegion);
                this.Invalidate(this.MaxRegion);
            }
            if (this.MaxRegion.IsVisible(new Point(e.X, e.Y)) && !this.IsMaxOn)
            {
                this.IsMinOn = false;
                this.IsCloseOn = false;
                this.IsMaxOn = true;
                this.Cursor = Cursors.Hand;
                this.Invalidate(this.MinRegion);
                this.Invalidate(this.MaxRegion);
                this.Invalidate(this.CloseRegion);
            }
            if (this.CloseRegion.IsVisible(new Point(e.X, e.Y)) && !this.IsCloseOn)
            {
                this.IsMinOn = false;
                this.IsMaxOn = false;
                this.IsCloseOn = true;
                this.Cursor = Cursors.Hand;
                this.Invalidate(this.MinRegion);
                this.Invalidate(this.CloseRegion);
                this.Invalidate(this.MaxRegion);
            }


            if (this.IsMoveClick)
            {
                this.Left += e.X - this.StartPoint.X;
                this.Top += e.Y - this.StartPoint.Y;
            }



            if (e.X > this.Width - 6 && e.Y >= 23 && e.Y <= this.Height - 6)//right
            {
                this.Cursor = Cursors.SizeWE;
                if (this.IsClick)
                {
                    this.Width += e.X - this.StartPoint.X;
                    this.StartPoint = new Point(e.X, e.Y);
                }
            }
            else if (e.X > this.Width - 6 && e.Y > this.Height - 6)//right bottom corner
            {
                this.Cursor = Cursors.SizeNWSE;
                if (this.IsClick)
                {
                    this.Width += e.X - this.StartPoint.X;
                    this.Height += e.Y - this.StartPoint.Y;
                    this.StartPoint = new Point(e.X, e.Y);
                }
            }
            else if (e.Y > this.Height - 6)//bottom
            {
                this.Cursor = Cursors.SizeNS;
                if (this.IsClick)
                {
                    this.Height -= this.StartPoint.Y - e.Y;
                    this.StartPoint = new Point(e.X, e.Y);
                }
            }//
           
           
            //
            else
            {
                this.Cursor = Cursors.Default;


            }
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (this.MinRegion.IsVisible(new Point(e.X, e.Y)) && this.MinimizeBox)
            {
            
                this.WindowState = FormWindowState.Minimized;
            }
            if (this.MaxRegion.IsVisible(new Point(e.X, e.Y)) && this.MaximizeBox)
            {
                if (this.WindowState == FormWindowState.Normal)
                {
                    this.WindowState = FormWindowState.Maximized;//取大化
                  
                
                  
                }
                else
                {
                    this.WindowState = FormWindowState.Normal;
                  
                }
               
            }
            if (this.CloseRegion.IsVisible(new Point(e.X, e.Y)))
            {
                this.Close();
            }
            this.IsClick = false;
            this.IsMoveClick = false;
            this.StartPoint = new Point(0, 0);
        }


        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlRemoved(e);
            e.Control.LocationChanged += new EventHandler(Control_SizeChanged);
            e.Control.SizeChanged += new EventHandler(Control_SizeChanged);
            this.Control_SizeChanged(e.Control, null);
        }


        void Control_SizeChanged(object sender, EventArgs e)
        {
            Control ctl = (Control)sender;
            if (ctl.Dock != DockStyle.None) return;
            if (ctl.Width > this.Width - 8) ctl.Width = this.Width - 8;
            if (ctl.Height > this.Height - 8) ctl.Height = this.Height - 8;
            if (ctl.Left < 3) ctl.Left = 4;
            if (ctl.Right > this.Width - 3) ctl.Left = this.Width - 4 - ctl.Width;
            if (ctl.Top < 23) ctl.Top = 24;
            if (ctl.Bottom > this.Height - 3) ctl.Top = this.Height - 4 - ctl.Height;
        }


        private void timerBackGround_Tick(object sender, EventArgs e)
        {
            if(this!=FocusForm)this.Invalidate(false);
        }


    }C#轻松打造vista透明窗口(xp系统无任何vista主题)(汤贞营作品)
}



群贤毕至

访客