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.估计其他控件也是如此.所以所有的控件还是一样,都是一点一滴画出来的.

  • 相关阅读:
    QT 中如何实现一个简单的动画
    qt 中画线时如何设置笔的颜色和填充
    QT自定义窗口
    qt 中创建一个工作线程(例子)
    QT 获取系统时间
    火狐浏览器 system error code 1722 rpc服务器不可用和谷歌浏览器的插件application/x-print-ladop不支持
    ORA-10858:在要求输入数字处找到非数字字符
    eaeyui-combobox实现组合查询(即实现多个值得搜索)
    Mybatis中的模糊查询
    如何设置像我这样的博客的样式。
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1706204.html
Copyright © 2011-2022 走看看