첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainWindow.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 스크롤 바 배열
        /// </summary>
        private ScrollBar[] scrollBarArray = new ScrollBar[3];

        /// <summary>
        /// 텍스트 블럭 배열
        /// </summary>
        private TextBlock[] textBlockArray = new TextBlock[3];

        /// <summary>
        /// 색상 패널
        /// </summary>
        private Panel colorPanel;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            Width      = 800;
            Height     = 600;
            Title      = "색상 선택하기";
            FontFamily = new FontFamily("나눔고딕코딩");
            FontSize   = 16;

            Grid mainGrid = new Grid();

            Content = mainGrid;

            ColumnDefinition columnDefinition = new ColumnDefinition();

            columnDefinition.Width = new GridLength(200, GridUnitType.Pixel);

            mainGrid.ColumnDefinitions.Add(columnDefinition);

            columnDefinition = new ColumnDefinition();

            columnDefinition.Width = GridLength.Auto;

            mainGrid.ColumnDefinitions.Add(columnDefinition);

            columnDefinition = new ColumnDefinition();

            columnDefinition.Width = new GridLength(100, GridUnitType.Star);

            mainGrid.ColumnDefinitions.Add(columnDefinition);

            GridSplitter gridSplitter = new GridSplitter();

            gridSplitter.HorizontalAlignment = HorizontalAlignment.Center;
            gridSplitter.VerticalAlignment   = VerticalAlignment.Stretch;
            gridSplitter.Width               = 6;

            mainGrid.Children.Add(gridSplitter);

            Grid.SetRow   (gridSplitter, 0);
            Grid.SetColumn(gridSplitter, 1);

            colorPanel = new StackPanel();

            colorPanel.Background = new SolidColorBrush(SystemColors.WindowColor);

            mainGrid.Children.Add(colorPanel);

            Grid.SetRow   (colorPanel, 0);
            Grid.SetColumn(colorPanel, 2);

            Grid grid = new Grid();

            mainGrid.Children.Add(grid);

            Grid.SetRow   (grid, 0);
            Grid.SetColumn(grid, 0);

            RowDefinition rowDefinition = new RowDefinition();

            rowDefinition.Height = GridLength.Auto;

            grid.RowDefinitions.Add(rowDefinition);

            rowDefinition = new RowDefinition();

            rowDefinition.Height = new GridLength(100, GridUnitType.Star);

            grid.RowDefinitions.Add(rowDefinition);

            rowDefinition = new RowDefinition();

            rowDefinition.Height = GridLength.Auto;

            grid.RowDefinitions.Add(rowDefinition);

            for(int i = 0; i < 3; i++)
            {
                columnDefinition = new ColumnDefinition();

                columnDefinition.Width = new GridLength(33, GridUnitType.Star);

                grid.ColumnDefinitions.Add(columnDefinition);
            }

            for(int i = 0; i < 3; i++)
            {
                Label label = new Label();

                label.Content             = new string[] { "Red", "Green", "Blue" }[i];
                label.HorizontalAlignment = HorizontalAlignment.Center;

                grid.Children.Add(label);

                Grid.SetRow   (label, 0);
                Grid.SetColumn(label, i);

                scrollBarArray[i] = new ScrollBar();

                scrollBarArray[i].Focusable   = true;
                scrollBarArray[i].Orientation = Orientation.Vertical;
                scrollBarArray[i].Minimum     = 0;
                scrollBarArray[i].Maximum     = 255;
                scrollBarArray[i].SmallChange = 1;
                scrollBarArray[i].LargeChange = 16;

                scrollBarArray[i].ValueChanged += scrollBar_ValueChanged;

                grid.Children.Add(scrollBarArray[i]);

                Grid.SetRow   (scrollBarArray[i], 1);
                Grid.SetColumn(scrollBarArray[i], i);

                textBlockArray[i] = new TextBlock();

                textBlockArray[i].TextAlignment       = TextAlignment.Center;
                textBlockArray[i].HorizontalAlignment = HorizontalAlignment.Center;
                textBlockArray[i].Margin              = new Thickness(5);

                grid.Children.Add(textBlockArray[i]);

                Grid.SetRow   (textBlockArray[i], 2);
                Grid.SetColumn(textBlockArray[i], i);
            }

            Color color = (colorPanel.Background as SolidColorBrush).Color;

            scrollBarArray[0].Value = color.R;
            scrollBarArray[1].Value = color.G;
            scrollBarArray[2].Value = color.B;

            scrollBarArray[0].Focus();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        [STAThread]
        public static void Main()
        {
            Application application = new Application();

            application.Run(new MainWindow());
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 스크롤 바 값 변경시 처리하기 - scrollBar_ValueChanged(sender, e)

        /// <summary>
        /// 스크롤 바 값 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void scrollBar_ValueChanged(object sender, RoutedEventArgs e)
        {
            ScrollBar scrollBar = sender as ScrollBar;
            Panel     panel     = scrollBar.Parent as Panel;
            TextBlock textBlock = panel.Children[1 + panel.Children.IndexOf(scrollBar)] as TextBlock;

            textBlock.Text = String.Format("{0}\n0x{0:X2}", (int)scrollBar.Value);

            colorPanel.Background = new SolidColorBrush
            (
                Color.FromRgb
                (
                    (byte) scrollBarArray[0].Value,
                    (byte) scrollBarArray[1].Value,
                    (byte) scrollBarArray[2].Value
                )
            );
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker
TAG , , ,

댓글을 달아 주세요