c# WinForm之窗体美化

本文详细介绍了如何使用C#实现WinForm窗体的美化,包括自定义标题栏颜色、边框、按钮样式等,通过设置系统API、处理鼠标区域状态值和重载消息处理过程来实现丰富的交互效果。
摘要由CSDN通过智能技术生成

1、定义系统API

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowLong")]
        public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

2、定义变量

        private Color initColor = Color.FromArgb(72, 116, 162);
        private Color seleColor = Color.FromArgb(100, 100, 100);
        private Color fontColor = Color.White;
        private int captionHeight = 30;
        private int borderWidth = 3;
        private int buttonWidth = 30;
        private int iconWidth = 22;
        //鼠标区域状态值
        private int MouseLoctionStatue = 0;

3、初始化基类窗体值

        public Form()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            base.FormBorderStyle = FormBorderStyle.None;
            base.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;
            base.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
            base.Padding = new Padding(borderWidth, captionHeight, borderWidth, borderWidth);
        }

4、返回鼠标区域状态值

        private int GetMouseLoction(int x, int y)
        {
            if (x > 0 && x < this.Width && y > 0 && y < this.Height)
            {
                if (x > 0 && x < 5 && y > 5 && y < this.Height - 5)
                {
                    //Left
                    this.Cursor = Cursors.SizeWE;
                    return 0xF001;
                }
                else if (x > this.Width - 5 && x < this.Width && y > 5 && y < this.Height - 5)
                {
                    //Right
                    this.Cursor = Cursors.SizeWE;
                    return 0xF002;
                }
                else if (x > 5 && x < this.Width - 5 && y > 0 && y < 5)
                {
                    //Top
                    this.Cursor = Cursors.SizeNS;
                    return 0xF003;
                }
                else if (x > 5 && x < this.Width - 5 && y > this.Height - 5 && y < this.Height)
                {
                    //Bottom
                    this.Cursor = Cursors.SizeNS;
                    return 0xF006;
                }
                else if (x > 0 && x < 5 && y > 0 && y < 5)
                {
                    //TopLeft
                    this.Cursor = Cursors.SizeNWSE;
                    return 0xF004;
                }
                else if (x > this.Width - 5 && x < this.Width && y > 0 && y < 5)
                {
                    //TopRight
                    this.Cursor = Cursors.SizeNESW;
                    return 0xF005;
                }
                else if (x > 0 && x < 5 && y > this.Height - 5 && y < this.Height)
                {
                    //BottomLeft
                    this.Cursor = Cursors.SizeNESW;
                    return 0xF007;
                }
                else if (x > this.Width - 5 && x < this.Width && y > this.Height - 5 && y < this.Height)
                {
                    //BottomRight
                    this.Cursor = Cursors.SizeNWSE;
                    return 0xF008;
                }
                else if (x > 5 && x < this.Width - 3 * buttonWidth - 5 && y > 5 && y < captionHeight)
                {
                    //Caption
                    this.Cursor = Cursors.Default;
                    return 0xF012;
                }
                else if (x > this.Width - 3 * buttonWidth - 5 && x < this.Width - 2 * buttonWidth - 5 && y > 5 && y < captionHeight)
                {
                    //Min
                    this.Cursor = Cursors.Default;
                    if (base.MaximizeBox && base.MinimizeBox)
                        return 1;
             

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值