代码之家  ›  专栏  ›  技术社区  ›  jonathanpeppers

c,wpf-视觉刷和猫眼石

  •  2
  • jonathanpeppers  · 技术社区  · 15 年前

    我需要创建一个用户控件,该控件的一部分背景是透明的。透明部分被切割成一个圆角半径为2的边界形状——这是因为设计需要的。

    以下是我的不起作用的代码:

        <UserControl Margin="1" x:Name="Box">
            <UserControl.Resources>
                <Style TargetType="UserControl">
                    <Setter Property="Height" Value="16" />
                </Style>
            </UserControl.Resources>
            <Grid>
                <Border CornerRadius="2" BorderThickness="0">
                    <Border.Background>
                        <SolidColorBrush Color="Black" Opacity=".3" />
                    </Border.Background>
                    <Border.OpacityMask>
                        <VisualBrush>
                            <VisualBrush.Visual>
                                <Grid 
                                    Background="Black" 
                                    Width="{Binding ElementName=Box, Path=ActualWidth}"
                                    Height="{Binding ElementName=Box, Path=ActualHeight}">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition />
                                    </Grid.ColumnDefinitions>
                                    <Border Grid.Column="1" Margin="1" CornerRadius="2" Background="Transparent" BorderThickness="0" />
                                </Grid>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Border.OpacityMask>
                </Border>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
    
                    <TextBlock 
                        VerticalAlignment="Center" TextAlignment="Right" FontSize="10" Margin="2"
                        Foreground="White" Text="Property" />
    
                    <TextBlock 
                        Grid.Column="1" VerticalAlignment="Center" TextAlignment="Center" FontSize="10" Margin="2"
                        Text="Value" />
                </Grid>
            </Grid>
        </UserControl>
    

    我做了一些修改,所以你们可以直接把它放到xamlpad中。

    出于某种原因,我设置到边界的OpacityMask的VisualBrush根本不起作用。OpacityMask只是显示所有完全可见的内容。为了进行测试,我把一个快速的lineargradientbrush放进去,它工作正常。

    使用VisualBrush和OpacityMask是否有问题?这里出什么事了?

    以下是我试图实现的目标的截图:

    ScreenShot http://monitor.utopiaselfscan.com/Screen.png

    用户控件是表示实体编号、进度、小时数等的标题。它们是黑色的,透明度为30%,具有圆角矩形不透明度遮罩剪切。我通常使用图像来渲染这样的东西,我们的图形艺术家会对玻璃效果疯狂。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Anvaka    15 年前

    谁是 Box 在代码中?你还可以添加一个你想要实现的形象吗?

    你有没有尝试过获得你想要的东西的途径?例如,以下路径:

    <Path Stroke="Black" Stretch="Fill" StrokeThickness="1" Fill="#CCCCFF">
        <Path.Data>
         <GeometryGroup FillRule="EvenOdd" >
          <EllipseGeometry Center="40,70" RadiusX="30" RadiusY="30" />              
          <RectangleGeometry Rect="30,55 100 30" />
        </GeometryGroup>
      </Path.Data>
    </Path>
    

    给你这个剪影: alt text http://img704.imageshack.us/img704/928/cutw.jpg

    编辑: 以下是实现您的设计的一种方法:

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
       <Page.Resources>
          <SolidColorBrush x:Key="FillBrush" Color="Gray" Opacity="0.3"/>
       </Page.Resources>
       <Grid>
       <!-- Set background image here, instead of border-->
          <Border>
             <Border.Background>
                <LinearGradientBrush>
                   <GradientStop Color="#FFcacaca"/>
                   <GradientStop Offset="1" Color="#FF353535"/>
                </LinearGradientBrush>
             </Border.Background>
          </Border>
       <!-- Content goes here -->
          <Grid>
             <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
             </Grid.ColumnDefinitions>
             <Border
                MinHeight="24"
                MinWidth="100"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Background="{StaticResource FillBrush}"
                CornerRadius="15, 0, 0, 15"
                Padding="0, 0, 5, 0">
                <TextBlock
                   HorizontalAlignment="Right"
                   VerticalAlignment="Center"
                   Foreground="White"
                   Text="From"/>
             </Border>
             <Border
                MinHeight="24"
                MinWidth="100"
                Grid.Column="1"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                BorderBrush="{StaticResource FillBrush}"
                BorderThickness="1"
                CornerRadius="0, 15, 15, 0">
                <TextBox
                   Margin="5, 0, 5, 0"
                   VerticalAlignment="Center"
                   Background="Transparent"
                   BorderThickness="0"
                   Foreground="#2f3740"
                   Text="Verylongname, Johnathan"/>
             </Border>
          </Grid>
       </Grid>
    </Page>
    

    主要是使用两个边框和一个画笔。对于标题字段,绘制边框的背景;对于内容字段,绘制边框的边框:)。

    干杯,Anvaka。

        2
  •  0
  •   Community CDub    7 年前

    我正在使用创建的路径来创建这个不透明度蒙版。

    你可以在这篇文章中找到我用作遮罩的对象:

    Odd-Shape Geometry

    推荐文章