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

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

    • 1. 效果
    • 2. 实现代码

1. 效果

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);
        }

        /// 
        /// 设置按钮事件
        /// 
        /// 
        /// 
        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();
        /// 
        /// 鼠标出入按钮变换图像
        /// 
        /// 
        /// 
        /// 
        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#,and,Halcon,c#)