zoukankan      html  css  js  c++  java
  • wpf单容器中的Chrome

    Chrome有点类似于用GDI+画图一般,除了模板,也可以使用DrawingContext对象来画图.

    WPF内置的很多控件都是如此实现的,虽然麻烦了一些,可能是为了提升性能考虑.

    其方式与定义样式模板差不多,只不过换成代码形式了.

    1.先定义控件不同状态下的颜色,

    private static SolidColorBrush CommonDisabledBackgroundOverlay {
        get {
            if (commonDisabledBackgroundOverlay == null) {
                lock (resourceAccess) {
                    if (commonDisabledBackgroundOverlay == null) {
                        SolidColorBrush brush = new SolidColorBrush(Color.FromRgb(0xf4, 0xf4, 0xf4));
                        brush.Freeze();
                        commonDisabledBackgroundOverlay = brush;
                    }
                }
            }
            return commonDisabledBackgroundOverlay;
        }
    }


    2.定义控件样式状态

    private Brush BackgroundOverlay {
        get {
            if (!base.IsEnabled) {
                return CommonDisabledBackgroundOverlay;
            }
            if (!this.Animates) {
                if (this.RenderPressed) {
                    return CommonPressedBackgroundOverlay;
                }
                if (this.RenderMouseOver) {
                    return CommonHoverBackgroundOverlay;
                }
                return null;
            }
    return null; } }

    3.在OnRender方法中呈现样式

    private void DrawBackground(DrawingContext dc, ref Rect bounds)
    {
        if (base.IsEnabled || Corners.None != this.RoundCorners)
        {
            Brush background = this.Background;
            if (background == null) background = this.BackgroundOverlay;
            if ((bounds.Width > 4.0) && (bounds.Height > 4.0))
            {
                Rect rectangle = new Rect(bounds.Left + 1.0, bounds.Top + 1.0, bounds.Width - 2.0, bounds.Height - 2.0);
                if (background != null)
                {
                    if (!this.RenderTransparentBackground || this.RenderDefaulted || this.RenderMouseOver || this.RenderPressed)
                        dc.DrawRectangle(background, null, rectangle);
                    else
                        dc.DrawRectangle(Brushes.Transparent, null, rectangle);
                }
            }
        }
    }

    现在为止一个背景色就设置好了,从这里回头看Border容器,也是采用用样的做法,WPF内置的Shape也是如此.

    之前感觉比较困惑,Shape的呈现可以理解,Button的呈现还无法理解.
    内置的Button使用了ButtonChrome,CheckBox使用了BulletChrome.估计其他控件也是如此.所以所有的控件还是一样,都是一点一滴画出来的.

  • 相关阅读:
    springboot实现redis的分布式锁
    剑指offer--二维数组中查找
    剑指offer--二维数组中查找
    对JDK动态代理的模拟实现
    Spring(4)AOP
    设计模式之单例模式(Java)
    【Java并发系列】--Java内存模型
    maven 解决jar包冲突及简单使用
    基于注解的SpringAOP源码解析(三)
    Java代码中可以优化性能的小细节
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1706204.html
Copyright © 2011-2022 走看看