zoukankan      html  css  js  c++  java
  • 1.窗体与界面设计-窗体效果

    在项目开发中的窗体的设计会影响用户对软件的整体印象,因此窗体的效果要设计得美观一些。

    029 制作鼠标穿透窗体

    在对桌面进行操作时,为了使桌面更加美观,可以在桌面的上面加一层类似于玻璃的效果,用户可以用鼠标透过“玻璃”对桌面进行操作。本实例通过使用鼠标穿透窗体类实现以上功能。主要用到了 API 函数 SetWindowLong 和 GetWindowLong。

    1.创建一个项目,默认窗体为 Form1,将该窗体的 FormBordeStyle 属性设置为 None,BackColor 属性设置为 Gainsboro,Opacity 属性设置为 60%,WindowState 属性设置为 Maximized。

    2.在 Form1 窗体中添加一个 NotifyIcon 控件,并设置其 Icon 属性为指定的图标,显示提示信息;添加一个 ContextMenuStrip 控件,作为程序的快捷菜单。

    namespace _029_MouseThroughForm
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private const uint WS_EX_LAYERED = 0x80000;
            private const int WS_EX_TRANSPARENT = 0x20;
            private const int GWL_EXSTYLE = (-20);
            private string Var_genre = "";//记录当前操作的类型
    
            #region 在窗口结构中为指定的窗口设置信息
            /// <summary>
            /// 在窗口结构中为指定的窗口设置信息
            /// </summary>
            /// <param name="hwnd">欲为其取得信息的窗口的句柄</param>
            /// <param name="nIndex">欲取回的信息</param>
            /// <param name="dwNewLong">由nIndex指定的窗口信息的新值</param>
            /// <returns></returns>
            [DllImport("user32", EntryPoint = "SetWindowLong")]
            private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
            #endregion
    
            #region 从指定窗口的结构中取得信息
            /// <summary>
            /// 从指定窗口的结构中取得信息
            /// </summary>
            /// <param name="hwnd">欲为其获取信息的窗口的句柄</param>
            /// <param name="nIndex">欲取回的信息</param>
            /// <returns></returns>
            [DllImport("user32", EntryPoint = "GetWindowLong")]
            private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
            #endregion
    
            #region 使窗口有鼠标穿透功能
            /// <summary>
            /// 使窗口有鼠标穿透功能
            /// </summary>
            private void CanPenetrate()
            {
                uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE);   //从当前窗口的结构中取得信息
                //在窗口结构中为当前窗口设置信息
                uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
            }
            #endregion
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.ShowInTaskbar = false;//窗体不出现在Windows任务栏中
                CanPenetrate();//自定义方法,用来通过API函数SetWindowLong和GetWindowLong实现鼠标的穿透效果
                this.TopMost = true;//使窗体始终在其它窗体之上
            }
    
            #region 设置颜色和透明度的状态
            /// <summary>
            /// 设置颜色和透明度的状态
            /// </summary>
            private void SetEstate(Form Frm, object sender)
            {
                Var_genre = ((ToolStripMenuItem)sender).Name;
                string Tem_Str = Var_genre;
                if (Var_genre.IndexOf('_') >= 0)
                {
                    Var_genre = Tem_Str.Substring(0, Tem_Str.IndexOf('_'));
                }
    
                switch (Var_genre)
                {
                    case "ToolColor":
                        {
                            Color Tem_Color = Color.Gainsboro;
                            switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()))
                            {
                                case 1: Tem_Color = Color.Gainsboro; break;
                                case 2: Tem_Color = Color.DarkOrchid; break;
                                case 3: Tem_Color = Color.RoyalBlue; break;
                                case 4: Tem_Color = Color.Gold; break;
                                case 5: Tem_Color = Color.LightGreen; break;
                            }
                            Frm.BackColor = Tem_Color;
                            break;
                        }
                    case "ToolClarity":
                        {
                            double Tem_Double = 0.0;
                            switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()))
                            {
                                case 1: Tem_Double = 0.1; break;
                                case 2: Tem_Double = 0.2; break;
                                case 3: Tem_Double = 0.3; break;
                                case 4: Tem_Double = 0.4; break;
                                case 5: Tem_Double = 0.5; break;
                                case 6: Tem_Double = 0.6; break;
                                case 7: Tem_Double = 0.7; break;
                                case 8: Tem_Double = 0.8; break;
                                case 9: Tem_Double = 0.9; break;
    
                            }
                            Frm.Opacity = Tem_Double;
                            break;
                        }
                    case "ToolAcquiescence":
                        {
                            Frm.BackColor = Color.Gainsboro;
                            Frm.Opacity = 0.6;
                            break;
                        }
                    case "ToolClose":
                        {
                            Close();
                            break;
                        }
    
                }
            }
            #endregion
    
            private void ToolColor_Glass_Click(object sender, EventArgs e)
            {
                SetEstate(this, sender);
            }
        }
    }

    030 窗体换肤程序

    本实例的基本原理是给窗体的各个组成部分更换图片,基于该原理,首先需要分析窗体的组成部分,主要包括标题栏、左边框、右边框、下边框、窗体中间区域及可能存在的菜单栏,其中标题栏和3个边框无法通过设置相关属性来达到更换背景图片的目的。对于这个问题,可以通过取消窗体的 FormBorderStyle 属性,同时在标题栏和3个边框的位置添加 Panel 控件来解决,然后通过选择不同的皮肤类型,为窗体的各个组成部分设置图片,最终达到窗体换肤的效果。在以上过程中,最主要的技术问题是如何从指定的文件创建 Image 对象,以及如何获取图片的路径。

    1.创建一个项目,默认窗体为 Form1,设置 Form1 的 AutoscaleMode 属性为 Inherit,DoubleBuffered 属性为 True,IsMdiContainer 属性为 True。

    2.在窗体中添加6个 Panel 控件,分别用来作为窗体的标题栏、下边框、左边框、右边框、左下角边框和右下角边框;添加一个 ContextMenuStrip 控件,用来作为更换皮肤的快捷菜单;添加3个 PictureBox 控件,分别用来显示最大化、最小化和关闭图片。

    namespace _030_WinCusSkin
    {
        public partial class Form1 : Form
        {
            String strImagesPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\")).LastIndexOf("\"));
            int top, left, hei, wid;
            bool bol = false, bo = false, bolTop = false, bolLeft = false, bolRight = false, bolBottom = false, bolLeftCornu = false, bolRightCornu = false;
            int x = 0, y = 0;
            public Form1()
            {
                InitializeComponent();
            }
    
            //关闭事件。
            private void picClose_Click(object sender, System.EventArgs e)
            {
                Application.Exit();
            }
            //最大化事件。  
            private void picMaximize_Click(object sender, System.EventArgs e)
            {
                if (!bol)   //若窗体处于普通状态
                {
                    top = this.Top;     //获取窗体的Top属性值
                    left = this.Left;   //获取窗体的Left属性值
                    hei = this.Height;  //获取窗体的Height属性值
                    wid = this.Width;   //获取窗体的Width属性值
                    this.Top = 0;       //设置窗体的Top属性值为零
                    this.Left = 0;      //设置窗体的Left属性值为零
                    int hg = SystemInformation.MaxWindowTrackSize.Height;   //获取窗口的默认最大高度
                    int wh = SystemInformation.MaxWindowTrackSize.Width;    //获取窗口的默认最大宽度
                    this.Height = hg;   //设置窗体的Height属性值
                    this.Width = wh;    //设置窗体的Width属性值
                    bol = true;         //设置窗体标记表示最大化
                    if (menItemSkin1.Checked)   //若选择“紫色小花”菜单项,设置最大化图片的Image属性
                        this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagespurplemax.png");
                    if (menItemSkin2.Checked)   //若选择“蓝色经典”菜单项,设置最大化图片的Image属性
                        this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagesluemax.png");
                    if (menItemSkin3.Checked)   //若选择“绿色菜园”菜单项,设置最大化图片的Image属性
                        this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagesgreenmax.png");
                }
                else                    //若窗体处于最大化状态
                {
                    this.Top = top;     //设置窗体的Top属性值
                    this.Left = left;   //设置窗体的Left属性值
                    this.Height = hei;  //设置窗体的Height属性值
                    this.Width = wid;   //设置窗体的Width属性值
                    bol = false;        //设置窗体标记表示普通状态
                    if (menItemSkin1.Checked)
                        this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagespurplemax_Normal.png");
                    if (menItemSkin2.Checked)
                        this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagesluemax_Normal.png");
                    if (menItemSkin3.Checked)
                        this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagesgreenmax_Normal.png");
                }
            }
            //最小化事件。
            private void picMinimize_Click(object sender, System.EventArgs e)
            {
                top = this.Top;
                left = this.Left;
                hei = this.Height;
                wid = this.Width;
                this.Height = 0;
                this.Width = 0;
                bo = true;
            }
            //窗口再次被激活时。
            private void Form_Activated(object sender, System.EventArgs e)
            {
                if (bo)
                {
                    this.Top = top;
                    this.Left = left;
                    this.Height = hei;
                    this.Width = wid;
                    bo = false;
                }
            }
            //选择关闭。
            private void mItemColse_Click(object sender, System.EventArgs e)
            {
                Application.Exit();
            }
            //上边框鼠标按下。
            private void panel_Top_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                x = e.X;
                y = e.Y;
                this.bolTop = true;
            }
            //上边框鼠标移动。 
            private void panel_Top_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (bolTop)
                {
                    this.Top += e.Y - y;
                    this.Left += e.X - x;
                }
            }
            //上边框鼠标释放。
            private void panel_Top_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                this.bolTop = false;
            }
            //左边框鼠标按下。
            private void panel_Left_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                x = e.X;
                this.bolLeft = true;
            }
            //左边框鼠标移动。
            private void panel_Left_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (bolLeft)
                {
                    this.Width += x - e.X;
                    this.Left += e.X - x;
                }
            }
            //左边框鼠标离开。
            private void panel_Left_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                this.bolLeft = false;
            }
            //右边框鼠标按下。
            private void panel_Right_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                x = e.X;
                this.bolRight = true;
            }
            //右边框鼠标移动。
            private void panel_Right_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
               if (bolRight)
                {
                    this.Width += e.X - x;
                }
            }
            //右边框鼠标离开。
            private void panel_Right_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                this.bolRight = false;
            }
            //下边框鼠标按下。
            private void panel_Bottom_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                y = e.Y;
                this.bolBottom = true;
            }
            //下边框鼠标移动。
            private void panel_Bottom_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (bolBottom)
                {
                    this.Height += e.Y - y;
                }
            }
            //下边框鼠标离开。
            private void panel_Bottom_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                this.bolBottom = false;
            }
            //左下角鼠标按下。
            private void panelLeftCornu_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                x = e.X;
                y = e.Y;
                this.bolLeftCornu = true;
            }
            //左下角鼠标移动。
            private void panelLeftCornu_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (bolLeftCornu)
                {
                    this.Width += x - e.X;
                    this.Left += e.X - x;
                    this.Height += e.Y - y;
                }
            }
            //左下角鼠标离开。
            private void panelLeftCornu_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                x = e.X;
                y = e.Y;
                this.bolLeftCornu = false;
            }
            //右下角鼠标按下。
            private void panelRightCornu_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                this.bolRightCornu = true;
            }
            //右下角鼠标移动。
            private void panelRightCornu_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (bolRightCornu)
                {
                    this.Width += e.X - x;
                    this.Height += e.Y - y;
                }
            }
            //右下角鼠标离开。
            private void panelRightCornu_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                this.bolRightCornu = false;
            }
    
            private void Form_Load(object sender, EventArgs e)
            {
                menItemSkin2_Click(sender, e);//默认蓝色经典
            }
    
            private void menItemSkin1_Click(object sender, EventArgs e)
            {
                //设置Panel控件的BackgroundImage属性
                this.panel_Top.BackgroundImage = Image.FromFile(strImagesPath + @"imagespurple	op.png");
                this.panel_Left.BackgroundImage = Image.FromFile(strImagesPath + @"imagespurpleleft.png");
                this.panel_Right.BackgroundImage = Image.FromFile(strImagesPath + @"imagespurple
    ight.png");
                this.panel_Bottom.BackgroundImage = Image.FromFile(strImagesPath + @"imagespurpleottom.png");
                //设置最小化图片控件的Image属性
                this.picMinimize.Image = Image.FromFile(strImagesPath + @"imagespurplemin.png");
                if (bol == true)    //若当前窗体处于最大化状态
                {
                    //设置最大化图片控件的Image属性
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagespurplemax.png");
                }
                else
                {
                    //设置最大化图片控件的Image属性
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagespurplemax_normal.png");
                }
                //设置关闭图片控件的Image属性
                this.picClose.Image = Image.FromFile(strImagesPath + @"imagespurpleclose.png");
                //设置菜单的选中标记
                this.menItemSkin1.Checked = true; 
                this.menItemSkin2.Checked = false;
                this.menItemSkin3.Checked = false;
                //设置窗体主菜单的背景图像属性
                this.menuStrip1.BackgroundImage = Image.FromFile(strImagesPath + @"imagespurplemenu.gif");
                //设置窗体的背景图像属性
                this.BackgroundImage = Image.FromFile(strImagesPath + @"imagespurpleackground.gif");
            }
    
            private void menItemSkin2_Click(object sender, EventArgs e)
            {
                this.panel_Top.BackgroundImage = Image.FromFile(strImagesPath + @"imageslue	op.png");
                this.panel_Left.BackgroundImage = Image.FromFile(strImagesPath + @"imageslueleft.png");
                this.panel_Right.BackgroundImage = Image.FromFile(strImagesPath + @"imageslue
    ight.png");
                this.panel_Bottom.BackgroundImage = Image.FromFile(strImagesPath + @"imageslueottom.png");
                this.picMinimize.Image = Image.FromFile(strImagesPath + @"imagesluemin.png");
                if (bol == true)
                {
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagesluemax.png");
                }
                else
                {
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagesluemax_normal.png");
                }
                this.picClose.Image = Image.FromFile(strImagesPath + @"imageslueclose.png");
                this.menItemSkin1.Checked = false;
                this.menItemSkin2.Checked = true;
                this.menItemSkin3.Checked = false;
                this.menuStrip1.BackgroundImage = Image.FromFile(strImagesPath + @"imagesluemenu.gif");
                this.BackgroundImage = Image.FromFile(strImagesPath + @"imageslueackground.gif");
            }
    
            private void menItemSkin3_Click(object sender, EventArgs e)
            {
                this.panel_Top.BackgroundImage = Image.FromFile(strImagesPath + @"imagesgreen	op.png");
                this.panel_Left.BackgroundImage = Image.FromFile(strImagesPath + @"imagesgreenleft.png");
                this.panel_Right.BackgroundImage = Image.FromFile(strImagesPath + @"imagesgreen
    ight.png");
                this.panel_Bottom.BackgroundImage = Image.FromFile(strImagesPath + @"imagesgreenottom.png");
                this.picMinimize.Image = Image.FromFile(strImagesPath + @"imagesgreenmin.png");
                if (bol == true)
                {
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagesgreenmax.png");
                }
                else
                {
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"imagesgreenmax_normal.png");
                }
                this.picClose.Image = Image.FromFile(strImagesPath + @"imagesgreenclose.png");
                this.menItemSkin1.Checked = false;
                this.menItemSkin2.Checked = false;
                this.menItemSkin3.Checked = true;
                this.menuStrip1.BackgroundImage = Image.FromFile(strImagesPath + @"imagesgreenmenu.gif");
                this.BackgroundImage = Image.FromFile(strImagesPath + @"imagesgreenackground.gif");
            }
    
            private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void panel_Top_DoubleClick(object sender, EventArgs e)
            {
                picMaximize_Click(sender, e);
            }
        }
    }
  • 相关阅读:
    sql 2008 r2
    js压缩反压缩
    .NET调用Java写的WebServices(可能会碰到的问题)
    ecshop
    Chrome插件
    Unity3d ShaderLab之WorldNormalVector
    Unity 3d中Shader是什么,可以吃吗?
    java,类的继承
    java,关于POJO,查阅资料
    java,编写一个汽车类
  • 原文地址:https://www.cnblogs.com/iwanc/p/3138926.html
Copyright © 2011-2022 走看看