C# WinForm窗体最大化

效果图:

核心代码:
1. 控制类
ContractedBlock.gif ExpandedBlockStart.gif 窗体最大化控制类
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
    
public class Pb_C_MaxFrm
    {
        Form _MaxFrom;
        ESCMsgFrm _ESCFrm;

        FormBorderStyle _oriFrmBorderStyle;
        FormWindowState _oriFrmWindowState;

        
bool _bIsMax = false;
        
public Pb_C_MaxFrm()
        {

        }
        
public Pb_C_MaxFrm(Form maxForm)
        {
            setMaxForm(maxForm);
        }
        
public bool IsMax
        {
            
get { return _bIsMax; }
        }
        
public void setMaxForm(Form maxForm)
        {
            _MaxFrom 
= maxForm;
            
//记录窗体的原始状态
            _oriFrmBorderStyle = maxForm.FormBorderStyle;
            _oriFrmWindowState 
= maxForm.WindowState;
            _MaxFrom.KeyDown
+=new KeyEventHandler(_MaxFrom_KeyDown);
 
        }
        
bool CheckData()
        {
            
return _MaxFrom == null ? false : true;
        }
        
void  _MaxFrom_KeyDown(object sender, KeyEventArgs e)
        {
            
//按下[F11]键执行全屏化
            if (e.KeyCode == Keys.F11)
                Show();
            
else if (e.KeyCode == Keys.Escape)//按下[Esc]键返回原始窗体
                unShow();
        }

        
public void Show()
        {
            
if (!CheckData())
                
return;

            
#region 窗体最大化
            
if (_MaxFrom.WindowState != FormWindowState.Maximized || _MaxFrom.FormBorderStyle!= FormBorderStyle.None)
            {
                _MaxFrom.FormBorderStyle 
= FormBorderStyle.None;
                _MaxFrom.WindowState 
= FormWindowState.Maximized;

                
#region 启动闪屏
                
if (_ESCFrm == null)
                {
                    _ESCFrm 
= new ESCMsgFrm();
                    _ESCFrm.FormClosed 
+= new FormClosedEventHandler(_ESCFrm_FormClosed);
                    _ESCFrm.Owner 
= _MaxFrom;
                }
                _ESCFrm.Show();
                
#endregion
            }
            
#endregion  

            _bIsMax 
= true;
        }

        
public void unShow()
        {
            
if (!CheckData())
                
return;

            
if (_ESCFrm != null)
                _ESCFrm.Close();
            
//返回到初始状态
            _MaxFrom.FormBorderStyle = _oriFrmBorderStyle;
            _MaxFrom.WindowState 
= _oriFrmWindowState;

            _bIsMax 
= false;
        }

        
void _ESCFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _ESCFrm 
= null;
        }
    }
}

2.[按ESC返回]信息窗口类
ContractedBlock.gif ExpandedBlockStart.gif 按ESC返回
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
    
public partial class ESCMsgFrm : Form
    {
        
public ESCMsgFrm()
        {
            InitializeComponent();
        }

        
#region 窗体效果(暂时未用)
        
/// <summary>
        
/// 设置窗体的四角为圆角
        
/// </summary>
        void setCircleCorner()
        {
            
#region 圆角蒙板

            System.Drawing.Drawing2D.GraphicsPath GraphicsPath 
= new System.Drawing.Drawing2D.GraphicsPath();

            
#region 左上
            GraphicsPath.AddRectangle(
new Rectangle(0055));
            GraphicsPath.AddRectangle(
new Rectangle(10021));
            GraphicsPath.AddRectangle(
new Rectangle(9012));
            GraphicsPath.AddRectangle(
new Rectangle(8012));
            GraphicsPath.AddRectangle(
new Rectangle(7013));
            GraphicsPath.AddRectangle(
new Rectangle(6014));
            GraphicsPath.AddRectangle(
new Rectangle(5015));
            GraphicsPath.AddRectangle(
new Rectangle(0551));
            GraphicsPath.AddRectangle(
new Rectangle(0641));
            GraphicsPath.AddRectangle(
new Rectangle(0731));
            GraphicsPath.AddRectangle(
new Rectangle(0821));
            GraphicsPath.AddRectangle(
new Rectangle(0921));
            GraphicsPath.AddRectangle(
new Rectangle(01011));
            GraphicsPath.AddRectangle(
new Rectangle(01111));
            
#endregion

            
#region 右上
            GraphicsPath.AddRectangle(
new Rectangle(Width - 11021));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 9022));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 7013));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 6014));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 5055));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 4541));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 3651));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 2722));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 1912));
            
#endregion

            
#region 左下
            GraphicsPath.AddRectangle(
new Rectangle(0, Height - 551));
            GraphicsPath.AddRectangle(
new Rectangle(0, Height - 641));
            GraphicsPath.AddRectangle(
new Rectangle(0, Height - 731));
            GraphicsPath.AddRectangle(
new Rectangle(0, Height - 922));
            GraphicsPath.AddRectangle(
new Rectangle(0, Height - 1213));
            GraphicsPath.AddRectangle(
new Rectangle(0, Height - 454));
            GraphicsPath.AddRectangle(
new Rectangle(5, Height - 414));
            GraphicsPath.AddRectangle(
new Rectangle(6, Height - 313));
            GraphicsPath.AddRectangle(
new Rectangle(7, Height - 222));
            GraphicsPath.AddRectangle(
new Rectangle(9, Height - 131));
            
#endregion

            
#region 右下
            GraphicsPath.AddRectangle(
new Rectangle(Width - 9, Height - 121));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 7, Height - 212));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 6, Height - 313));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 5, Height - 414));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 4, Height - 444));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 4, Height - 551));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 3, Height - 631));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 2, Height - 822));
            GraphicsPath.AddRectangle(
new Rectangle(Width - 1, Height - 1113));
            
#endregion

            System.Drawing.Region RegionX 
= new Region();
            RegionX.Xor(GraphicsPath);
            
this.Region = RegionX;

            
#endregion
        }
        
/// <summary>
        
/// 圆角透明
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="p_1"></param>
        
/// <param name="p_2"></param>
        private void Type(Control sender, int p_1, double p_2)
        {
            GraphicsPath oPath 
= new GraphicsPath();
            oPath.AddClosedCurve(
new Point[] { new Point(0, sender.Height / p_1),
                
new Point(sender.Width / p_1, 0), 
                
new Point(sender.Width - sender.Width / p_1, 0), 
                
new Point(sender.Width, sender.Height / p_1), 
                
new Point(sender.Width, sender.Height - sender.Height / p_1), 
                
new Point(sender.Width - sender.Width / p_1, sender.Height), 
                
new Point(sender.Width / p_1, sender.Height), 
                
new Point(0, sender.Height - sender.Height / p_1) }, 
                (
float)p_2);
            sender.Region 
= new Region(oPath);
        }

        
#endregion

        
private void ESCMsgFrm_Load(object sender, EventArgs e)
        {
            
this.ShowInTaskbar = false;//不在任务栏里显示
            this.StartPosition = FormStartPosition.CenterScreen;
            
this.Opacity = 1;

            
//以下两句话可以保证窗体显示时只显示字体,因为背景色已被设置为透明
            
//设置窗体背景色为银色
            this.BackColor = Color.Silver;
            
this.TransparencyKey = Color.Silver;//将窗体中银色部分设置为透明

            timer1.Interval 
= 300;
            timer1.Start();
        }

        
private void timer1_Tick(object sender, EventArgs e)
        {
            
if (this.Opacity > 0)
            {
                
this.Opacity -= 0.17;
            }
            
else
            {
                
this.Opacity = 0;
                
this.Close();
            }
        }

        
private void ESCMsgFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
        }

    }
}

3.如何调用
ContractedBlock.gif ExpandedBlockStart.gif 调用类
public partial class Form2 : Form
    {
        Pb_C_MaxFrm m;
        
public Form2()
        {
            InitializeComponent();
            m 
= new Pb_C_MaxFrm();
            m.setMaxForm(
this);
        }

        
private void Form2_Load(object sender, EventArgs e)
        {
            
        }
    }


转载于:https://www.cnblogs.com/JinDin/archive/2009/10/09/1579488.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值