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

相关推荐
安小牛1 分钟前
Kotlin 学习-集合
android·开发语言·学习·kotlin
Peter_chq7 分钟前
selenium快速入门
linux·开发语言·chrome·python·selenium
双叶83612 分钟前
(51单片机)串口通讯(串口通讯教程)(串口接收发送教程)
c语言·开发语言·c++·单片机·嵌入式硬件·microsoft·51单片机
_x_w1 小时前
【12】数据结构之基于线性表的排序算法
开发语言·数据结构·笔记·python·算法·链表·排序算法
不爱学英文的码字机器1 小时前
Rust 的征服:从系统编程到全栈开发的 IT 新宠
开发语言·后端·rust
q567315232 小时前
用Dispatch库的爬虫程序爬取图片网站
开发语言·爬虫·python·scrapy
knightkkzboy2 小时前
《C语言中的“魔法盒子”:自定义函数的奇妙之旅》
c语言·开发语言·函数
Jelena技术达人2 小时前
深入解析:Python 爬取淘宝商品上下架接口
开发语言·python
菠萝崽.2 小时前
springboot中测试python脚本:ProcessBuilder
java·开发语言·spring boot·python·processbuilder
仙人掌_lz2 小时前
详解如何从零用 Python复现类似 GPT-4o 的多模态模型
开发语言·python·gpt·llm·gpt-4o·deepseek