码迷,mamicode.com
首页 > Windows程序 > 详细

[C#自学视频]认识windows窗体

时间:2016-07-13 19:56:34      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:

7.4.1  根据桌面大小调节窗体大小

namespace SetFormSizeByDeskSize
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            int DeskWidth = Screen.PrimaryScreen.WorkingArea.Width;//获取桌面宽度
            int DeskHeight = Screen.PrimaryScreen.WorkingArea.Height;//获取桌面高度
            this.Width = Convert.ToInt32(DeskWidth * 0.8);//设置窗体宽度
            this.Height = Convert.ToInt32(DeskHeight * 0.8);//设置窗体高度
        }
    }
}

7.4.2 自定义最大化、最小化和关闭按钮

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

namespace ControlFormStatus
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            this.Width = Properties.Resources.登录界面标题.Width;//设置体的宽度
            this.Height = Properties.Resources.登录界面标题.Height + Properties.Resources.登录界面下面.Height;//设置窗体的高度
            panel_Title.BackgroundImage = Properties.Resources.登录界面标题;//显示窗体标题栏的图片
            panel_ALL.BackgroundImage = Properties.Resources.登录界面下面;//显示窗体标题栏下同的图片
            pictureBox_Min.Image = null;//清空PictuteBox控件
            pictureBox_Min.Image = Properties.Resources.最小化按钮;//显示最小化按钮的图片
            pictureBox_Max.Image = null; //清空PictuteBox控件
            pictureBox_Max.Image = Properties.Resources.最大化按钮;//显示最大化按钮的图片
            pictureBox_Close.Image = null;//清空PictuteBox控件
            pictureBox_Close.Image = Properties.Resources.关闭按钮;//显示关闭按钮的图片
        }

        #region  设置窗体的最大化、最小化和关闭按钮的单击事件
        /// <summary>
        /// 设置窗体的最大化、最小化和关闭按钮的单击事件
        /// </summary>
        /// <param Frm_Tem="Form">窗体</param>
        /// <param n="int">标识</param>
        public void FrmClickMeans(Form Frm_Tem, int n)
        {
            switch (n)//窗体的操作样式
            {
                case 0://窗体最小化
                    Frm_Tem.WindowState = FormWindowState.Minimized;//窗体最小化
                    break;
                case 1://窗体最大化和还原的切换
                    {
                        if (Frm_Tem.WindowState == FormWindowState.Maximized)//如果窗体当前是最大化
                            Frm_Tem.WindowState = FormWindowState.Normal;//还原窗体大小
                        else
                            Frm_Tem.WindowState = FormWindowState.Maximized;//窗体最大化
                        break;
                    }
                case 2:    //关闭窗体
                    Frm_Tem.Close();
                    break;
            }
        }
        #endregion

        #region  控制图片的切换状态
        /// <summary>
        /// 控制图片的切换状态
        /// </summary>
        /// <param sender =" object ">要改变图片的对象</param>
        /// <param n="int">标识</param>
        /// <param ns="int">移出移入标识</param>
        public static PictureBox Tem_PictB = new PictureBox();//实例化PictureBox控件
        public void ImageSwitch(object sender, int n, int ns)
        {
            Tem_PictB = (PictureBox)sender;
            switch (n)//获取标识
            {
                case 0://当前为最小化按钮
                    {
                        Tem_PictB.Image = null;//清空图片
                        if (ns == 0)//鼠标移入
                            Tem_PictB.Image = Properties.Resources.最小化变色;
                        if (ns == 1)//鼠标移出
                            Tem_PictB.Image = Properties.Resources.最小化按钮;
                        break;
                    }
                case 1://最大化按钮
                    {
                        Tem_PictB.Image = null;
                        if (ns == 0)
                            Tem_PictB.Image = Properties.Resources.最大化变色;
                        if (ns == 1)
                            Tem_PictB.Image = Properties.Resources.最大化按钮;
                        break;
                    }
                case 2://关闭按钮
                    {
                        Tem_PictB.Image = null;
                        if (ns == 0)
                            Tem_PictB.Image = Properties.Resources.关闭变色;
                        if (ns == 1)
                            Tem_PictB.Image = Properties.Resources.关闭按钮;
                        break;
                    }
            }
        }
        #endregion

        private void pictureBox_Close_Click(object sender, EventArgs e)//单击事件
        {
            FrmClickMeans(this, Convert.ToInt16(((PictureBox)sender).Tag.ToString()));//设置鼠标单击时按钮的图片
        }
        private void pictureBox_Close_MouseEnter(object sender, EventArgs e)//鼠标移入事件
        {
            ImageSwitch(sender, Convert.ToInt16(((PictureBox)sender).Tag.ToString()), 0);//设置鼠标移入后按钮的图片
        }
        private void pictureBox_Close_MouseLeave(object sender, EventArgs e)//鼠标移出事件
        {
            ImageSwitch(sender, Convert.ToInt16(((PictureBox)sender).Tag.ToString()), 1);//设置鼠标移出后按钮的图片
        }
    }
}

7.4.3  窗体换肤程序

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

namespace WinCusSkin
{
    public partial class Frm_Main  : 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 Frm_Main()
        {
            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;
                left = this.Left;
                hei = this.Height;
                wid = this.Width;
                this.Top = 0;
                this.Left = 0;
                int hg = SystemInformation.MaxWindowTrackSize.Height;
                int wh = SystemInformation.MaxWindowTrackSize.Width;
                this.Height = hg;
                this.Width = wh;
                bol = true;
                if (menItemSkin1.Checked)
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\purple\max.png");
                if (menItemSkin2.Checked)
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\blue\max.png");
                if (menItemSkin3.Checked)
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\green\max.png");
            }
            else
            {
                this.Top = top;
                this.Left = left;
                this.Height = hei;
                this.Width = wid;
                bol = false;
                if (menItemSkin1.Checked)
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\purple\max_Normal.png");
                if (menItemSkin2.Checked)
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\blue\max_Normal.png");
                if (menItemSkin3.Checked)
                    this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\green\max_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 FormCusSkin_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 FormCusSkin_Load(object sender, EventArgs e)
        {
            menItemSkin2_Click(sender, e);//默认蓝色经典
        }

        private void menItemSkin1_Click(object sender, EventArgs e)
        {
            this.panel_Top.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\top.png");
            this.panel_Left.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\left.png");
            this.panel_Right.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\right.png");
            this.panel_Bottom.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\bottom.png");
            this.picMinimize.Image = Image.FromFile(strImagesPath + @"\images\purple\min.png");
            if (bol == true)
            {
                this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\purple\max.png");
            }
            else
            {
                this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\purple\max_normal.png");
            }
            this.picClose.Image = Image.FromFile(strImagesPath + @"\images\purple\close.png");
            this.menItemSkin1.Checked = true;
            this.menItemSkin2.Checked = false;
            this.menItemSkin3.Checked = false;
            this.menuStrip1.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\menu.gif");
            this.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\background.gif");
        }

        private void menItemSkin2_Click(object sender, EventArgs e)
        {
            this.panel_Top.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\top.png");
            this.panel_Left.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\left.png");
            this.panel_Right.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\right.png");
            this.panel_Bottom.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\bottom.png");
            this.picMinimize.Image = Image.FromFile(strImagesPath + @"\images\blue\min.png");
            if (bol == true)
            {
                this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\blue\max.png");
            }
            else
            {
                this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\blue\max_normal.png");
            }
            this.picClose.Image = Image.FromFile(strImagesPath + @"\images\blue\close.png");
            this.menItemSkin1.Checked = false;
            this.menItemSkin2.Checked = true;
            this.menItemSkin3.Checked = false;
            this.menuStrip1.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\menu.gif");
            this.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\background.gif");
        }

        private void menItemSkin3_Click(object sender, EventArgs e)
        {
            this.panel_Top.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\top.png");
            this.panel_Left.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\left.png");
            this.panel_Right.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\right.png");
            this.panel_Bottom.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\bottom.png");
            this.picMinimize.Image = Image.FromFile(strImagesPath + @"\images\green\min.png");
            if (bol == true)
            {
                this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\green\max.png");
            }
            else
            {
                this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\green\max_normal.png");
            }
            this.picClose.Image = Image.FromFile(strImagesPath + @"\images\green\close.png");
            this.menItemSkin1.Checked = false;
            this.menItemSkin2.Checked = false;
            this.menItemSkin3.Checked = true;
            this.menuStrip1.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\menu.gif");
            this.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\background.gif");
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void panel_Top_DoubleClick(object sender, EventArgs e)
        {
            picMaximize_Click(sender, e);
        }
    }
}

 

[C#自学视频]认识windows窗体

标签:

原文地址:http://www.cnblogs.com/kingboys/p/5667161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!