使用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 小时前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
quant_19863 小时前
R语言如何接入实时行情接口
开发语言·经验分享·笔记·python·websocket·金融·r语言
百锦再7 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
风吹落叶花飘荡7 小时前
2025 Next.js项目提前编译并在服务器
服务器·开发语言·javascript
程序猿多布8 小时前
C# 值拷贝、引用拷贝、浅拷贝、深拷贝
c#
失败又激情的man8 小时前
python之requests库解析
开发语言·爬虫·python
专注VB编程开发20年8 小时前
常见 HTTP 方法的成功状态码200,204,202,201
开发语言·网络协议·tcp/ip·http
有没有没有重复的名字8 小时前
线程安全的单例模式与读者写者问题
java·开发语言·单例模式
阿蒙Amon9 小时前
C#随机数生成全面详解:从基础到高级应用
服务器·网络·c#