日期:2014-05-18  浏览次数:20739 次

请教一下UserControl的OnPaint重写的问题
我的UserControl,问题在OnPaint事件,不解。
C# code

namespace 启动系统服务
{
    [ToolboxBitmap(typeof(PanelAlphaEx))]
    public partial class PanelAlphaEx : UserControl
    {
        public PanelAlphaEx()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.Opaque, true);
            this.CreateControl();
            this.SetStyle(ControlStyles.ResizeRedraw, false);
        }
        #region 透明配置

        // 属性设置
        private bool _transparentBG = true;
        private int _alpha = 125;
        [Category("透明层设置"), Description("是否使用透明,默认为True")]
        public bool TransparentBG { get { return _transparentBG; } set { _transparentBG = value; } }
        [Category("透明层设置"), Description("设置透明度")]
        public int Alpha { get { return _alpha; } set { _alpha = value; } }

        #endregion

        /// <summary>
        /// 开启 WS_EX_TRANSPARENT,使控件支持透明
        /// </summary>
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                //cp.ExStyle |= 0x20;
                cp.ExStyle |= 0x00000020;
                return cp;
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Color drawColor = _transparentBG ? Color.FromArgb(this._alpha, this.BackColor) : this.BackColor;
            Pen penBorder = new Pen(drawColor, 0);
            SolidBrush brushBgColor = new SolidBrush(drawColor);
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
            e.Graphics.DrawRectangle(penBorder, e.ClipRectangle);
            e.Graphics.FillRectangle(brushBgColor, e.ClipRectangle);
            base.OnPaint(e);
        }
    }
}


最后这个重写很头疼,功能吧算是可以实现,
就是一隐藏一显示就要重写一次,在VS里失去焦点,得到焦点也要重写一次
窗口还是可以最大化的,最大化以后显示这个userControl时更缓慢了。
然后显示的时候是从上到下逐渐显示,有没有什么办法让他快速而且一下就显示出来的呢?
目前的效果图:


------解决方案--------------------
居然没人回答,我自己回答:我绘制的时候,不填充矩形,不用颜色,反正就是起了遮挡的遮罩层而已,看不见我加个汉字,加个进度条。