FormBase.cs 13.7 KB
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Asa.FaceControl
{
    public partial class FormBase : Form
    {
        //=====属性变量=====
        private int _borderWidth = 2;
        private Bitmap _iconBmp = null;
        private Font _titleFont = new("宋体", 12f);
        private int _titleHeight = 36;

        //=====私有变量=====
        private CursorPlace place;
        private RectangleF borderRect;
        private RectangleF iconRect;
        private RectangleF titleTextRect;
        private Rectangle windowRect;
        private Bitmap iconGrayBmp;
        private bool formActivated = true;
        private readonly SolidBrush BRUSH_TEXT;
        private readonly SolidBrush BRUSH_TEXT_DISABLED;

        //=====子类变量=====
        internal IFaceThemeColor theme;

        public FormBase()
        {
            InitializeComponent();

            theme = new DarkThemeColor();
            BRUSH_TEXT = new SolidBrush(theme.FORE);
            BRUSH_TEXT_DISABLED = new SolidBrush(theme.DISABLED);

            AutoScaleMode = AutoScaleMode.None;
            BackColor = theme.BACK;
            ForeColor = theme.FORE;
            Padding = new Padding(6 + _borderWidth);
            _iconBmp = base.Icon.ToBitmap();
            iconGrayBmp = ImageConvert.ToDisabledGray(_iconBmp, 0);

            Activated += FormBase_Activated;
            Deactivate += FormBase_Deactivate;
        }





        //=====公共方法=====
        public Bitmap ToImage()
        {
            IntPtr hSrce = API.GetWindowDC(Handle);
            IntPtr hDest = API.CreateCompatibleDC(hSrce);
            IntPtr hBmp = API.CreateCompatibleBitmap(hSrce, Width, Height);
            IntPtr hOldBmp = API.SelectObject(hDest, hBmp);
            if (API.BitBlt(hDest, 0, 0, Width, Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
            {
                Bitmap bmp = Image.FromHbitmap(hBmp);
                API.SelectObject(hDest, hOldBmp);
                API.DeleteObject(hBmp);
                API.DeleteDC(hDest);
                API.ReleaseDC(Handle, hSrce);
                return bmp;
            }
            return null;
        }

        public void Maximize()
        {
            windowRect = new Rectangle(Location, Size);
            Screen screen = Screen.FromControl(this);
            Top = screen.WorkingArea.Top;
            Left = screen.WorkingArea.Left;
            Width = screen.WorkingArea.Width;
            Height = screen.WorkingArea.Height;
            IsMaximized = true;
        }

        public void Restore()
        {
            Left = windowRect.Left;
            Top = windowRect.Top;
            Width = windowRect.Width;
            Height = windowRect.Height;
            IsMaximized = false;
        }



        //=====子类方法=====
        protected virtual void CalcSize()
        {
            //边框位置
            float x = _borderWidth / 2f;
            float y = _borderWidth / 2f;
            float width = ClientSize.Width - _borderWidth - 1;
            float height = ClientSize.Height - _borderWidth - 1;
            borderRect = new RectangleF(x, y, width, height);
            //titleRect = new RectangleF(x, y, width, _titleHeight);

            //标题文本位置
            using Graphics g = CreateGraphics();
            SizeF sf = g.MeasureString(Text, _titleFont);
            x = (Width - sf.Width) / 2f;
            y = (_titleHeight - sf.Height) / 2f;
            titleTextRect = new RectangleF(x, y, sf.Width, sf.Height);

            //图标位置
            if (_iconBmp != null)
            {
                height = _titleHeight - 6;
                width = _iconBmp.Width * height / _iconBmp.Height;
                iconRect = new RectangleF(borderRect.X + 3, borderRect.Y + 3, width, height);
            }
        }

        protected virtual void DrawEnabled(Graphics g)
        {
        }

        protected virtual void DrawDisabled(Graphics g)
        {
        }



        //=====事件=====
        [Browsable(true)]
        public new event EventHandler TextChanged;
        [Browsable(true)]
        public event EventHandler BorderWidthChanged;



        //=====属性=====
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int BorderWidth
        {
            get => _borderWidth;
            set
            {
                _borderWidth = value > 0 ? value : 0;
                Padding = new Padding(6 + _borderWidth);
                CalcSize();
                Refresh();
                BorderWidthChanged?.Invoke(this, new EventArgs());
            }
        }

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get => base.Text;
            set
            {
                base.Text = value;
                CalcSize();
                Refresh();
                TextChanged?.Invoke(this, new EventArgs());
            }
        }

        [Browsable(true), Category(""), Description("图标"), DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public new Bitmap Icon
        {
            get => _iconBmp;
            set
            {
                _iconBmp = value;
                iconGrayBmp = ImageConvert.ToDisabledGray(_iconBmp, 0);
                CalcSize();
                Refresh();
            }
        }

        [Browsable(true), Category("Title"), Description("标题字体")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Font TitleFont
        {
            get => _titleFont;
            set
            {
                _titleFont = value;
                CalcSize();
                Refresh();
            }
        }

        [Browsable(true), Category("Title"), Description("标题栏高度"), DefaultValue(36)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int TitleHeight
        {
            get => _titleHeight;
            set
            {
                _titleHeight = value;
                CalcSize();
                Refresh();
            }
        }

        [Browsable(false)]
        public new Font Font { get; set; }

        internal bool IsMaximized { get; set; } = false;

        internal bool CanResize { get; set; } = true;


        public void SetLostFocus(Control ctl, IntPtr ptr)
        {
            foreach (Control list in ctl.Controls)
            {
                if (list is ControlBase control)
                {
                    if (control.Handle == ptr) continue;
                    control.LostFocused();
                }
                else if (list is PanelBase)
                {
                    SetLostFocus(list, ptr);
                }
            }
        }



        private void DrawBorder(Graphics g)
        {
            if (_borderWidth > 0)
            {
                Pen borderPen = new(formActivated ? theme.BORDER_ENTER : theme.BORDER_LEAVE, _borderWidth);
                g.DrawRectangle(borderPen, borderRect.X, borderRect.Y, borderRect.Width, borderRect.Height);
            }

        }

        private void DrawTitle(Graphics g)
        {
            if (_iconBmp != null)
                g.DrawImage(formActivated ? _iconBmp : iconGrayBmp, iconRect, new RectangleF(0, 0, _iconBmp.Width, _iconBmp.Height), GraphicsUnit.Pixel);
            if (formActivated)
                g.DrawString(Text, _titleFont, BRUSH_TEXT, titleTextRect);
            else
                g.DrawString(Text, _titleFont, BRUSH_TEXT_DISABLED, titleTextRect);
        }

        private void ChangeMouseCursor(Point pt)
        {
            int space = 6;
            Cursor cursor = Cursors.Default;

            if (CanResize)
            {
                if (pt.X <= space && pt.Y <= space)  //左上角
                {
                    cursor = Cursors.SizeNWSE;
                    place = CursorPlace.LeftTop;
                }
                else if (pt.X > space && pt.X < Width - space && pt.Y <= space)  //上边
                {
                    cursor = Cursors.SizeNS;
                    place = CursorPlace.Top;
                }
                else if (pt.X >= Width - space && pt.Y <= space)  //右上角
                {
                    cursor = Cursors.SizeNESW;
                    place = CursorPlace.RightTop;
                }
                else if (pt.X <= space && pt.Y > space && pt.Y < Height - space)  //左边
                {
                    cursor = Cursors.SizeWE;
                    place = CursorPlace.Left;
                }
                else if (pt.X >= Width - space && pt.Y > space && pt.Y < Height - space)  //右边
                {
                    cursor = Cursors.SizeWE;
                    place = CursorPlace.Right;
                }
                else if (pt.X <= space && pt.Y >= Height - space)  //左下角
                {
                    cursor = Cursors.SizeNESW;
                    place = CursorPlace.LeftBottom;
                }
                else if (pt.X > space && pt.X < Width - space && pt.Y >= Height - space)  //下边
                {
                    cursor = Cursors.SizeNS;
                    place = CursorPlace.Bottom;
                }
                else if (pt.X >= Width - space - space && pt.Y >= Height - space - space)  //右下角
                {
                    cursor = Cursors.SizeNWSE;
                    place = CursorPlace.RightBottom;
                }
                else if (pt.X > space && pt.X < Width - space && pt.Y > space && pt.Y < _titleHeight)  //标题栏
                {
                    cursor = Cursors.Default;
                    place = CursorPlace.Inside;
                }
                else if (pt.X > space && pt.X < Width - space && pt.Y > _titleHeight && pt.Y < Height - space)  //中间
                {
                    cursor = Cursors.Default;
                    place = CursorPlace.None;
                }
            }
            else
            {
                if (pt.X > space && pt.X < Width - space && pt.Y > space && pt.Y < _titleHeight)  //标题栏
                {
                    cursor = Cursors.Default;
                    place = CursorPlace.Inside;
                }
                else
                {
                    cursor = Cursors.Default;
                    place = CursorPlace.None;
                }
            }

            if (cursor != Cursor)
                Cursor = cursor;
        }

        private void ChangeFormSize()
        {
            if (place == CursorPlace.None)
                return;
            API.ReleaseCapture();
            API.SendMessage(Handle, API.WM_SYSCOMMAND, (int)place, 0);
        }



        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0014)  //禁掉清除背景消息
                return;
            base.WndProc(ref m);
        }

        private void FormBase_Load(object sender, EventArgs e)
        {
            CalcSize();
            Refresh();

            //居中屏幕
            if (!DesignMode)
            {
                Screen screen = Screen.FromControl(this);
                int x = (screen.WorkingArea.Width - Width) / 2;
                int y = (screen.WorkingArea.Height - Height) / 2;
                Location = new Point(x, y);
            }

            windowRect = new Rectangle(Location, Size);
            Language.SetLanguage(this);
        }

        private void FormBase_Resize(object sender, EventArgs e)
        {
            CalcSize();
            Refresh();
        }

        private void FormBase_Paint(object sender, PaintEventArgs e)
        {
            BufferedGraphicsContext current = BufferedGraphicsManager.Current;
            BufferedGraphics bg = current.Allocate(e.Graphics, e.ClipRectangle);
            Graphics g = bg.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.Clear(BackColor);  //背景

            DrawTitle(g);
            DrawBorder(g);

            bg.Render();
            bg.Dispose();
        }

        private void FormBase_MouseDown(object sender, MouseEventArgs e)
        {
            SetLostFocus(this, Handle);

            if (e.Button == MouseButtons.Left)
            {
                if (!IsMaximized)
                    ChangeFormSize();
            }
        }

        private void FormBase_MouseUp(object sender, MouseEventArgs e)
        {

        }

        private void FormBase_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseButtons == MouseButtons.None)
            {
                if (!IsMaximized)  //不是最大化
                    ChangeMouseCursor(e.Location);
            }
        }

        private void FormBase_Deactivate(object sender, EventArgs e)
        {
            formActivated = false;
            Refresh();
        }

        private void FormBase_Activated(object sender, EventArgs e)
        {
            formActivated = true;
            Refresh();
        }


        private enum CursorPlace : int
        {
            None = 0,
            Left = 0xF001,
            Top = 0xF003,
            Right = 0xF002,
            Bottom = 0xF006,
            LeftTop = 0xF004,
            RightTop = 0xF005,
            LeftBottom = 0xF007,
            RightBottom = 0xF008,
            Inside = API.SC_MOVE + API.HTCAPTION
        }

    }
}