使用WPF在C#中制作下载按钮

本示例使用 WPF 制作一个下载按钮。以下 XAML 代码显示了程序如何构建该按钮。

cs 复制代码
<Window x:Class="howto_download_button.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="107" Width="140" Loaded="Window_Loaded"
    WindowStyle="ToolWindow">
    <Grid Background="Transparent" Name="grdMain">
        <!-- The button's background -->
        <Ellipse Margin="5" StrokeThickness="5">
            <Ellipse.Fill>
                <RadialGradientBrush>
                    <GradientStop Color="LightGreen" Offset="0"/>
                    <GradientStop Color="#FF00C000" Offset="1"/>
                </RadialGradientBrush>
            </Ellipse.Fill>
        </Ellipse>
        <!-- Text -->
        <TextBlock FontFamily="Arial Rounded MT" FontSize="20"
         FontWeight="Bold" Foreground="Black"
         Margin="10" HorizontalAlignment="Center"
         VerticalAlignment="Center"
         TextWrapping="Wrap">
            Download
        </TextBlock>
        <!-- Highlight -->
        <Ellipse VerticalAlignment="Top"
            Height="30" Margin="20,8,20,0"
            Fill="White" Opacity="0.5">
        </Ellipse>
    </Grid>
</Window>

程序窗口包含一个Grid控件,该控件包含两个Ellipse和一个TextBlock。第一个Ellipse定义按钮的背景。它填充了一个RadialGradientBrush,颜色从中心的 LightBlue渐变为边缘的深蓝色。

TextBlock显示文本"下载" 。

第二个椭圆是白色的,位于其他控件的上方。它的位置从按钮顶部稍下方开始,垂直延伸到按钮的中间,但不会覆盖按钮的整个宽度。它的不透明度为 0.5,因此不透明度为 50%,其他控件会透过它显示出来。

考虑到按钮只使用了三个控件,结果相当不错。(当然,需要花很多功夫才能使结果与 Twitter 按钮相当匹配。)

启动时,程序使用以下代码将Grid控件的图像保存到 png 文件中。(这是用于保存控件图像的典型代码。)

cs 复制代码
// Save an image of the grid.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // Render the grid.
    RenderTargetBitmap bm = new RenderTargetBitmap(
        (int)grdMain.ActualWidth, (int)grdMain.ActualHeight,
        96, 96, PixelFormats.Default);
    bm.Render(grdMain);

    // Save the result into a file.
    using (var fileStream = new FileStream("Download.png",
        FileMode.Create))
    {
        BitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bm));
        encoder.Save(fileStream);
    }
}

程序创建一个与Grid大小相同的RenderTargetBitmap,使用 96 像素/英寸分辨率和默认像素格式。然后它调用Grid的Render方法,使其将自身(及其内容)绘制到RenderTargetBitmap上。

接下来,程序创建一个FileStream,表示应包含图像的文件。它创建一个PngBitmapEncoder以 png 格式保存图像。

然后,程序调用编码器的Frames.Add方法向其添加新帧。它将由类的Create方法生成的新BitmapFrame传递给该方法,并将RenderTargetBitmap传递给该方法。

最后,在创建新帧之后,程序调用编码器的Save方法。

(如果这一切看起来令人烦恼,我同意。这是我对 WPF 最大的抱怨之一。尽管 .NET 程序员多年来一直在处理图像并将它们保存到文件中,但 WPF 使这变得更加困难。就好像 WPF 设计师以前从未保存过图像一样。事实上,几乎所有事情都变得更加困难。这让我怀疑设计 WPF 的开发人员是否曾经用 .NET 编程过。)

相关推荐
Pniubi2 小时前
Java为什么要分堆和栈
java·开发语言
小小晓.3 小时前
Qt 对话框、QLineEdit 与布局管理实战详解
开发语言·qt
炸薯条!3 小时前
博客系统测试报告
开发语言·lua
Evand J4 小时前
【MATLAB例程12】永磁同步电机基于增广无迹卡尔曼滤波的未知负载转矩在线观测
开发语言·matlab
1-1=04 小时前
C# 特性(Attribute)
c#·.net·.netcore
时间的拾荒人5 小时前
CMake 实战指南:从基础语法到静态库/动态库制作与链接
开发语言·c++·面试
xyouyuweizhu5 小时前
Java InetAddress 详解:从 IP 地址表示到主机名解析
java·开发语言·python
布莱克60514 小时前
C/C++ 中变量的声明与定义:区别、联系与常见误区
c语言·开发语言·c++·声明·定义
LabVIEW开发14 小时前
LabVIEW 局部变量为何引发竞争条件?四种同步机制
开发语言·labview
benchmark_cc15 小时前
A 股、港股、美股如何建立统一的标的主数据格式?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash