WPF创建进度条

使用wpf做一个原生的进度条,进度条上面有值,先看效果。

功能就是点击按钮,后台处理数据,前台显示处理数据的变化,当然还可以对进度条进行美化和关闭的操作,等待后台处理完毕数据,然后自动关闭。

1.首先简简单单创建一个项目

2.先建立进度条的页面DialogWait.xaml

<Window x:Class="WpfApp5.DialogWait"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp5"
        mc:Ignorable="d"
        WindowStyle="None" AllowsTransparency="True"  Background="Transparent" ShowInTaskbar="False" ResizeMode="NoResize">
    <ProgressBar x:Name="progressBar" Maximum="100"   Height="25" Width="300" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
</Window>

3.DialogWait.xaml.cs后台代码如下

关键点就是要对max的值进行判断,如果大于100和小于100的话,显示是不一样的,主要是因为进度条的值是100,要相对的扩大或者缩小,那么界面上显示的数据变化就是一样的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp5
{
    /// <summary>
    /// DialogWait.xaml 的交互逻辑
    /// </summary>
    public partial class DialogWait : Window
    {
        /// <summary>
        /// 进度条最大值
        /// </summary>
        private int max = 0;
        /// <summary>
        /// 大于100的增量
        /// </summary>
        private int increment = 0;
        /// <summary>
        /// 小于100的增量
        /// </summary>
        private double incrementLess = 0;
        public DialogWait()
        {
            InitializeComponent();
        }
        public DialogWait(int max)
        {
            InitializeComponent();
            this.Width = 300;
            this.Height = 25;
            this.Owner = Application.Current.MainWindow;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            this.max = max;
            if (max >= 100)
            {
                increment = max / 100;
            }
            else
            {
                incrementLess = Math.Round(100.0 / max, 3);
            }
        }
        public void ShowWait(int value)
        {
            progressBar.Dispatcher.Invoke(() =>
            {
                if (max >= 100)
                {
                    progressBar.Value = value / increment;
                }
                else
                {
                    progressBar.Value = Math.Round(value * incrementLess, 0);
                }
            });
        }
    }
}

4.MainWindow.xaml.cs的代码

其中最重要的就是Task

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp5
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DialogWait dialogWaitPercent = new DialogWait(500);
            Task.Run(() =>
            {
                A(dialogWaitPercent);//处理数据
            });
            dialogWaitPercent.ShowDialog();
        }
        public void A(DialogWait dialogWaitPercent)
        {
            for (int i = 0; i < 500; i++)
            {
                dialogWaitPercent.ShowWait(i);
                Thread.Sleep(10);
            }
            dialogWaitPercent.Dispatcher.Invoke(() =>
            {
                dialogWaitPercent.Close();
            });
        }
    }
}

5.ProgressBarStyle.xaml,最后就是对进度条的美化样式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type ProgressBar}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Height" Value="15"/>
        <Setter Property="Background" Value="#6fae5f"/>
        <Setter Property="FontSize" Value="10"/>
        <Setter Property="Padding" Value="5,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ProgressBar}">
                    <Grid Background="#00000000">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Determinate"/>
                                <VisualState x:Name="Indeterminate">
                                    <Storyboard RepeatBehavior="Forever">
                                        <PointAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)">
                                            <EasingPointKeyFrame KeyTime="0:0:0" Value="0.5,0.5"/>
                                            <EasingPointKeyFrame KeyTime="0:0:1.5" Value="1.95,0.5"/>
                                            <EasingPointKeyFrame KeyTime="0:0:3" Value="0.5,0.5"/>
                                        </PointAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <Grid Height="{TemplateBinding Height}">
                            <Border Background="#000000" CornerRadius="7.5" Opacity="0.05"/>
                            <Border BorderBrush="#000000" BorderThickness="1" CornerRadius="7.5" Opacity="0.1"/>
                            <Grid Margin="{TemplateBinding BorderThickness}">
                                <Border x:Name="PART_Track"/>
                                <Grid x:Name="PART_Indicator" ClipToBounds="True" HorizontalAlignment="Left" >
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition x:Name="width1"/>
                                        <ColumnDefinition x:Name="width2" Width="0"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid x:Name="Animation"  RenderTransformOrigin="0.5,0.5">
                                        <Grid.RenderTransform>
                                            <TransformGroup>
                                                <ScaleTransform ScaleY="-1" ScaleX="1"/>
                                                <SkewTransform AngleY="0" AngleX="0"/>
                                                <RotateTransform Angle="180"/>
                                                <TranslateTransform/>
                                            </TransformGroup>
                                        </Grid.RenderTransform>
                                        <Border Background="{TemplateBinding Background}" CornerRadius="7.5">
                                            <Viewbox HorizontalAlignment="Left" StretchDirection="DownOnly" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                                                <TextBlock Foreground="#ffffff" SnapsToDevicePixels="True" FontSize="{TemplateBinding FontSize}" VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Value,StringFormat={}{0}%}" RenderTransformOrigin="0.5,0.5">
                                                    <TextBlock.RenderTransform>
                                                        <TransformGroup>
                                                            <ScaleTransform ScaleY="1" ScaleX="-1"/>
                                                            <SkewTransform AngleY="0" AngleX="0"/>
                                                            <RotateTransform Angle="0"/>
                                                            <TranslateTransform/>
                                                        </TransformGroup>
                                                    </TextBlock.RenderTransform>
                                                </TextBlock>
                                            </Viewbox>
                                        </Border>
                                        <Border BorderBrush="#000000" BorderThickness="1" CornerRadius="7.5" Opacity="0.1"/>
                                    </Grid>
                                </Grid>
                            </Grid>
                        </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="#c5c5c5"/>
                        </Trigger>
                        <Trigger Property="IsIndeterminate" Value="true">
                            <Setter TargetName="width1" Property="Width" Value="0.25*"/>
                            <Setter TargetName="width2" Property="Width" Value="0.725*"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

本案例代码:https://download.csdn.net/download/u012563853/88578158

来源:WPF创建进度条-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/197689.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

设计师福利!2024在线图标设计网站推荐,不容错过的宝藏!

在当今竞争激烈的商业环境中&#xff0c;公司或个人品牌的视觉识别元素已经成为区分你和竞争对手的关键因素之一。一个独特而引人注目的标志可以深深扎根于人们的心中&#xff0c;并在消费者心中建立一个强烈的品牌印象。如果你正在寻找合适的工具来创建或改进你的标志&#xf…

js数组map()的用法

JavaScript Array map() 方法 先说说这个方法浏览器的支持&#xff1a; 支持五大主流的浏览器&#xff0c; 特别注意&#xff1a;IE 9 以下的浏览器不支持&#xff0c;只支持IE 9以上的版本的浏览器 特别注意&#xff1a;IE 9 以下的浏览器不支持&#xff0c;只支持IE 9以上的…

基于mvc电影院售票预订选座系统php+vue+elementui

本影院售票系统主要包括二大功能模块&#xff0c;管理员功能模块和用户功能模块。 &#xff08;1&#xff09;管理员模块&#xff1a;系统中的核心用户管理员登录后&#xff0c;通过管理员功能来管理后台系统。主要功能有&#xff1a;首页、个人中心、电影类型管理、场次时间管…

Corel产品注册机Corel Products KeyGen 2023 – XFORCE解决会声会影2023试用30天

CorelDRAW注册机2023支持全系列产品_Corel Products KeyGen 2023 X-FORCE v8 CorelDRAW注册机2023支持全系列产品_Corel Products KeyGen 2023 X-FORCE v8&#xff0c;Corel产品注册机&#xff08;Corel Products KeyGen 2023 – XFORCE&#xff09;&#xff0c;支持Corel旗下所…

jQuery_07 函数的使用

在jQuery中&#xff0c;如何使用函数呢&#xff1f; 1.基本函数 函数(常用的) 其实有很多函数&#xff0c;但是我们只需要掌握常用的函数即可 1.val 操作dom对象的value val() 没有参数 获取dom数组中第一个dom对象的value值 val(value) 有参数 设置dom数组中所有dom对象的…

Zotero | 取消翻译后自动添加笔记

目录 Step1&#xff1a;点击 “编辑” << “首选项” Step2&#xff1a;“翻译” << 取消勾选 “自动翻译批注” 在 Zetoro 中&#xff0c;选择颜色标记勾画的内容&#xff0c;将会自动生成一条笔记&#xff0c;如下图所示&#xff1a; 本人觉得很鸡肋&#xff0…

记录本地与服务器之间数据传输方法(上传、下载文件)

文章目录 一、使用scp命令实现参数说明示例说明 二、使用工具实现windows系统苹果系统如有启发&#xff0c;可点赞收藏哟~ 一、使用scp命令实现 scp 是 secure copy &#xff08;安全复制&#xff09;的缩写, scp 是基于 ssh 登陆进行安全的远程文件拷贝命令。相当于 cp 命令 …

渗透测试考核--两层内网 cs windows socks5

这里考核为渗透 这里是网络拓扑图 这里记录一下 两台外网 两台内网 首先拿到C段 nmap进行扫描 外网1 nmap -p 80 172.16.17.2/24 主机存活 一般都是web服务入手 所以我们指定80端口 然后去查找开放的 最后获取到2个ip Nmap scan report for 172.16.17.177 Host is u…

哈希函数:保护数据完整性的关键

&#x1f90d; 前端开发工程师&#xff08;主业&#xff09;、技术博主&#xff08;副业&#xff09;、已过CET6 &#x1f368; 阿珊和她的猫_CSDN个人主页 &#x1f560; 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 &#x1f35a; 蓝桥云课签约作者、已在蓝桥云…

WebSocket快速入门

WebSocket 借鉴&#xff1a; https://blog.csdn.net/weixin_45747080/article/details/117477006 https://cloud.tencent.com/developer/article/1887095 简介 WebSocket 是一种网络传输协议&#xff0c;可在单个 TCP 连接上进行全双工通信&#xff0c;位于 OSI 模型的应用…

Android Bitmap保存成至手机图片文件,Kotlin

Android Bitmap保存成至手机图片文件&#xff0c;Kotlin fun saveBitmap(name: String?, bm: Bitmap) {val savePath Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()if (!Files.exists(Paths.get(savePath))) {Log.d("保存文…

DHCP协议及实验omnipeek抓包工具分析 IPv4协议

一 抓包命令 adb shell tcpdump -i wlan0 -w /data/tcpdump.pcap 抓包后截图如下 二 DHCP是什么 2.1 DHCP定义 DHCP( Dynamic Host Configuration Protocol, 动态主机配置协议)定义: 存在于应用层(OSI) 前身是BOOTP(Bootstrap Protocol)协议 是一个使用UDP(User …

【Unity实战】按物品掉落率,随机掉落战利品物品系统(附项目源码)

文章目录 前言开始参考源码完结 前言 当开发游戏时&#xff0c;一个常见的需求是实现一个物品随机掉落系统。这个系统可以让玩家在击败敌人或完成任务后获得随机的物品奖励&#xff0c;增加游戏的可玩性和乐趣。 在Unity中&#xff0c;我们可以通过编写代码来实现这样的战利品…

Open Feign 源码解析(一) --- FactoryBean的妙用

什么是Open Feign? OpenFeign 是 Spring Cloud 全家桶的组件之一&#xff0c; 其核心的作用是为 Rest API 提供高效简洁的 RPC 调用方式 搭建测试项目 服务接口和实体 项目名称 cloud-feign-api 实体类 public class Order implements Serializable {private Long id;p…

windows中打开psql命令行

一、第一种方式 1.点击下方的psql&#xff0c;打开命令行窗口 2.中括号中的是默认值&#xff0c;直接回车就行 3.成功 二、第二种方式 双击安装目录中的执行文件 “D:\soft\postgresql\catalogue\scripts\runpsql.bat” 三、第三种方式 1.加到环境变量 把“D:\soft\postg…

ubuntu vmware开启3d加速画面异常

在ubuntu上开启vmware&#xff0c;进入全屏就会出现左上角和右下角两个不同的画面&#xff0c;并来回闪&#xff0c;不使用3d加速&#xff0c;一切正常&#xff0c;但是画面模糊。在ubuntu18 20 22上测试&#xff0c;vmware 15 16 17问题依旧。 原因 经过测试&#xff0c;原…

【Java】认识异常

文章目录 一、异常的概念和体系结构1.异常的概念2.异常的体系结构3.异常的分类 二、异常的处理1.防御式异常2.异常的抛出3.异常的捕捉 三、异常的处理流程四、自定义异常类 一、异常的概念和体系结构 1.异常的概念 在Java中&#xff0c;将程序执行过程中发生的不正常行为称为…

麒麟操作系统光盘救援模式

麒麟操作系统光盘救援模式 Kylin V4 桌面版&#xff1a; 启动主机后&#xff0c;插入系统光盘&#xff0c;在 BIOS 启动项里设置成从光盘启动后保存退出重启主机。 稍等片刻就会到启动菜单选项&#xff0c;到启动菜单界面后选择第一项试用银河麒麟操作系统而不安 装&#xff…

6.2 Windows驱动开发:内核枚举SSSDT表基址

在Windows内核中&#xff0c;SSSDT&#xff08;System Service Shadow Descriptor Table&#xff09;是SSDT&#xff08;System Service Descriptor Table&#xff09;的一种变种&#xff0c;其主要用途是提供Windows系统对系统服务调用的阴影拷贝。SSSDT表存储了系统调用的函数…

3.前端--HTML标签-文本图像链接【2023.11.25】

1.HTML常用标签(文本图像链接&#xff09; 文本标签 标题 <h1> - <h6> 段落<p> 我是一个段落标签 </p> 换行 <br /> <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta ht…
最新文章