none
WPF 图像剪切处理 RRS feed

  • 问题

  • 我有一张集成了关闭按钮的三种状态的图片,我想在按钮的三个时期(鼠标移到上面,点击,离开)显示这个图片不同的部分,应该怎么做?

    是一张图片中集合了三种状态。

    2011年6月7日 5:01

答案

  • 直接看我的例子:

        <Button Content="Button">
          <Button.Style>
            <Style TargetType="{x:Type Button}">
              <Setter Property="Background">
                <Setter.Value>
                  <DrawingBrush Stretch="Fill" TileMode="Tile" Viewport="0,0,3,1" Viewbox="0,0,1,1">
                    <DrawingBrush.Drawing>
                      <ImageDrawing ImageSource="status.png" Rect="0,0,1,1"/>
                    </DrawingBrush.Drawing>
                  </DrawingBrush>
                </Setter.Value>
              </Setter>
              <Setter Property="Template">
                <Setter.Value>
                  <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Background="{TemplateBinding Background}">
                      <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </Grid>
                  </ControlTemplate>
                </Setter.Value>
              </Setter>
              <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                  <Setter Property="Background">
                    <Setter.Value>
                      <DrawingBrush Stretch="Fill" TileMode="Tile" Viewport="2,0,3,1" Viewbox="0,0,1,1">
                        <DrawingBrush.Drawing>
                          <ImageDrawing ImageSource="status.png" Rect="0,0,1,1"/>
                        </DrawingBrush.Drawing>
                      </DrawingBrush>
                    </Setter.Value>
                  </Setter>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                  <Setter Property="Background">
                    <Setter.Value>
                      <DrawingBrush Stretch="Fill" TileMode="Tile" Viewport="1,0,3,1" Viewbox="0,0,1,1">
                        <DrawingBrush.Drawing>
                          <ImageDrawing ImageSource="status.png" Rect="0,0,1,1"/>
                        </DrawingBrush.Drawing>
                      </DrawingBrush>
                    </Setter.Value>
                  </Setter>
                </Trigger>
              </Style.Triggers>
            </Style>
          </Button.Style>
        </Button>

     

    下载:http://cid-51b2fdd068799d15.office.live.com/self.aspx/.Public/Samples%5E_2011/20110607%5E_ThreeStatusImageInButton.zip

     

    Sincerely,

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 Eilot 2011年6月7日 10:19
    2011年6月7日 9:51
    版主

全部回复

  • 直接看我的例子:

        <Button Content="Button">
          <Button.Style>
            <Style TargetType="{x:Type Button}">
              <Setter Property="Background">
                <Setter.Value>
                  <DrawingBrush Stretch="Fill" TileMode="Tile" Viewport="0,0,3,1" Viewbox="0,0,1,1">
                    <DrawingBrush.Drawing>
                      <ImageDrawing ImageSource="status.png" Rect="0,0,1,1"/>
                    </DrawingBrush.Drawing>
                  </DrawingBrush>
                </Setter.Value>
              </Setter>
              <Setter Property="Template">
                <Setter.Value>
                  <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Background="{TemplateBinding Background}">
                      <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </Grid>
                  </ControlTemplate>
                </Setter.Value>
              </Setter>
              <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                  <Setter Property="Background">
                    <Setter.Value>
                      <DrawingBrush Stretch="Fill" TileMode="Tile" Viewport="2,0,3,1" Viewbox="0,0,1,1">
                        <DrawingBrush.Drawing>
                          <ImageDrawing ImageSource="status.png" Rect="0,0,1,1"/>
                        </DrawingBrush.Drawing>
                      </DrawingBrush>
                    </Setter.Value>
                  </Setter>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                  <Setter Property="Background">
                    <Setter.Value>
                      <DrawingBrush Stretch="Fill" TileMode="Tile" Viewport="1,0,3,1" Viewbox="0,0,1,1">
                        <DrawingBrush.Drawing>
                          <ImageDrawing ImageSource="status.png" Rect="0,0,1,1"/>
                        </DrawingBrush.Drawing>
                      </DrawingBrush>
                    </Setter.Value>
                  </Setter>
                </Trigger>
              </Style.Triggers>
            </Style>
          </Button.Style>
        </Button>

     

    下载:http://cid-51b2fdd068799d15.office.live.com/self.aspx/.Public/Samples%5E_2011/20110607%5E_ThreeStatusImageInButton.zip

     

    Sincerely,

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 Eilot 2011年6月7日 10:19
    2011年6月7日 9:51
    版主
  • 谢谢。。我还有写标签没有看懂,我找本书好好看看去。。谢啦
    2011年6月7日 10:20