⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coolbutton.cs

📁 windows mobile 5 下的渐变Button 按钮.
💻 CS
📖 第 1 页 / 共 2 页
字号:
        /// <remarks>控件文件</remarks>
        /// <value>影石控件</value>
        [DefaultValue("YSBT")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public string ButtonText
        {            
            get
            {
                return strText="YSBT";
            }           
            set
            {
                strText = value;
                OnButtonTextChanged(new EventArgs());
            }
        }
        #endregion

        protected virtual void OnButtonTextChanged(EventArgs e)
        {
            if (eh != null)
            {
                eh(this, new EventArgs());
            }
        }

        public bool YsFlag
        {
            get { return ysFlag; }
            set { ysFlag = value; OnYsFlagChanged(new EventArgs()); }
        }
        protected virtual void OnYsFlagChanged(EventArgs e)
        {
            if (eh != null)
            {
                eh(this, new EventArgs());
            }
        }       

        public Color StartColor
        {
            get
            {
                return startColor;
            }
            set
            {
                startColor = value;
                OnStartColorChanged(new EventArgs());
            }
        }
        protected virtual void OnStartColorChanged(EventArgs e)
        {
            if (eh != null)
            {
                eh(this, new EventArgs());
            }
        }

        public Color EndColor
        {
            get { return endColor; }
            set
            {
                endColor = value;
                OnEndColorChanged(new EventArgs());
                
            }
        }
        protected virtual void OnEndColorChanged(EventArgs e)
        {
            if (eh != null)
            {
                eh(this, new EventArgs());
            }
        }
       
        protected virtual void OnYSSizeChanged(EventArgs e)
        {
            if (eh != null)
            {
                eh(this, new EventArgs());
            }
        }

        #region   基类属性    

        [DefaultValue("YSBT")]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }
        
       

        #endregion

        #endregion

        #region    方法

        protected virtual void Fill(Rectangle rc, Color startColor, Color endColor, FillDirection fillDir,IntPtr hdc)
        {
            TRIVERTEX[] tva = new TRIVERTEX[2];           
            tva[0] = new TRIVERTEX(rc.X, rc.Y, startColor);
            tva[1] = new TRIVERTEX(rc.Right, rc.Bottom, endColor);          
            GRADIENT_RECT[] gra = new GRADIENT_RECT[] { new GRADIENT_RECT(0,1) };           
            GradientFill(hdc, tva, (uint)tva.Length, gra, (uint)gra.Length, (uint)fillDir);                     
        }

       

        #endregion

        #region   继承方法
        //protected override void OnResize(EventArgs e)
        //{
        //    this.Refresh();
        //}
        //protected override void OnTextChanged(EventArgs e)
        //{
        //    //base.OnTextChanged(e);
        //    this.Refresh();
        //}
        protected override void OnMouseDown(MouseEventArgs e)
        {           
            ButtonFlag = 1;            
            Graphics g = Graphics.FromHdc(GetDC(this.Handle));
            YsDraw(g); 
            Invalidate();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {

            if (!MU)
            {
                ButtonFlag = 0;
            }
            else
            {
                ButtonFlag = 0;
            }
            if (BackRed)
            {
                ButtonFlag = 5;
            }

            Invalidate();
        }
        #endregion

        #region   方法
        [MTAThread]
        public void YsDraw(Graphics gr)
        {
            
            IntPtr handle = gr.GetHdc();
            Graphics g = Graphics.FromHdc(handle);
            StringFormat format1 = new StringFormat();
            format1.Alignment = StringAlignment.Center;
            format1.LineAlignment = StringAlignment.Center;
            Rectangle rt = new Rectangle(0, 0, this.Width, this.Height);
            if (!YsFlag)
            {
                if (RoundRect)
                {
                    if (RoundRect)
                    {
                        switch (ButtonFlag)
                        {
                            case 0:
                                FrameColor = Color.DarkBlue;
                                EndColor = Color.SkyBlue;
                                thePen = CreatePen(0, 1, YsRgb(FrameColor));
                                theBrush = CreateSolidBrush(YsRgb(EndColor));
                                oldPen = SelectObject(handle, thePen);
                                oldBrush = SelectObject(handle, theBrush);                               
                                DrawRoundRect(handle, rt.X, rt.Y, rt.Width, rt.Height, RoundLength, RoundLength);   
                                SelectObject(handle, oldPen);
                                DeleteObject(thePen);
                                SelectObject(handle, theBrush);
                                DeleteObject(oldBrush);                                
                                break;
                            case 1:
                                FrameColor = Color.Sienna;                                
                                EndColor = Color.Gold;
                                thePen = CreatePen(0, 1, YsRgb(FrameColor));
                                theBrush = CreateSolidBrush(YsRgb(EndColor));
                                oldPen = SelectObject(handle, thePen);
                                oldBrush = SelectObject(handle, theBrush);
                                DrawRoundRect(handle, rt.X, rt.Y, rt.Width, rt.Height, RoundLength, RoundLength);
                                SelectObject(handle, oldPen);
                                DeleteObject(thePen);
                                SelectObject(handle, theBrush);
                                DeleteObject(oldBrush);   
                                break;
                            case 2:
                                FrameColor = Color.Red;                               
                                EndColor = Color.OrangeRed;
                                thePen = CreatePen(0, 1, YsRgb(FrameColor));
                                theBrush = CreateSolidBrush(YsRgb(EndColor));
                                oldPen = SelectObject(handle, thePen);
                                oldBrush = SelectObject(handle, theBrush);
                                DrawRoundRect(handle, rt.X, rt.Y, rt.Width, rt.Height, RoundLength, RoundLength);
                                SelectObject(handle, oldPen);
                                DeleteObject(thePen);
                                SelectObject(handle, theBrush);
                                DeleteObject(oldBrush);   
                                break;
                            case 3:
                                FrameColor = Color.Tan;                                
                                EndColor = Color.DarkGray;
                                thePen = CreatePen(0, 1, YsRgb(FrameColor));
                                theBrush = CreateSolidBrush(YsRgb(EndColor));
                                oldPen = SelectObject(handle, thePen);
                                oldBrush = SelectObject(handle, theBrush);
                                DrawRoundRect(handle, rt.X, rt.Y, rt.Width, rt.Height, RoundLength, RoundLength);
                                SelectObject(handle, oldPen);
                                DeleteObject(thePen);
                                SelectObject(handle, theBrush);
                                DeleteObject(oldBrush);   
                                break;
                            case 5:
                                FrameColor = Color.Purple;                               
                                EndColor = Color.OrangeRed;
                                thePen = CreatePen(0, 1, YsRgb(FrameColor));
                                theBrush = CreateSolidBrush(YsRgb(EndColor));
                                oldPen = SelectObject(handle, thePen);
                                oldBrush = SelectObject(handle, theBrush);
                                DrawRoundRect(handle, rt.X, rt.Y, rt.Width, rt.Height, RoundLength, RoundLength);
                                SelectObject(handle, oldPen);
                                DeleteObject(thePen);
                                SelectObject(handle, theBrush);
                                DeleteObject(oldBrush);   
                                break;
                            case 6:
                                FrameColor = Color.Goldenrod;                               
                                EndColor = Color.Olive;
                                thePen = CreatePen(0, 1, YsRgb(FrameColor));
                                theBrush = CreateSolidBrush(YsRgb(EndColor));
                                oldPen = SelectObject(handle, thePen);
                                oldBrush = SelectObject(handle, theBrush);
                                DrawRoundRect(handle, rt.X, rt.Y, rt.Width, rt.Height, RoundLength, RoundLength);
                                SelectObject(handle, oldPen);
                                DeleteObject(thePen);
                                SelectObject(handle, theBrush);
                                DeleteObject(oldBrush);   
                                break;
                            case 7:
                                FrameColor = Color.Green;                               
                                EndColor = Color.ForestGreen;
                                thePen = CreatePen(0, 1, YsRgb(FrameColor));
                                theBrush = CreateSolidBrush(YsRgb(EndColor));
                                oldPen = SelectObject(handle, thePen);
                                oldBrush = SelectObject(handle, theBrush);
                                DrawRoundRect(handle, rt.X, rt.Y, rt.Width, rt.Height, RoundLength, RoundLength);
                                SelectObject(handle, oldPen);
                                DeleteObject(thePen);
                                SelectObject(handle, theBrush);
                                DeleteObject(oldBrush);   
                                break;
                            default:                               
                                thePen = CreatePen(0, 1, YsRgb(FrameColor));
                                theBrush = CreateSolidBrush(YsRgb(EndColor));
                                oldPen = SelectObject(handle, thePen);
                                oldBrush = SelectObject(handle, theBrush);
                                DrawRoundRect(handle, rt.X, rt.Y, rt.Width, rt.Height, RoundLength, RoundLength);
                                SelectObject(handle, oldPen);
                                DeleteObject(thePen);
                                SelectObject(handle, theBrush);
                                DeleteObject(oldBrush);   
                                break;
                        }
                    }
                }
                else
                {
                    switch (ButtonFlag)
                    {
                        case 0:
                            FrameColor = Color.Blue;
                            StartColor = Color.White;
                            EndColor = Color.SkyBlue;
                            Fill(rt, StartColor, EndColor, FillDirection.TopToBottom, handle);
                            break;
                        case 1:
                            FrameColor = Color.Sienna;
                            StartColor = Color.GhostWhite;
                            EndColor = Color.Gold;
                            Fill(rt, StartColor, EndColor, FillDirection.TopToBottom, handle);
                            break;
                        case 2:
                            FrameColor = Color.Red;
                            StartColor = Color.GhostWhite;
                            EndColor = Color.OrangeRed;
                            Fill(rt, StartColor, EndColor, FillDirection.TopToBottom, handle);
                            break;
                        case 3:
                            FrameColor = Color.Tan;
                            StartColor = Color.White;
                            EndColor = Color.DarkGray;
                            Fill(rt, StartColor, EndColor, FillDirection.TopToBottom, handle);
                            break;
                        case 5:
                            FrameColor = Color.Purple;
                            StartColor = Color.GhostWhite;
                            EndColor = Color.OrangeRed;
                            Fill(rt, StartColor, EndColor, FillDirection.TopToBottom, handle);
                            break;
                        case 6:
                            FrameColor = Color.Goldenrod;
                            StartColor = Color.LightGoldenrodYellow;
                            EndColor = Color.Olive;
                            Fill(rt, StartColor, EndColor, FillDirection.TopToBottom, handle);
                            break;
                        case 7:
                            FrameColor = Color.Green;
                            StartColor = Color.LawnGreen;
                            EndColor = Color.ForestGreen;
                            Fill(rt, StartColor, EndColor, FillDirection.TopToBottom, handle);
                            break;
                        default:
                            FrameColor = Color.Blue;
                            Fill(rt, StartColor, EndColor, FillDirection.TRIANGLE, handle);
                            break;
                    }
                }
            }
            else
            {
                if (!RoundRect)
                {
                    FrameColor = Color.Tan;
                    StartColor = Color.White;
                    EndColor = Color.DarkGray;
                    Fill(rt, StartColor, EndColor, FillDirection.TopToBottom, handle);
                }
                else
                {
                    FrameColor = Color.Tan;                   
                    EndColor = Color.DarkGray;
                    thePen = CreatePen(0, 1, YsRgb(FrameColor));
                    theBrush = CreateSolidBrush(YsRgb(EndColor));
                    oldPen = SelectObject(handle, thePen);
                    oldBrush = SelectObject(handle, theBrush);
                    DrawRoundRect(handle, rt.X, rt.Y, rt.Width, rt.Height, RoundLength, RoundLength);
                    SelectObject(handle, oldPen);
                    DeleteObject(thePen);
                    SelectObject(handle, theBrush);
                    DeleteObject(oldBrush);
                }
            }
            gr.ReleaseHdc(handle);           
            Pen p = new Pen(FrameColor);
            if (!RoundRect)
            {
                g.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), new RectangleF(0f, 0f, (float)this.Width, (float)this.Height), format1);
                g.DrawRectangle(p, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
            }
            else
            {
                g.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), new RectangleF((float)(this.Width/10),(float)(this.Height/10), (float)(this.Width-this.Width/10*2), (float)(this.Height-this.Height/10*2)), format1);
            }
            g.Dispose();           
        }
        #endregion

        public CoolButton()
        {
            this.Visible = false;
            this.Invoke(new CBDelegate(InitializeComponent));
            //InitializeComponent();            
            this.Visible = true;
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: 在此处添加自定义绘制代码
            YsDraw(pe.Graphics);           
            // 调用基类 OnPaint
            //base.OnPaint(pe);
            
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -