公元 2025 年 11 月 10 日

仿QQ窗体自动收缩

2023/5/31 1:05:21

模仿QQ主窗体自动收缩的功能,展示部分功能。

        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (Top < 3 && Tem_place == 0)//如果窗体被移到屏幕的顶部
            {
                if (Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上
                {
                    panel_Title.Tag = 1;//设置标识,用于判断窗体在屏幕顶部
                    timer2.Enabled = false;//不对窗体进行拉伸操作
                    Top = 0;//使窗体致顶
                }
                else
                {
                    panel_Title.Tag = 1;//设置标识,用于判断窗体在屏幕顶部
                    timer2.Enabled = true;//将窗体在顶部进行隐藏
                }
            }
            else
            {
                if (Left < 3 || Right > GetSystemMetrics(0) - 3)//如果窗体被移到屏幕的左端或右端
                {
                    if (Left < 3)//如果窗体被移到屏幕的左端
                    {
                        if (Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上
                        {
                            panel_Title.Tag = 2;//设置标识,用于判断窗体在屏幕左端
                            timer2.Enabled = false;
                            Frm_Height = Height;
                            Left = 0;//使窗体致左
                            Top = 0;
                            Height = Screen.AllScreens[0].Bounds.Height;
                            Tem_place = 1;
                        }
                        else
                        {
                            panel_Title.Tag = 2;
                            timer2.Enabled = true;//将窗体在左端进行隐藏
                        }
                    }
                    if (Right > GetSystemMetrics(0) - 3)//如果窗体被移到屏幕的右端
                    {
                        if (Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上
                        {
                            panel_Title.Tag = 3;//设置标识,用于判断窗体在屏幕右端
                            timer2.Enabled = false;
                            Frm_Height = Height;
                            Left = GetSystemMetrics(0) - Width;//使窗体致右
                            Top = 0;
                            Height = Screen.AllScreens[0].Bounds.Height;
                            Tem_place = 1;
                        }
                        else
                        {
                            panel_Title.Tag = 3;
                            timer2.Enabled = true;//将窗体在右端进行隐藏
                        }
                    }
                }
            }
        }

 

经常用到的东西,记录下来,然后用的时候想不起来。

1.遍历整个 dataGridView1 控件

for (int i = 0; i < dataGridView1.RowCount; i++)
{
    for (int j = 0; j < dataGridView1.ColumnCount; j++)
    {
        //打印第i行第j列数据
        Console.WriteLine(Convert.ToString(dataGridView1[j, i].Value));
        // 注意dataGridView1[j,i]代表的是第i行第j列
    }
}

2.模糊查找数据并定位并加入是否还要继续查找

int row = dataGridView1.Rows.Count;//得到总行数
int cell = dataGridView1.Rows[1].Cells.Count;//得到总列数
string strTxt = txtFind.Text;//得到输入的字符串,并付值给变量
Regex r = new Regex(strTxt); // 定义一个Regex对象实例 
for (int i = 0; i < row; i++)//得到总行数并在之内循环
{
    for (int j = 0; j < cell; j++)//得到总列数并在之内循环
    {
        Match m = r.Match(dataGridView1.Rows[i].Cells[j].Value.ToString()); // 在字符串中模糊匹配 
        if (m.Success)
        {   //对比TexBox中的值是否与dataGridView中的值相同(上面这句)
            dataGridView1.CurrentCell = dataGridView1[j, i];//定位到相同的单元格
            if (MessageBox.Show("是否需要继续查找?", "", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                //如果选择了取消就会返回,如果选择了确定,就会继续查找匹配的.
                return;//返回
            }
        }
    }
}

 

自己网上Ctrl + C 、Ctrl + V 收集,并自己开发的,自用 简单窗体控件库,如有喜欢的朋友可以下载去探索下。

目前有 10个重写控件,分别是:Button、CheckBox、Form、GlassButton、ImageButton、ListView、MessageBox、RadioButtom

SplashScreen、TextBox。不会写的很复杂,适合新手,一看就明白。源码注释明确。

控件演示窗口:

EM.Controls

 消息窗口展示:

EM.Controls

文件目录展示:

EM.Controls

 展示部分窗体代码:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace EM.Controls
{
    /// <summary>
    /// 自定义扩展窗体Form
    /// </summary>
    public class Form : System.Windows.Forms.Form
    {
        #region 私有变量
        //系统按钮管理器
        private SystemButtonManager _systemButtonManager;
        #endregion

        #region 构造函数

        public Form()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            _systemButtonManager = new SystemButtonManager(this);
            FormIni();
        }
        #endregion

        #region 属性
        [Description("确定窗体标题栏的右上角是否有关闭框。")]
        public bool CloseBox { get; set; } = true;

        [Description("窗体圆角的半径。")]
        public int Radius { get; set; } = 5;

        [Description("是否允许窗体改变大小。")]
        public bool CanResize { get; set; } = true;

        [Description("窗体背景图片。")]
        public override Image BackgroundImage { get; set; }

        [Description("绘制窗体标题的字体。")]
        public Font TextFont { get; set; } = new Font("宋体", 9.0f, FontStyle.Bold);

        [Description("绘制窗体标题的颜色。")]
        public Color TextForeColor { get; set; } = Color.FromArgb(250, Color.White);

        [Description("绘制带有阴影的窗体标题。")]
        public bool TextWithShadow { get; set; } = true;

        [Description("使窗体绘制阴影。")]
        public Color TextShadowColor { get; set; } = Color.FromArgb(2, Color.Black);

        [Description("窗体阴影的宽度。默认值:3")]
        public int TextShadowWidth { get; set; } = 3;

        [Description("最小窗体宽大小。")]
        public int MinFormSizeWidth { get; set; } = 50;

        [Description("最小窗体高大小。")]
        public int MinFormSizeHeight { get; set; } = 30;

        [Browsable(false)]
        [Description("返回窗体关闭系统按钮所在的坐标矩形")]
        public Rectangle CloseBoxRect
        {
            get { return SystemButtonManager.SystemButtonArray[0].LocationRect; }
        } 
        [Browsable(false)]
        [Description("返回窗体最大化或者还原系统按钮所在的坐标矩形")]
        public Rectangle MaximiziBoxRect
        {
            get { return SystemButtonManager.SystemButtonArray[1].LocationRect; }
        }

        [Browsable(false)]
        [Description("返回窗体最小化系统按钮所在的坐标矩形")]
        public Rectangle MinimiziBoxRect
        {
            get { return SystemButtonManager.SystemButtonArray[2].LocationRect; }
        }
        /// <summary>
        /// 窗体 Icon 矩形
        /// </summary>
        internal Rectangle IconRect
        {
            get
            {
                if (base.ShowIcon && base.Icon != null)
                {
                    return new Rectangle(8, 8, SystemInformation.SmallIconSize.Width, SystemInformation.SmallIconSize.Width);
                }
                return Rectangle.Empty;
            }
        }
        /// <summary>
        /// 窗体标题矩形
        /// </summary>
        internal Rectangle TextRect
        {
            get
            {
                if (base.Text.Length != 0)
                {
                    return new Rectangle(IconRect.Right + 5, 8, Width - (8 + IconRect.Width + 2), TextFont.Height);
                }
                return Rectangle.Empty;
            }
        } 
        internal SystemButtonManager SystemButtonManager
        {
            get
            {
                if (_systemButtonManager == null)
                {
                    _systemButtonManager = new SystemButtonManager(this);
                }
                return _systemButtonManager;
            }
        } 
        #endregion 
        #region 继承

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                if (!DesignMode)
                {
                    if (MaximizeBox) { cp.Style |= (int)WindowStyle.WS_MAXIMIZEBOX; }
                    if (MinimizeBox) { cp.Style |= (int)WindowStyle.WS_MINIMIZEBOX; }
                    cp.ExStyle |= (int)WindowStyle.WS_CLIPCHILDREN;  //防止因窗体控件太多出现闪烁
                    cp.ClassStyle |= (int)ClassStyle.CS_DropSHADOW;  //实现窗体边框阴影效果
                }
                return cp;
            }
        } 
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            RenderHelper.SetFormRoundRectRgn(this, Radius);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            UpdateSystemButtonRect();
        } 
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (this.Width <= MinFormSizeWidth) { this.Width = MinFormSizeWidth; }
            if (this.Height <= MinFormSizeHeight) { this.Height = MinFormSizeHeight; }

            RenderHelper.SetFormRoundRectRgn(this, Radius);

            //更新 最大化 按钮 图标
            UpdateMaxButton();
            // 更新  按钮的位置
            UpdateSystemButtonRect();
        }
        /// <summary>
        /// 处理Win 系统消息
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case Win32.WM_ERASEBKGND:
                    m.Result = IntPtr.Zero;
                    break;
                case Win32.WM_NCHITTEST://这个消息是当鼠标移动或者有鼠标键按下时候发出的
                    WmNcHitTest(ref m);
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        } 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Move);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Down);
            }
        } 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.Button == MouseButtons.Left)
            {
                SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Up);
            }
        } 
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            SystemButtonManager.ProcessMouseOperate(Point.Empty, MouseOperate.Leave);
        }
        /// <summary>
        /// 重写控件的 OnPaint 事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.CompositingQuality = CompositingQuality.Default;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

            //绘制 背景图片
            if (BackgroundImage != null)
            {
                switch (BackgroundImageLayout)
                {
                    case ImageLayout.Stretch:
                    case ImageLayout.Zoom:
                        e.Graphics.DrawImage(BackgroundImage, ClientRectangle, new Rectangle(0, 0, BackgroundImage.Width, BackgroundImage.Height), GraphicsUnit.Pixel);
                        break;
                    case ImageLayout.Center:
                    case ImageLayout.None:
                    case ImageLayout.Tile:
                        e.Graphics.DrawImage(BackgroundImage, ClientRectangle, ClientRectangle, GraphicsUnit.Pixel);
                        break;
                }
            }

            //绘制窗体主体部分白色透明层
            RenderHelper.DrawFromAlphaMainPart(this, e.Graphics);

            //绘制 边缘
            RenderHelper.DrawFormFringe(this, e.Graphics, RenderHelper.GetImageFormResourceStream("Control.Form.Res.fringe_bkg.png"), Radius);

            //绘制 系统按钮
            SystemButtonManager.DrawSystemButtons(e.Graphics);

            //绘制 icon
            if (Icon != null && ShowIcon) { e.Graphics.DrawIcon(Icon, IconRect); }

            //绘制文本
            if (Text.Length != 0)
            {
                if (TextWithShadow)
                {
                    using (Image textImg = RenderHelper.GetStringImgWithShadowEffect(Text, TextFont, TextForeColor, TextShadowColor, TextShadowWidth))
                    {
                        e.Graphics.DrawImage(textImg, TextRect.Location);
                    }
                }
                else
                {
                    TextRenderer.DrawText(e.Graphics, Text, TextFont, TextRect, TextForeColor, TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis);
                }
            }
        } 
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                _systemButtonManager?.Dispose();
                TextFont?.Dispose();
                BackgroundImage?.Dispose();
            }
        } 
        #endregion 
        #region 私有方法

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form
            // 
            this.ClientSize = new System.Drawing.Size(528, 385);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "Form";
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.ResumeLayout(false); 
        }

        private void FormIni()
        {
            this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;
            this.DoubleBuffered = true;  //设置本窗体双缓冲
            SetStyles();
        }  
        private void SetStyles()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            UpdateStyles();
        }
        /// <summary>
        /// 通过获取系统消息,调整窗体大小。
        /// </summary>
        /// <param name="m"></param>
        private void WmNcHitTest(ref Message m)
        {
            if (CanResize == true && WindowState != FormWindowState.Maximized)
            {
                Point mouseLocation = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)); //获取鼠标在屏幕的坐标点
                                                                                                      //左上斜角
                if (mouseLocation.X < 5 && mouseLocation.Y < 5)
                {
                    m.Result = new IntPtr(Win32.HTTOPLEFT);
                    return;
                }
                //右上斜角
                if (mouseLocation.X > Width - 5 && mouseLocation.Y < 5)
                {
                    m.Result = new IntPtr(Win32.HTTOPRIGHT);
                    return;
                }
                //左下斜角
                if (mouseLocation.X < 5 && mouseLocation.Y > Height - 5)
                {
                    m.Result = new IntPtr(Win32.HTBOTTOMLEFT);
                    return;
                }
                //右下斜角
                if (mouseLocation.X > Width - 5 && mouseLocation.Y > Height - 5)
                {
                    m.Result = new IntPtr(Win32.HTBOTTOMRIGHT);
                    return;
                }
                //上
                if (mouseLocation.Y < 5)
                {
                    m.Result = new IntPtr(Win32.HTTOP);
                    return;
                }
                //下
                if (mouseLocation.Y > Height - 5)
                {
                    m.Result = new IntPtr(Win32.HTBOTTOM);
                    return;
                }
                //左
                if (mouseLocation.X < 5)
                {
                    m.Result = new IntPtr(Win32.HTLEFT);
                    return;
                }
                //右
                if (mouseLocation.X > Width - 5)
                {
                    m.Result = new IntPtr(Win32.HTRIGHT);
                    return;
                }
            } 
            m.Result = new IntPtr(Win32.HTCLIENT);
        }

        private void UpdateMaxButton()
        {
            //获取窗体 最大化 状态。
            bool isMax = WindowState == FormWindowState.Maximized;

            if (isMax) //最大化状态,变换按钮图标、提示
            {
                SystemButtonManager.SystemButtonArray[1].NormalImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.restore_normal.png");
                SystemButtonManager.SystemButtonArray[1].HighLightImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.restore_highlight.png");
                SystemButtonManager.SystemButtonArray[1].DownImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.restore_down.png");
                SystemButtonManager.SystemButtonArray[1].ToolTip = "还原";
            }
            else //非最大化状态,变换按钮图标、提示
            {
                SystemButtonManager.SystemButtonArray[1].NormalImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.max_normal.png");
                SystemButtonManager.SystemButtonArray[1].HighLightImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.max_highlight.png");
                SystemButtonManager.SystemButtonArray[1].DownImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.max_down.png");
                SystemButtonManager.SystemButtonArray[1].ToolTip = "最大化";
            }
        }

        protected void UpdateSystemButtonRect()
        {
            //更新关闭按钮位置矩形。
            Rectangle closeRect = new Rectangle(Width - SystemButtonManager.SystemButtonArray[0].NormalImg.Width,
                -1,
                SystemButtonManager.SystemButtonArray[0].NormalImg.Width,
                SystemButtonManager.SystemButtonArray[0].NormalImg.Height);
            SystemButtonManager.SystemButtonArray[0].LocationRect = CloseBox ? closeRect : Rectangle.Empty;

            //Max
            Rectangle MaxButtonRect = new Rectangle(closeRect.X - SystemButtonManager.SystemButtonArray[1].NormalImg.Width,
                -1,
                SystemButtonManager.SystemButtonArray[1].NormalImg.Width,
                SystemButtonManager.SystemButtonArray[1].NormalImg.Height);
            SystemButtonManager.SystemButtonArray[1].LocationRect = MaximizeBox ? MaxButtonRect : Rectangle.Empty;

            //Min
            //如果没有最小化按钮,则画空白矩形,
            if (!MinimizeBox)
            {
                SystemButtonManager.SystemButtonArray[2].LocationRect = Rectangle.Empty;
                return;
            }
            //如果没有最大化按钮,最小化按钮,就画在关闭按钮旁,
            int MinLocation = MaximizeBox ?
                MaxButtonRect.X - SystemButtonManager.SystemButtonArray[2].NormalImg.Width :
                closeRect.X - SystemButtonManager.SystemButtonArray[2].NormalImg.Width;
            Rectangle MinButtonRect = new Rectangle(MinLocation,
                -1,
                SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
                SystemButtonManager.SystemButtonArray[2].NormalImg.Height);
            SystemButtonManager.SystemButtonArray[2].LocationRect = MinButtonRect;
        } 
        #endregion
    }
}

 

代码,目前还有比较多的功能BUG。有能力,有兴趣的朋友,可以一起完善,可以加我们的QQ群:7261922

 

1/1, 3«1»
公告

欢迎加入我们!!!

开关电源 交流QQ群: 392599244

另启动 交流QQ群: 154186357

EM.CMS 交流QQ群: 26032741

恶魔界 交流QQ群: 7261922

搜索
最新评论
站点数据
栏目总数: 9 个
文章数量: 16 篇
评论数量: 1 条
标签云: 25 个
自定义标签: 0 个
合作伙伴: 3 位
附件数量: 22 个
当前在线: 36 人