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

📄 imagebutton.cs

📁 用C#写的USB数据采集程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
            base.OnResize(e);
            if (_showshadow)
            {
                _p[0].X = 0;
                _p[0].Y = 2;
                _p[1].X = 2;
                _p[1].Y = 0;
                _p[2].X = Width - 6;
                _p[2].Y = 0;
                _p[3].X = Width - 4;
                _p[3].Y = 2;
                _p[4].X = Width - 4;
                _p[4].Y = Height - 6;
                _p[5].X = Width - 6;
                _p[5].Y = Height - 4;
                _p[6].X = 2;
                _p[6].Y = Height - 4;
                _p[7].X = 0;
                _p[7].Y = Height - 6;
            }
            else
            {
                _p[0].X = 0;
                _p[0].Y = 2;
                _p[1].X = 2;
                _p[1].Y = 0;
                _p[2].X = Width - 3;
                _p[2].Y = 0;
                _p[3].X = Width - 1;
                _p[3].Y = 2;
                _p[4].X = Width - 1;
                _p[4].Y = Height - 3;
                _p[5].X = Width - 3;
                _p[5].Y = Height - 1;
                _p[6].X = 2;
                _p[6].Y = Height - 1;
                _p[7].X = 0;
                _p[7].Y = Height - 3;
            }
            this.Invalidate();
        }
        #endregion

        #region "父容器改变"
        /// <summary>
        /// 在容器改变时改变背景色
        /// </summary>
        /// <param name="e"></param>
        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            this.BackColor = Parent.BackColor;
        }
        #endregion

        #region "绘画"
        /// <summary>
		/// 重载OnPaint绘制按钮
		/// </summary>
		/// <param name="e"></param>
		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
			Rectangle imgrect; 
			Brush fillbrush;
            Pen linepen;

			e.Graphics.Clear(_backgroundcolor);

            if (this.Enabled) //根据不同状态改变背景和边框颜色
            {
                //有效状态
                if (!bPushed)
                {
                    //一般状态
                    fillbrush = new SolidBrush(_fillcolor);
                    linepen = new Pen(_linecolor);
                    //画阴影
                    if (_showshadow)
                    {
                        using (Pen shadowpen1 = new Pen(_shadowcolor),
                            shadowpen2 = new Pen(Color.FromArgb((int)((_shadowcolor.R + _backgroundcolor.R) * 0.5), (int)((_shadowcolor.G + _backgroundcolor.G) * 0.5), (int)((_shadowcolor.B + _backgroundcolor.B) * 0.5))))
                        {
                            e.Graphics.DrawLine(shadowpen1, Width - 3, 4, Width - 3, Height - 5);
                            e.Graphics.DrawLine(shadowpen2, Width - 2, 5, Width - 2, Height - 5);
                            e.Graphics.DrawLine(shadowpen1, Width - 3, Height - 6, Width - 6, Height - 3);
                            e.Graphics.DrawLine(shadowpen1, Width - 4, Height - 4, Width - 4, Height - 5);
                            e.Graphics.DrawLine(shadowpen1, 4, Height - 3, Width - 5, Height - 3);
                            e.Graphics.DrawLine(shadowpen2, 5, Height - 2, Width - 5, Height - 2);
                            e.Graphics.DrawLine(shadowpen2, Width - 5, Height - 2, Width - 2, Height - 5);
                        }
                    }
                }
                else
                {
                    //按下状态
                    fillbrush = new SolidBrush(Color.FromArgb((int)(_fillcolor.R * 0.9), (int)(_fillcolor.G * 0.9), (int)(_fillcolor.B * 0.9)));
                    linepen = new Pen(Color.FromArgb((int)(_linecolor.R * 0.9), (int)(_linecolor.G * 0.9), (int)(_linecolor.B * 0.9)));
                }
            }
            else
            {
                //禁止状态
                fillbrush = new SolidBrush(Color.FromArgb((int)((_fillcolor.R + _backgroundcolor.R) * 0.2), (int)((_fillcolor.G + _backgroundcolor.G) * 0.2), (int)((_fillcolor.B + _backgroundcolor.B) * 0.2)));
                linepen = new Pen(Color.FromArgb((int)((_linecolor.R + _backgroundcolor.R) * 0.2), (int)((_linecolor.G + _backgroundcolor.G) * 0.2), (int)((_linecolor.B + _backgroundcolor.B) * 0.2)));
            }

			//填充背景色
			e.Graphics.FillPolygon(fillbrush, _p);
            //画边框
            e.Graphics.DrawPolygon(linepen, _p);
            //画3d效果
            if (Enabled && bPushed)
            {
                using (Pen whitepen = new Pen(Color.FromArgb((int)((_linecolor.R + Color.White.R) * 0.5), (int)((_linecolor.G + Color.White.G) * 0.5), (int)((_linecolor.B + Color.White.B) * 0.5))),
                    blackpen = new Pen(Color.FromArgb((int)((_linecolor.R + Color.Black.R) * 0.5), (int)((_linecolor.G + Color.Black.G) * 0.5), (int)((_linecolor.B + Color.Black.B) * 0.5))))
                {
                    e.Graphics.DrawLine(blackpen, _p[0].X + 1, _p[0].Y, _p[0].X + 1, _p[7].Y);
                    e.Graphics.DrawLine(blackpen, _p[0].X + 1, _p[0].Y, _p[1].X, _p[1].Y + 1);
                    e.Graphics.DrawLine(blackpen, _p[1].X, _p[1].Y + 1, _p[2].X, _p[2].Y + 1);
                    e.Graphics.DrawLine(blackpen, _p[1].X, _p[1].Y + 1, _p[1].X, _p[1].Y + 2);
                    e.Graphics.DrawLine(whitepen, _p[3].X - 1, _p[3].Y, _p[4].X - 1, _p[4].Y);
                    e.Graphics.DrawLine(whitepen, _p[4].X - 1, _p[4].Y, _p[5].X, _p[5].Y - 1);
                    e.Graphics.DrawLine(whitepen, _p[5].X, _p[5].Y - 1, _p[6].X, _p[6].Y - 1);
                    e.Graphics.DrawLine(whitepen, _p[5].X, _p[5].Y - 1, _p[5].X, _p[5].Y - 2);
                }
            }

			if (_image != null)
			{
                //根据图像定位方式来绘画
                switch (_sizemode)
                {
                    case PictureBoxSizeMode.CenterImage://居中
                        if (_showshadow)
                        {
                            imgrect = new Rectangle(2 + (Width - 7 - _image.Width) / 2, 2 + (Height - 7 - _image.Height) / 2, _image.Width, _image.Height);
                        }
                        else
                        {
                            imgrect = new Rectangle(2 + (Width - 4 - _image.Width) / 2, 2 + (Height - 4 - _image.Height) / 2, _image.Width, _image.Height);
                        }
                        break;
                    case PictureBoxSizeMode.Normal://左上角
                        imgrect = new Rectangle(2, 2, _image.Width, _image.Height);
                        break;
                    case PictureBoxSizeMode.StretchImage://拉伸
                        if (_showshadow)
                        {
                            imgrect = new Rectangle(2, 2, Width - 7, Height - 7);
                        }
                        else
                        {
                            imgrect = new Rectangle(2, 2, Width - 4, Height - 4);
                        }
                        break;
                    default:
                        if (_showshadow)
                        {
                            imgrect = new Rectangle(2, 2, Width - 7, Height - 7);
                        }
                        else
                        {
                            imgrect = new Rectangle(2, 2, Width - 4, Height - 4);
                        }
                        break;
                }

				//设置透明色
				ImageAttributes imageAttr = new ImageAttributes();
				imageAttr.SetColorKey(BackgroundImageColor(_image), BackgroundImageColor(_image));
				//在内存中画图
				e.Graphics.DrawImage(_image, imgrect, 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel, imageAttr); 

			}
			//把内存中的图像画到按钮上
			base.OnPaint(e);
            fillbrush.Dispose();
            linepen.Dispose();
        }
        #endregion

        #region "鼠标按下"
        /// <summary>
		/// 在鼠标按下时改变重绘按钮,使其看上去凹下
		/// </summary>
		/// <param name="e"></param>
		protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e )
		{
			bPushed = true;
			this.Invalidate();
            base.OnMouseDown(e);
        }
        #endregion

        #region "鼠标放开"
        /// <summary>
		/// 在鼠标放开时改变重绘按钮,使其恢复原样
		/// </summary>
		/// <param name="e"></param>
		protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
		{
			bPushed = false;
			this.Invalidate();
            if ((e.X >= 0) && (e.X <= Width) && (e.Y >= 0) && (e.Y <= Height))
            {
                base.OnMouseUp(e);
            }
        }
        #endregion

        #region "按钮被禁用"
        /// <summary>
		/// 按钮被禁用时重绘按钮,让其变成平面样式
		/// </summary>
		/// <param name="e"></param>
		protected override void OnEnabledChanged(System.EventArgs e)
		{
			this.Invalidate();
            base.OnEnabledChanged(e);
        }
        #endregion

        #region "获取图像背景色"
        /// <summary>
        /// 获取图像背景色
        /// </summary>
        /// <param name="image">要获取背景色的图像</param>
        /// <returns></returns>
        private Color BackgroundImageColor(Image image)
        {
            Bitmap bmp = new Bitmap(image);
            Color c = bmp.GetPixel(_x, _y);
            bmp.Dispose();
            return c;
        }
        #endregion

        #region "释放资源"
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            if ((_image != null) && disposing)
            {
                _image.Dispose();
            }
            base.Dispose(disposing);
        }
        #endregion

        #endregion

        #region "公共方法"

        #region "显示提示条"
        /// <summary>
        /// 显示提示条
        /// </summary>
        public void ShowTip()
        {
            if (_screen == null)
            {
                return;
            }
            Point point = new Point(0, 0);
            point = this.PointToScreen(point);
            int left = point.X;
            int top = point.Y;
            //获取文字的高度和宽度
            int height = _screen.MeasureString(Text, Font).ToSize().Height;
            int width = _screen.MeasureString(Text, Font).ToSize().Width;
            if ((height == 0) || (width == 0))
            {
                return;
            }
            //显示
            using (Brush b1 = new SolidBrush(Color.Khaki), b2 = new SolidBrush(Color.Black))
            {
                using (Pen p = new Pen(Color.Black))
                {
                    switch (_tipanchor)
                    {
                        case TipAnchorStyles.Top:
                            _screen.FillRectangle(b1, left + (this.Width - width - 2) / 2, top - height - 4, width + 2, height + 2);
                            _screen.DrawRectangle(p, left + (this.Width - width - 2) / 2, top - height - 4, width + 2, height + 2);
                            _screen.DrawString(Text, Font, b2, left + (this.Width - width) / 2, top - height - 2);
                            break;
                        case TipAnchorStyles.Left:
                            _screen.FillRectangle(b1, left - width - 4, top + (this.Height - height - 2) / 2, width + 2, height + 2);
                            _screen.DrawRectangle(p, left - width - 4, top + (this.Height - height - 2) / 2, width + 2, height + 2);
                            _screen.DrawString(Text, Font, b2, left - width - 3, top + (this.Height - height) / 2);
                            break;
                        case TipAnchorStyles.Bottom:
                            _screen.FillRectangle(b1, left + (this.Width - width - 2) / 2, top + this.Height + 2, width + 2, height + 2);
                            _screen.DrawRectangle(p, left + (this.Width - width - 2) / 2, top + this.Height + 2, width + 2, height + 2);
                            _screen.DrawString(Text, Font, b2, left + (this.Width - width) / 2, top + this.Height + 2);
                            break;
                        case TipAnchorStyles.Right:
                            _screen.FillRectangle(b1, left + this.Width + 2, top + (this.Height - height - 2) / 2, width + 2, height + 2);
                            _screen.DrawRectangle(p, left + this.Width + 2, top + (this.Height - height - 2) / 2, width + 2, height + 2);
                            _screen.DrawString(Text, Font, b2, left + this.Width + 3, top + (this.Height - height) / 2);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        #endregion

        #endregion

        #endregion
    }

    #region "提示条定位方式枚举"
    public enum TipAnchorStyles
    {
        Top, Left, Bottom, Right
    }
    #endregion
}

⌨️ 快捷键说明

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