C#/C#200제

[C#] 21일차 - 156. WPF로 Hello World 프로그램 만들기

반나무 2021. 3. 1. 14:49

WPF(Windows Presentation Foundation)

 

장점 : 디자인과 프로그램 로직이 분리되어 있다는 점.

디자인은 XAML(eXrensible Application Markup Language)로 로직은 C#을 사용한다.

강력한 데이터바인딩을 제공한다는점

컨트롤을 새로 만들 수 있다는점.(사용자가 버튼안에 이밎와 텍스트가 있는 새로운 버튼 컨트롤을 만들어서 아용할 수 있습니다.)

 

<Window x:Class="A156_WPF_HelloWorld.MainWindow"
        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:A156_WPF_HelloWorld"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="450">
    <Grid>
        <Label Name="label1"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               FontSize="30"
               FontWeight="Bold"
               Content="Hello World!"
               MouseDown="label1_MouseDown"/>
    </Grid>
</Window>
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.Navigation;
using System.Windows.Shapes;

namespace A156_WPF_HelloWorld
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // label1의 마우스 다운 이벤트 처리 메소드
        private void label1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // 처음 label1의 전경색이 흰색이 아니면 참 ( 처음엔 무조건 검정이라 참이됨 )
            if(label1.Foreground != Brushes.White)
            {
                label1.Foreground = Brushes.White;
                this.Background = Brushes.Blue;
            } else
            {
                label1.Foreground = SystemColors.WindowTextBrush;
                this.Background = SystemColors.WindowBrush;
            }
        }
    }
}

(좌) 클릭전 / (우) 클릭후

 

반응형