珂珂的个人博客 - 一个程序猿的个人网站

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(二)-基窗体

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(一)-效果图

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(二)-基窗体

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(三)-主窗体

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(四)-MessageBox

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(五)-按钮1

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(六)-按钮2

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(七)-按钮3

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(八)-文本框

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(九)-有边框的PictureBox

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(十)-右键菜单

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(十一)-换颜色

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(十二)-换背景

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(十三)-TabControl

接前面,来看基窗体代码

构造函数和属性

 private Size oldSize;
        public BaseForm()
        {
            InitializeComponent();
            if (!DesignMode)
            {
                //StrongNameCodeAccessPermission.Demand();
            }
        }

        public bool AllowMove
        {
            get { return _allowMove; }
            set { _allowMove = value; }
        }
        private bool _allowMove = true;

重载Load事件

Win32.CreateRoundRectRgn(0, 0, this.Width + 1, this.Height + 1, 5, 5);

将窗体设做成圆角

 protected override void OnLoad(EventArgs e)
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            //this.SetStyle(ControlStyles.ResizeRedraw, true);
            Version v = System.Environment.Version;
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            TaskMenu.ShowSYSMENU(this);
            int Rgn = Win32.CreateRoundRectRgn(0, 0, this.Width + 1, this.Height + 1, 5, 5);
            Win32.SetWindowRgn(this.Handle, Rgn, true);

            this.BackgroundImageLayout = MyFormTemp.CurrentImageLayout;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.BackColor = MyFormTemp.CurrentBackgroundColor;
            if (MyFormTemp.CurrentBackgroundImage != null)
            {
                this.BackgroundImage = MyFormTemp.CurrentBackgroundImage;
            }
            if (this.StartPosition == FormStartPosition.CenterScreen && (!this.DesignMode))
            {
                this.Location = new Point((Screen.PrimaryScreen.Bounds.Width - this.Width) / 2, (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2);
            }
            ResizeControl();
            // this.TransparencyKey = Color.FromArgb(255,0,255);
            // this.TransparencyKey = BackColor;

            base.OnLoad(e);
            oldSize = this.Size;
        }

重载windows消息

 protected override void WndProc(ref Message m)
        {
            if (DesignMode)
            {
                switch (m.Msg)
                {
                    case Win32.WM_NCACTIVATE:
                        if (m.WParam == (IntPtr)0)
                        {
                            m.Result = (IntPtr)1;
                        }
                        if (m.WParam == (IntPtr)2097152)
                        {
                            m.Result = (IntPtr)1;
                        }
                        break;
                    case Win32.WM_NCCALCSIZE:
                        break;
                    default:
                        base.WndProc(ref m);
                        break;
                }

                return;
            }
            switch (m.Msg)
            {
                case Win32.WM_NCPAINT:
                    break;
                case Win32.WM_NCACTIVATE:
                    if (m.WParam == (IntPtr)0)
                    {
                        m.Result = (IntPtr)1;
                    }
                    if (m.WParam == (IntPtr)2097152)
                    {
                        m.Result = (IntPtr)1;
                    }
                    break;
                case Win32.WM_NCCALCSIZE:
                    break;
                case Win32.WM_SYSCOMMAND:

                    if (m.WParam == (IntPtr)Win32.SC_RESTORE || m.WParam.ToInt32() == Win32.SC_RESTORE + 2)
                    {
                        this.Size = oldSize;
                    }
                    else if (m.WParam == (IntPtr)Win32.SC_MINIMIZE || m.WParam.ToInt32() == Win32.SC_MINIMIZE + 2)
                    {
                        //if (oldSize.Width == 0)
                        // oldSize = this.Size;
                    }
                    base.WndProc(ref m);
                    break;

                case Win32.WM_NCHITTEST:
                    Point vPoint = new Point((int)m.LParam & 0xFFFF, (int)m.LParam >> 16 & 0xFFFF);
                    vPoint = PointToClient(vPoint);
                    if (MaximizeBox)
                    {
                        if (vPoint.X = Width - 3)
                        {
                            if (vPoint.Y = Height - 3)
                                m.Result = (IntPtr)Win32.HTBOTTOMRIGHT;
                            else m.Result = (IntPtr)Win32.HTRIGHT;
                        }
                        else if (vPoint.Y = Height - 3)
                            m.Result = (IntPtr)Win32.HTBOTTOM;
                    }
                    if (AllowMove)
                    {
                        if (vPoint.Y < Height - 3 && vPoint.Y > 2 && (vPoint.X > 3 && vPoint.X < Width - 3))
                            m.Result = (IntPtr)Win32.HTCAPTION;
                    }
                    else
                    {
                        if (vPoint.Y < 31 && vPoint.Y > 2 && (vPoint.X > 3 && vPoint.X < Width - 3))
                            m.Result = (IntPtr)Win32.HTCAPTION;
                    }

                    break;
                case Win32.WM_NCLBUTTONDBLCLK:
                    if (MaximizeBox)
                    {
                        base.WndProc(ref m);
                        this.Invalidate(false);
                        UpdateMaxButton();
                    }
                    break;
                case 0x00a4:
                    {
                        if (this.ContextMenuStrip != null)
                            this.ContextMenuStrip.Show(System.Windows.Forms.Control.MousePosition);
                    }
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

最关键的绘制

重载DrawBackground事件  也可以是OnPaint事件

 protected virtual void DrawBackground(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Bitmap Bmp = global::FYJ.Winform.Properties.Resources.base_form_bg;
            g.DrawImage(Bmp, new Rectangle(0, 0, 5, 31), 0, 0, 5, 31, GraphicsUnit.Pixel);
            g.DrawImage(Bmp, new Rectangle(5, 0, this.Width - 10, 31), 5, 0, Bmp.Width - 10, 31, GraphicsUnit.Pixel);
            g.DrawImage(Bmp, new Rectangle(this.Width - 5, 0, 5, 31), Bmp.Width - 5, 0, 5, 31, GraphicsUnit.Pixel);

            g.DrawImage(Bmp, new Rectangle(0, 31, 2, 25), 0, 31, 2, 25, GraphicsUnit.Pixel);
            g.DrawImage(Bmp, new Rectangle(2, 31, this.Width - 4, 25), 2, 31, 5, 25, GraphicsUnit.Pixel);
            g.DrawImage(Bmp, new Rectangle(this.Width - 2, 31, 2, 25), Bmp.Width - 2, 31, 2, 25, GraphicsUnit.Pixel);

            g.DrawImage(Bmp, new Rectangle(0, 56, 5, Height - 90), 0, Bmp.Height - 230, 5, 130, GraphicsUnit.Pixel);
            g.DrawImage(Bmp, new Rectangle(5, 56, this.Width - 10, Height - 90), 5, Bmp.Height - 230, Bmp.Width - 10, 130, GraphicsUnit.Pixel);
            g.DrawImage(Bmp, new Rectangle(this.Width - 5, 56, 5, Height - 90), Bmp.Width - 5, Bmp.Height - 230, 5, 130, GraphicsUnit.Pixel);

            g.DrawImage(Bmp, new Rectangle(0, this.Height - 34, 5, 34), 0, Bmp.Height - 34, 5, 34, GraphicsUnit.Pixel);
            g.DrawImage(Bmp, new Rectangle(5, this.Height - 34, this.Width - 10, 34), 5, Bmp.Height - 34, Bmp.Width - 10, 34, GraphicsUnit.Pixel);
            g.DrawImage(Bmp, new Rectangle(this.Width - 5, this.Height - 34, 5, 34), Bmp.Width - 5, Bmp.Height - 34, 5, 34, GraphicsUnit.Pixel);

            int strX = 10;
            if (this.ShowIcon)
            {
                strX = 30;
                g.DrawIcon(Icon, new Rectangle(8, 7, 16, 16));
            }

            if (!String.IsNullOrEmpty(this.Text))
            {
                Font f = null;
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    f = new Font("微软雅黑", 11F, FontStyle.Bold);
                    g.DrawString(this.Text, f, titleColor, strX, 4);
                    //g.DrawImage(Bmp, new Rectangle(strX, 4, (int)SkinUtil.GetStrWidth(Text, f), 20), 0, 0, Bmp.Width, Bmp.Height, GraphicsUnit.Pixel);
                }
                else
                {
                    f = new Font("宋体", 11F, FontStyle.Bold);
                    g.DrawString(this.Text, f, titleColor, strX, 8);
                }
                // g.DrawString(this.Text, new Font("宋体", 11F, FontStyle.Regular), Brushes.WhiteSmoke, strX, 8);
            }

            Bmp.Dispose();
        }

三个按钮事件即最大化、最小化、关闭

 private void ButtonMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void UpdateMaxButton()
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.ButtonMax.ToolTipString = "最大化";
                this.ButtonMax.BackgroundImage = global::FYJ.Winform.Properties.Resources.btn_max_normal;   //这句写到下三条语句最前
                this.ButtonMax.MouseNormalImage = global::FYJ.Winform.Properties.Resources.btn_max_normal;
                this.ButtonMax.MouseEnterImage = global::FYJ.Winform.Properties.Resources.btn_max_highlight;
                this.ButtonMax.MouseDownImage = global::FYJ.Winform.Properties.Resources.btn_max_down;

            }
            else
            {
                this.ButtonMax.ToolTipString = "向下还原";
                this.ButtonMax.BackgroundImage = global::FYJ.Winform.Properties.Resources.btn_restore_normal; //这个句写到下三条语句最前
                this.ButtonMax.MouseNormalImage = global::FYJ.Winform.Properties.Resources.btn_restore_normal;
                this.ButtonMax.MouseEnterImage = global::FYJ.Winform.Properties.Resources.btn_restore_highlight;
                this.ButtonMax.MouseDownImage = global::FYJ.Winform.Properties.Resources.btn_restore_down;

            }
        }

        private void ButtonMax_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Maximized;
                Win32.SendMessage(this.Handle, Win32.WM_SYSCOMMAND, Win32.SC_MAXIMIZE, 0);
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
                Win32.SendMessage(this.Handle, Win32.WM_SYSCOMMAND, Win32.SC_RESTORE, 0);
            }
            UpdateMaxButton();
            this.Invalidate(false);
        }

        protected virtual void ButtonClose_Click(object sender, EventArgs e)
        {
            Win32.SendMessage(this.Handle, Win32.WM_SYSCOMMAND, Win32.SC_CLOSE, 0);
            this.Close();
        }

源码将在最后一章中放出


上一篇:wordpress3.6 修改的地方

下一篇:个人代码全部开源


0 评论

查看所有评论

给个评论吧