728x90
반응형
[새 프로젝트 만들기]

[기본구성]
<Window>
<Grid>
<Grid>
<Window>
[tag작성]
<Button>호랑이</Button>
or
<Button Content="호랑이"/>

<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<!-- 주석처리: ctrl + k +c -->
<!--<Button>호랑이</Button>-->
<!--<Button Content="호랑이"/>-->
<Button
Content="호랑이"
Margin="68,99,275,78"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="200"
Height="150"
/>
</Grid>
</Window>
[버튼 1, 2 생성]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button
x:Name="button1"
Content="호랑이"
Margin="188,135,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="75"
/>
<Button
x:Name="button2"
Content="호랑이"
Margin="188,165,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="75"
/>
</Grid>
</Window>

[MainWindow.xaml.cs]
[캡션 설정]
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("1");
MessageBox.Show("2", "캡션");
MessageBox.Show("3", "캡션", MessageBoxButton.OK);
MessageBox.Show("3", "캡션", MessageBoxButton.YesNo);
MessageBox.Show("3", "캡션", MessageBoxButton.YesNoCancel);
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("1");
}
}
}

[yes/no/cancel]
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBoxResult result1 = MessageBox.Show("3", "캡션",
MessageBoxButton.YesNoCancel);
if (result1 == MessageBoxResult.Yes)
{
MessageBox.Show("Yes");
}
else if (result1 == MessageBoxResult.No)
{
MessageBox.Show("No");
}
else if(result1 == MessageBoxResult.Cancel)
{
MessageBox.Show("Cancel");
}
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("1");
}
}
}

[Exclamation] 느낌표
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("6", "캡션", MessageBoxButton.OK,
MessageBoxImage.Exclamation, MessageBoxResult.OK);
}
}
}

[Brushes.Green]
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
//MessageBox.Show("2");
Button1.Background = Brushes.Green;
}
}
}

[2버튼으로 1버튼 제어]
button.IsEnabled = !button.IsEnabled;
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("6", "캡션", MessageBoxButton.OK,
MessageBoxImage.Exclamation, MessageBoxResult.OK);
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
//ex1) 2버튼으로 1버튼 제어
button.IsEnabled = !button.IsEnabled;
//ex2) 숨김기능
if (button.IsVisible)
{
button.Visibility = Visibility.Hidden;
}
else
{
button.Visibility = Visibility.Visible;
}
//ex3) ex2와 결과 동일 숨김기능
if (button.IsVisible)
{
button.Visibility = Visibility.Collapsed;
}
else
{
button.Visibility = Visibility.Visible;
}
}
}
}


[button1 클릭 시 button의 텍스트가 변경된다]
코끼리 버튼 클릭시 호랑이->독수리로 변경
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("6", "캡션", MessageBoxButton.OK,
MessageBoxImage.Exclamation, MessageBoxResult.OK);
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
if (button.Content.ToString() == "호랑이")
{
button.Content = "독수리";
}
else
{
button.Content = "호랑이";
}
}
}
}

[MainWindow.xaml]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="486.8">
<Grid>
<Button
x:Name="button"
Content="호랑이"
Margin="188,105,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="44" Height="49"
Click="f1"
/>
<Button
x:Name="Button1"
Content="코끼리"
Margin="188,180,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="44" Height="47"
Click="f1"
/>
<Button
x:Name="Button2"
Content="독수리"
Margin="188,255,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="44" Height="47"
Click="f1"
/>
</Grid>
</Window>
[호랑이버튼 클릭시 button 출력]
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void f1(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
MessageBox.Show(btn.Name);
}
}
}


[마우스 좌표 실시간 출력]
private void Window_MouseMove(object sender, MouseEventArgs e)
{
}
[마우스 좌표 생성]
private void Window_MouseMove(object sender, MouseEventArgs e)
{
Point pt = e.GetPosition(this);
this.Title = $"{pt.X} {pt.Y}";
}

[윈도우 배경 색상 변경하기 / 독수리 버튼]
private void f2(object sender, RoutedEventArgs e)
{
this.Background = new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0xff, 0x23));
}

728x90
반응형
'Education > Edu | .net' 카테고리의 다른 글
# 36.1) [WPF] 기본다지기3 (binding) (0) | 2021.03.08 |
---|---|
# 35) [WPF] 기본다지기2 (Grid, Canvas, binding) (0) | 2021.03.05 |
# 33) [C#] Winform 3 (VS2017에서 Oracle11g로 데이터 연동) (0) | 2021.03.03 |
# 32) [C#] Winform 2 (0) | 2021.03.02 |
# 31) [C#] Winform 1 (0) | 2021.02.26 |