使用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 编程过。)

相关推荐
苦夏木禾12 分钟前
js请求避免缓存的三种方式
开发语言·javascript·缓存
超级土豆粉20 分钟前
Turndown.js: 优雅地将 HTML 转换为 Markdown
开发语言·javascript·html
金增辉1 小时前
基于C#的OPCServer应用开发,引用WtOPCSvr.dll
c#
wei_shuo1 小时前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
熊猫钓鱼>_>1 小时前
用Python解锁图像处理之力:从基础到智能应用的深度探索
开发语言·图像处理·python
GO兔2 小时前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
好开心啊没烦恼2 小时前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas
future14123 小时前
C#学习日记
开发语言·学习·c#
king_harry3 小时前
Java程序-OceanBase Connector/J 示例
开发语言
傻啦嘿哟4 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#