첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 텍스트를 갖는 진행바(Progress Bar) 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="600"

    Height="450"

    Title="텍스트를 갖는 진행바(Progress Bar) 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16"

    Loaded="Window_Loaded">

    <Grid>

        <StackPanel VerticalAlignment="Center">

            <Button

                Width="100"

                Height="30"

                Content="실행"

                Click="runButton_Click"/>

            <Border Name="progressBorder"

                Margin="0 10 0 0"

                Width="300"

                Height="30"

                BorderBrush="Black"

                BorderThickness="2">

                <Grid Margin="0">

                    <Label Name="progressBackgroundLabel"

                        Margin="0 0 0 1"

                        HorizontalAlignment="Left"

                        Width="0"

                        Background="LightGreen" />

                    <Label Name="progressLabel"

                        HorizontalAlignment="Center"

                        VerticalAlignment="Center"

                        Content="0%" />

                </Grid>

            </Border>

        </StackPanel>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.Windows;

using System.Windows.Threading;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 타이머

        /// </summary>

        private DispatcherTimer timer = null;

 

        /// <summary>

        /// 진행 최소

        /// </summary>

        private const double PROGRESS_MINIMUM = 0;

 

        /// <summary>

        /// 진행 최대

        /// </summary>

        private const double PROGRESS_MAXIMUM = 100;

 

        /// <summary>

        /// 진행 단계

        /// </summary>

        private const double PROGRESS_STEP = 4;

 

        /// <summary>

        /// 진행 값

        /// </summary>

        private double progressValue = PROGRESS_MINIMUM;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

 

        /// <summary>

        /// 윈도우 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            this.timer = new DispatcherTimer();

 

            this.timer.Interval = new TimeSpan(0, 0, 0, 0, 100);

 

            this.timer.Tick += timer_Tick;

        }

 

        #endregion

        #region 실행 버튼 클릭시 처리하기 - runButton_Click(sender, e)

 

        /// <summary>

        /// 실행 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void runButton_Click(object sender, RoutedEventArgs e)

        {

            this.progressValue = PROGRESS_MINIMUM;

 

            this.timer.Start();

        }

 

        #endregion

        #region 타이머 틱 처리하기 - timer_Tick(sender, e)

 

        /// <summary>

        /// 타이머 틱 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void timer_Tick(object sender, EventArgs e)

        {

            this.progressValue += PROGRESS_STEP;

 

            if(this.progressValue > PROGRESS_MAXIMUM)

            {

                this.progressValue = PROGRESS_MINIMUM;

 

                this.timer.Stop();

            }

 

            double fraction = (this.progressValue - PROGRESS_MINIMUM) / (PROGRESS_MAXIMUM - PROGRESS_MINIMUM);

 

            this.progressBackgroundLabel.Width = this.progressBorder.Width * fraction;

 

            this.progressLabel.Content = this.progressValue.ToString() + "%";

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요