• winform窗体、控件的简单封装,重做标题栏


    1标题栏封装中使用了以下技术:

    知识点:
    1.父类、子类的继承
    2.窗体之间的继承;
    3.自定义控件的绘制;
    4.多线程在窗体间的应用;
    5.窗体和控件的封装;
    6.回调函数(委托);

    效果展示

    在这里插入图片描述

    部分代码展示

    	/// 
        /// 标题栏
        /// 
        public sealed partial class UcViewTitle : UserControl
        {
    
            /// 
            /// 鼠标按下时子控件、父控件的位置
            /// 
            private Point _mouseDownChildLocation, _mouseDownParentLocation;
    
            /// 
            /// 标题栏文本字体
            /// 
            public Font TitleFont { get; set; } = new Font("微软雅黑", 24.25F, FontStyle.Regular, GraphicsUnit.Point, 134);
    
            /// 
            /// 标题栏文本内容
            /// 
            public string Title { get; set; } = "科技未来,等你引领";
    
            /// 
            /// 标题栏背景颜色
            /// 
            public Color TitleBackColor { get; set; } = Color.FromArgb(0, 49, 71);
    
            /// 
            /// 标题栏文本颜色
            /// 
            public Color TitleTextBackColor { get; set; } = Color.White;
    
            /// 
            /// 回调函数:关闭窗体时触发
            /// 
            public Action CallbackCloseForm;
    
            /// 
            /// 鼠标双击时设置窗体最大化、默认状态
            /// 
            public Func<FormWindowState> FunGetFormState;
    
            /// 
            /// 鼠标双击时设置窗体最大化、默认状态
            /// 
            public Action<FormWindowState> ActionMouseDouble;
    
            /// 
            /// 鼠标按下时窗体的位置
            /// 
            public Func<Point> ActionMouseDownLocation;
    
            /// 
            /// 鼠标移动后窗体的新位置
            /// 
            public Action<Point> ActionMouseMoveLocation;
    
            /// 
            /// 构造函数
            /// 
            public UcViewTitle()
            {
                InitializeComponent();
                this.DoubleBuffered = true;
                SetToolTip();
            }
    
            private void UcViewTitle_Load(object sender, EventArgs e)
            {
                new Task(() =>
                {
                    try
                    {
                        while (true)
                        {
                            Thread.Sleep(1000);
                            Invoke(new EventHandler(delegate { this.Refresh(); }));
                        }
                    }
                    catch (Exception ex)
                    {
    
    
                    }
                }).Start();
            }
    
    
            [Description("跟随串口大小重新绘制标题栏")]
            private void UcTitle_SizeChanged(object sender, EventArgs e)
            {
                pic_Close.Location = new Point(this.Width - 5 - pic_Close.Width, this.Height / 2 - pic_Close.Height / 2);
                pic_Maximize.Location = new Point(pic_Close.Location.X - 5 - pic_Minimality.Width, this.Height / 2 - pic_Maximize.Height / 2);
                pic_Minimality.Location = new Point(pic_Maximize.Location.X - 5 - pic_Minimality.Width, this.Height / 2 - pic_Close.Height / 2);
                //pic_Logo.Location = new Point(10, this.Height / 2 - pic_Logo.Height / 2);
                SetPictureMaxIcon();
                Refresh();
            }
    
            [Description("绘制标题栏")]
            private void UcTitle_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.Clear(TitleBackColor);
                // e.Graphics.Clear(Color.FromArgb(14, 30, 63));
                e.Graphics.SmoothingMode = SmoothingMode.HighQuality;//指定抗锯齿的呈现
    
                var brush = new SolidBrush(TitleTextBackColor);
    
                #region [绘制日期时间]
    
                var textTimeStartX = pic_Logo.Location.X + pic_Logo.Width + 10;
                var textTimeFont = new Font(TitleFont.Name, 14f, FontStyle.Regular, GraphicsUnit.Point, 134);
    
                string text = GetDate();
                var textDateSize = e.Graphics.MeasureString(text, textTimeFont);
                PointF textLocation = new PointF(textTimeStartX, this.Height / 2);
                e.Graphics.DrawString(text, textTimeFont, brush, textLocation);
    
                text = GetTime();
                var textTimeSize = e.Graphics.MeasureString(text, textTimeFont);
                textLocation = new PointF(textTimeStartX + textDateSize.Width / 2 - textTimeSize.Width / 2, this.Height / 2 - textDateSize.Height);
                e.Graphics.DrawString(text, textTimeFont, brush, textLocation);
                #endregion [绘制日期时间]
    
                //绘制标题
                var textTitleSize = e.Graphics.MeasureString(Title, TitleFont);
                textLocation = new PointF(this.Width / 2.0f - textTitleSize.Width / 2.0f, this.Height / 2.0f - textTitleSize.Height / 2.0f);
                e.Graphics.DrawString(Title, TitleFont, brush, textLocation);
    
            }
    
            [Description("最小化窗口")]
            private void pic_Minimality_Click(object sender, EventArgs e) => ActionMouseDouble?.Invoke(FormWindowState.Minimized);
    
            [Description("最大化窗口")]
            private void pic_Maximize_Click(object sender, EventArgs e) => SwitchWindowState();
    
            [Description("关闭窗口")]
            private void pic_Close_Click(object sender, EventArgs e) => CallbackCloseForm?.Invoke();
    
            [Description("鼠标双击时设置窗体最大化、默认状态")]
            private void UcViewTitle_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left) return;
                SwitchWindowState();
            }
    
            [Description("鼠标按下记录位置信息用于计算移动窗口的初始值")]
            private void UcViewTitle_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    _mouseDownChildLocation = e.Location;
                }
            }
    
            [Description("鼠标按下并移动时设置窗口新的位置信息")]
            private void UcViewTitle_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    ActionMouseDouble?.Invoke(FormWindowState.Normal);
                    if (ActionMouseDownLocation != null)
                    {
                        _mouseDownParentLocation = ActionMouseDownLocation();
                    }
                    var addX = e.X - _mouseDownChildLocation.X;
                    var addY = e.Y - _mouseDownChildLocation.Y;
                    ActionMouseMoveLocation?.Invoke(new Point(_mouseDownParentLocation.X + addX, _mouseDownParentLocation.Y + addY));
                }
            }
    
            [Description("设置最小化、最大化、关闭的鼠标悬空提示信息")]
            private void SetToolTip()
            {
                new ToolTip().SetToolTip(pic_Minimality, "最小化");
                new ToolTip().SetToolTip(pic_Maximize, "最大化");
                new ToolTip().SetToolTip(pic_Close, "关闭");
            }
    
            [Description("鼠标双击时设置窗体最大化、默认状态")]
            private void SwitchWindowState()
            {
                if (FunGetFormState != null)
                {
                    var state = FunGetFormState();
                    state = state == FormWindowState.Maximized
                        ? FormWindowState.Normal
                        : FormWindowState.Maximized;
                    ActionMouseDouble?.Invoke(state);
                    SetPictureMaxIcon();
                }
            }
    
            [Description("设置最大化图标")]
            private void SetPictureMaxIcon()
            {
                if (FunGetFormState == null) return;
    
                var state = FunGetFormState();
                switch (state)
                {
                    case FormWindowState.Normal:
                        pic_Maximize.BackgroundImage = Resources.FormTitle_最大化1;
                        break;
                    default:
                        pic_Maximize.BackgroundImage = Resources.FormTitle_最大化;
                        break;
                }
            }
    
            /// 
            /// 获取时间串(格式:2008年08月08日 10:20:06)
            /// 
            /// 
            private string GetDate()
            {
                DateTime ts = DateTime.Now;
                var str = new StringBuilder();
                str.Append($"{ts.Year}年");
                str.Append($"{ts.Month.ToString().PadLeft(2, '0')}月");
                str.Append($"{ts.Day.ToString().PadLeft(2, '0')}日");
                return str.ToString();
            }
            /// 
            /// 获取时间串(格式:10:20:06)
            /// 
            /// 
            private string GetTime()
            {
                DateTime ts = DateTime.Now;
                var str = new StringBuilder();
                str.Append($"{ts.Hour.ToString().PadLeft(2, '0')}:");
                str.Append($"{ts.Minute.ToString().PadLeft(2, '0')}:");
                str.Append($"{ts.Second.ToString().PadLeft(2, '0')}");
                return str.ToString();
            }
    
            /// 
            /// 设置LOGO
            /// 
            /// LOGO图像
            /// LOGO的宽度
            public void SetLogo(Bitmap bmp, int logoWidth)
            {
                pic_Logo.Visible = true;
                pic_Logo.BackgroundImage = bmp;
                pic_Logo.Width = logoWidth;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
  • 相关阅读:
    Util应用框架基础(一) - 依赖注入
    力扣(LeetCode)82. 删除排序链表中的重复元素 II(C语言)
    git 命令使用
    JS 找到字符串中所有的数字部分
    方法参数调用-两种情况
    git 使用
    科技连接美好未来 | 美格智能5G FWA解决方案持续推进
    测试大老都是怎么理解cookie&session的?
    测试日常工作中需要具备哪些知识和能力,在需求评审时需要考虑哪些方面,在技术方面评审时需要考虑哪些方面,从什么方面进行设计测试用例
    「学习笔记」组合计数与中国剩余定理
  • 原文地址:https://blog.csdn.net/weixin_47492910/article/details/134421840