우선 xaml에서 계산기처럼 디자인을 꾸민다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <Window x:Class="test1.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:test1" mc:Ignorable="d" Title="계산기" Height="417.042" Width="280"> <Grid> <Button x:Name="버튼0" Content="0" HorizontalAlignment="Left" Margin="86,322,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼0_Click" Height="40"/> <Button x:Name="버튼1" Content="1" HorizontalAlignment="Left" Margin="32,274,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼1_Click" Height="40"/> <Button x:Name="버튼2" Content="2" HorizontalAlignment="Left" Margin="86,274,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼2_Click" Height="40"/> <Button x:Name="버튼3" Content="3" HorizontalAlignment="Left" Margin="140,274,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼3_Click" Height="40"/> <Button x:Name="버튼4" Content="4" HorizontalAlignment="Left" Margin="32,226,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼4_Click" Height="40"/> <Button x:Name="버튼5" Content="5" HorizontalAlignment="Left" Margin="86,226,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼5_Click" Height="40"/> <Button x:Name="버튼6" Content="6" HorizontalAlignment="Left" Margin="140,226,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼6_Click" Height="40"/> <Button x:Name="버튼7" Content="7" HorizontalAlignment="Left" Margin="32,178,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼7_Click" Height="40"/> <Button x:Name="버튼8" Content="8" HorizontalAlignment="Left" Margin="86,178,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼8_Click" Height="40"/> <Button x:Name="버튼9" Content="9" HorizontalAlignment="Left" Margin="140,178,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼9_Click" Height="40"/> <Button x:Name="나누기" Content="÷" HorizontalAlignment="Left" Margin="194,130,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="나누기_Click" Height="40" Background="#FF191414" Foreground="White" FontSize="20"/> <Button x:Name="곱하기" Content="×" HorizontalAlignment="Left" Margin="140,130,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="곱하기_Click" Height="40" Background="#FF191414" Foreground="White" FontSize="20"/> <Button x:Name="빼기" Content="-" HorizontalAlignment="Left" Margin="86,130,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="빼기_Click" Height="40" Background="#FF191414" Foreground="White" FontSize="20"/> <Button x:Name="더하기" Content="+" HorizontalAlignment="Left" Margin="32,130,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="더하기_Click" Height="40" Background="#FF191414" Foreground="White" FontSize="20"/> <Button x:Name="버튼C" Content="C" HorizontalAlignment="Left" Margin="194,226,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="버튼C_Click" Height="40" Background="#FF191414" Foreground="#FFD82424" FontSize="15" FontWeight="Bold"/> <Button x:Name="버튼결과" Content="=" HorizontalAlignment="Left" Margin="194,274,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="결과_Click" Height="88" Background="#FF191414" Foreground="#FF21B43C" FontSize="20" FontWeight="Bold"/> <Label x:Name="저장값" Content="" HorizontalAlignment="Left" Margin="32,35,0,0" VerticalAlignment="Top" Width="212" HorizontalContentAlignment="Right"/> <Label x:Name="결과" Content="0" HorizontalAlignment="Left" Margin="32,67,0,0" VerticalAlignment="Top" Width="212" Height="45" FontSize="20" HorizontalContentAlignment="Right" FontWeight="Bold"/> <Button x:Name="지우기" Content="←" HorizontalAlignment="Left" Margin="194,178,0,0" VerticalAlignment="Top" Width="45" RenderTransformOrigin="-0.444,0.608" Click="지우기_Click" Height="40" Background="#FF191414" Foreground="#FF647ADC" FontSize="15" FontWeight="Bold"/> </Grid> </Window> | cs |
값은 속성에서 바꿔줘도 되고 xaml 코드에서 직접 수정해도 된다.
이 후 버튼에 버튼클릭 함수를 하나씩 연결해준다.
cs파일에는 버튼마다 연결된 함수를 정의해준다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private int Result = 0; private int temp = 0; private int type = 0; private bool end = false; private void 버튼0_Click(object sender, RoutedEventArgs e) { if (end) { end = false; Result = 0; } Result = Result * 10; 결과.Content = Result.ToString(); } private void 버튼1_Click(object sender, RoutedEventArgs e) { if (end) { end = false; Result = 0; } Result = Result * 10 + 1; 결과.Content = Result.ToString(); } private void 버튼2_Click(object sender, RoutedEventArgs e) { if (end) { end = false; Result = 0; } Result = Result * 10 + 2; 결과.Content = Result.ToString(); } private void 버튼3_Click(object sender, RoutedEventArgs e) { if (end) { end = false; Result = 0; } Result = Result * 10 + 3; 결과.Content = Result.ToString(); } private void 버튼4_Click(object sender, RoutedEventArgs e) { if (end) { end = false; Result = 0; } Result = Result * 10 + 4; 결과.Content = Result.ToString(); } private void 버튼5_Click(object sender, RoutedEventArgs e) { if (end) { end = false; Result = 0; } Result = Result * 10 + 5; 결과.Content = Result.ToString(); } private void 버튼6_Click(object sender, RoutedEventArgs e) { if (end) { end = false; Result = 0; } Result = Result * 10 + 6; 결과.Content = Result.ToString(); } private void 버튼7_Click(object sender, RoutedEventArgs e) { if (end) { end = false; Result = 0; } Result = Result * 10 + 7; 결과.Content = Result.ToString(); } private void 버튼8_Click(object sender, RoutedEventArgs e) { if (end) { end = false; Result = 0; } Result = Result * 10 + 8; 결과.Content = Result.ToString(); } private void 버튼9_Click(object sender, RoutedEventArgs e) { if (end) { end = false; Result = 0; } Result = Result * 10 + 9; 결과.Content = Result.ToString(); } private void 더하기_Click(object sender, RoutedEventArgs e) { temp = Result; type = 1; Result = 0; 저장값.Content += temp.ToString() + " + "; 결과.Content = Result.ToString(); } private void 빼기_Click(object sender, RoutedEventArgs e) { temp = Result; type = 2; Result = 0; 저장값.Content += temp.ToString() + " - "; 결과.Content = Result.ToString(); } private void 곱하기_Click(object sender, RoutedEventArgs e) { temp = Result; type = 3; Result = 0; 저장값.Content += temp.ToString() + " × "; 결과.Content = Result.ToString(); } private void 나누기_Click(object sender, RoutedEventArgs e) { temp = Result; type = 4; Result = 0; 저장값.Content = temp.ToString() + " ÷ "; 결과.Content = Result.ToString(); } private void 지우기_Click(object sender, RoutedEventArgs e) { Result = Result / 10; 결과.Content = Result.ToString(); } private void 버튼C_Click(object sender, RoutedEventArgs e) { temp = 0; type = 0; Result = 0; 저장값.Content = " "; 결과.Content = Result.ToString(); } private void 결과_Click(object sender, RoutedEventArgs e) { if(type!=0) { if (type == 1) temp += Result; else if (type == 2) temp -= Result; else if (type == 3) temp *= Result; else if (type == 4) temp /= Result; 저장값.Content = " "; 결과.Content = temp.ToString(); end = true; } } } | cs |
result라는 int형 변수를 만들어서 숫자를 누르면 x10한 후 숫자를 더해줘서 계산기처럼 보이게 하였다.
이후 '결과' 라는 이름의 Label의 Content에 Result값을 스트링화해서 출력하였다.
연산자를 누르면 type 변수를 이용하여 연산자를 저장해둔 후
결과버튼을 누르면 해당 연산자에 따라 계산해서 출력하게 하였다.
기본 계산기능은 이정도면 완성인데 windows 계산기처럼 결과버튼 계속눌러도 이전 연산자로 계속 계산되거나
결과버튼을 누르지않고 연산자를 계속 누르면서 계산하는 것은 구현하려면 머리를 좀 써야될것 같더라.
귀찮아서 기본 계산기만 구현하였음.
'프로그래밍 공부 > WPF' 카테고리의 다른 글
json 저장, 불러오기 (0) | 2018.04.02 |
---|---|
파일 불러오기, 이미지 출력 및 이동하기 (0) | 2018.04.02 |
캔버스 만들기, 선, 네모, 원 그리기 (그림판) (0) | 2018.04.02 |
음악 재생하기 (MediaPlayer 사용) (1) | 2018.04.02 |
WPF 시작하기 (0) | 2018.04.02 |