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

news/2024/11/15 1:32:38/

 

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

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

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

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

是的.认真看它不是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));
            }
            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);
        }

    }
}


http://www.ppmy.cn/news/652469.html

相关文章

Windows 2008 如何开启并使用Windows Vista的主题

据说 Windows Server 2008中使用Vista相同的核心。 默认情况下&#xff0c;Windows Server 2008的界面是比较单调的&#xff0c;这是因为作为服务器操作系统&#xff0c;Windows Server 2008默认并没有启用主题服务和Vista界面&#xff0c;所以今天就教大家如何在Windows Serve…

在主线程中为子线程解锁_在XP中为Google Chrome启用Vista黑色风格主题

在主线程中为子线程解锁 If you’ve seen the screenshots of Google Chrome on XP vs Vista, you’ve probably noticed that the interface looks completely different. If you want to make your XP version use a slick black theme, you can do so with a custom hack. 如…

W2k8应用vista主题

简单的将vista主题应用到2008系统 系统自带 下面以图说话 1&#xff09;打开服务器管理器----->角色------>添加角色------桌面体验 2&#xff09;安装时会提示重启 &#xff0c;重启进入系统后 打开服务 将Theme主题服务启动 3&#xff09;鼠标在桌面右击 -->个性…

Windows Server 2008 如何实现Vista主题

Windows Server 2008操作系统有很多功能在默认情况下是没有安装的&#xff0c;这就需要用户在系统安装成功后手动安装添加的。桌面主题就是其中一个功能&#xff0c;下面简单介绍一下桌面主题的安装方法&#xff1a; 1、打开【服务器管理器】 2、选择【功能】&#xff0c;在右…

怨念主题第2作~粉红色超级女人vista主题

下载地址&#xff1a;234jo02pinkvista.rar 今天怨念大爆发&#xff0c;突然间想要做一个超级女人风的主题。。。。 没事干时候翻翻电脑&#xff0c;发现俺自己真的实在是太男人了。 朋友过来拷软件&#xff0c;我给她推荐一些好看的主题&#xff0c;结果她默了很久 然后小心…

美化windows 完美仿真Vista -- Vista风格包4.0

http://count.crsky.com/view_down.asp?down_urlhttp://2.gddx1.crsky.com/200611/VistaShow-v4.0.rar&downd_id14&ID30805&SOFTID9036&downyes

Vista主题包(高仿真)_方舟系统站

Vista主题包&#xff08;高仿真&#xff09;_方舟系统站 第四步&#xff0c;外观主题美化&#xff1a;外观主题美化目录里的MV5505目录是VS主题外观文件&#xff0c;目前在 ... 然后在桌面上用右键顺序点击桌面-显示属性-外观-窗口和按钮-中选择这个MV5505 ...

火狐强制刷新_强制Firefox 3在Windows XP中使用Vista默认主题

火狐强制刷新 This article was written by Leon Steadman, the same helpful reader that showed us how to Use a Custom Theme for the XP Classic Logon Screen. 本文由Leon Steadman撰写&#xff0c;Leon Steadman是向我们展示如何对 XP Classic Logon Screen使用自定义主…