C#精彩编程200例-第3例-最大化、最小化、关闭、移动窗口

C#精彩编程200例-第3例-最大化、最小化、关闭、移动窗口

1. 效果

在这里插入图片描述

2. 实现代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 自定义最大化_最小化_关闭按钮
{
    public partial class Form1 : Form
    {
        #region 使窗体可以移动的代码
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            //拖动窗体
            this.Cursor = System.Windows.Forms.Cursors.Hand;//改变鼠标样式
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            this.Cursor = System.Windows.Forms.Cursors.Default;
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //拖动窗体
            this.Cursor = System.Windows.Forms.Cursors.Hand;//改变鼠标样式
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            this.Cursor = System.Windows.Forms.Cursors.Default;
        }

        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            //拖动窗体
            this.Cursor = System.Windows.Forms.Cursors.Hand;//改变鼠标样式
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            this.Cursor = System.Windows.Forms.Cursors.Default;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //初始化窗体尺寸
            this.Width = Properties.Resources.登录界面标题.Width;
            this.Height = Properties.Resources.登录界面标题.Height + Properties.Resources.登录界面下面.Height;

            //初始化各模块背景图像
            this.panelTitle.BackgroundImage = Properties.Resources.登录界面标题;
            this.panelAll.BackgroundImage = Properties.Resources.登录界面下面;
            this.pbxMinimum.Image = null;
            this.pbxMinimum.Image = Properties.Resources.最小化按钮;
            this.pbxMaximum.Image = null;
            this.pbxMaximum.Image = Properties.Resources.最大化按钮;
            this.pbxClose.Image = null;
            this.pbxClose.Image = Properties.Resources.关闭按钮;
        }

        private void pbxClose_Click(object sender, EventArgs e)
        {
            this.MinMaxCloseForm(this, Convert.ToInt32(((PictureBox)sender).Tag.ToString()));
        }

        private void pbxClose_MouseEnter(object sender, EventArgs e)
        {
            this.SwitchImage(sender, Convert.ToInt32(((PictureBox)sender).Tag.ToString()), 0);
        }

        private void pbxClose_MouseLeave(object sender, EventArgs e)
        {
            this.SwitchImage(sender, Convert.ToInt32(((PictureBox)sender).Tag.ToString()), 1);
        }

        /// <summary>
        /// 设置按钮事件
        /// </summary>
        /// <param name="form"></param>
        /// <param name="n"></param>
        public void MinMaxCloseForm(Form form, int n)
        {
            switch (n)
            {
                case 0:
                    form.WindowState = FormWindowState.Minimized;
                    break;
                case 1:
                    if (form.WindowState == FormWindowState.Maximized)
                    {
                        form.WindowState = FormWindowState.Normal;
                    }
                    else
                    {
                        form.WindowState = FormWindowState.Maximized;
                    }
                    break;
                case 2:
                    form.Close();
                    form = null;
                    break;
            }
        }

        public static PictureBox temp_picture_box = new PictureBox();
        /// <summary>
        /// 鼠标出入按钮变换图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="form_index"></param>
        /// <param name="enter_leave_flag"></param>
        public void SwitchImage(object sender, int form_index, int enter_leave_flag)
        {
            temp_picture_box = (PictureBox)sender;
            switch (form_index)
            {
                case 0:
                    temp_picture_box.Image = null;
                    if (enter_leave_flag == 0)
                    {
                        temp_picture_box.Image = Properties.Resources.最小化变色;
                    }
                    if (enter_leave_flag == 1)
                    {
                        temp_picture_box.Image = Properties.Resources.最小化按钮;
                    }
                    break;
                case 1:
                    temp_picture_box.Image = null;
                    if (enter_leave_flag == 0)
                    {
                        temp_picture_box.Image = Properties.Resources.最大化变色;
                    }
                    if (enter_leave_flag == 1)
                    {
                        temp_picture_box.Image = Properties.Resources.最大化按钮;
                    }
                    break;
                case 2:
                    temp_picture_box.Image = null;
                    if (enter_leave_flag == 0)
                    {
                        temp_picture_box.Image = Properties.Resources.关闭变色;
                    }
                    if (enter_leave_flag == 1)
                    {
                        temp_picture_box.Image = Properties.Resources.关闭按钮;
                    }
                    break;
            }
        }
    }
}

c#精彩编程》配套源码 附录:本书配套光盘说明 本光盘包括本书所有源代码及其可执行文件、配套的C#编辑器(包括编辑器的C#源代码)及一些参考资料。 一、安装程序 将此光盘放到CD-ROM驱动器,运行Setup.htm文件,然后选择相应的选项。 二、光盘内容 1.CSharpSource文件夹 此文件夹包括了本书所有的源代码及其可执行文件。 *.CS:C#源代码。 *.EXE:对应的可执行文件,有些需要在MS DOS方式下运行,详细内容参考书中说明。 Context子文件夹:Context属性程序的源代码及其可执行文件。 Dtime子文件夹:Dtime程序(设置系统时间)的源代码及其可执行文件。 Fileupload子文件夹:File Uploader程序的所有源代码及其可执行文件。 Meal子文件夹:Meal程序的所有源代码及其可执行文件。 SharpDevelop子文件夹:SharpDevelop编辑器源代码及可执行文件。 srvDownload子文件夹:srvDownload程序的源代码及可执行文件。 XmlHelper子文件夹:XML Helper类库及测试程序的所有源代码及可执行文件。 Visual Studio Project子文件夹:书中所有Visual Studio.NET的工程文件、源代码、类库及可执行文件。 另外,某些配套的文件也在此文件夹中。 2.Software文件夹 此文件夹包括一些C#编辑器及相关软件。 Visual SlickEditor 6.0:这是一个功能强大的C#编辑器(也可以编辑HTML、ASP.NET等),安装时需要到http://www.slickedit.com上获得安装序列号(30天试用安装序列号为3778163584-0600-WB0000-PKGA )。 CSharpDevelop:这是一个包含源代码的C#、VB.NET的编辑器。 SitePad Pro 4.1:这是一个功能强大的C#编辑器。 CSharpEditor 3.1:这是著名Antechinus C#编辑器,其更新版本可到http://www.c-point.com上下载,可到清华大学教育网(ftp://166.111.168.6 )中获得注册码及相关解密文件。 CToCSharp:这是一个C到C#转换程序,包括源代码。 3.Reference文件夹 此文件夹包括如下内容: CSharp语言参考。 Visual Studio.NET焦点问答28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值