■ 잘라내기, 복사하기, 그리고 붙여넣는 방법을 보여준다.
▶ MainWindow.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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Protected #region Field /// <summary> /// 잘라내기 메뉴 /// </summary> protected MenuItem cutMenuItem; /// <summary> /// 복사하기 메뉴 /// </summary> protected MenuItem copyMenuItem; /// <summary> /// 붙여넣기 메뉴 /// </summary> protected MenuItem pasteMenuItem; /// <summary> /// 지우기 메뉴 /// </summary> protected MenuItem deleteMenuItem; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Field #region Field /// <summary> /// 텍스트 블럭 /// </summary> private TextBlock textBlock; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { Width = 800; Height = 600; Title = "잘라내기, 복사하기, 그리고 붙여넣기"; FontFamily = new FontFamily("나눔고딕코딩"); FontSize = 16; DockPanel dockPanel = new DockPanel(); Content = dockPanel; Menu menu = new Menu(); dockPanel.Children.Add(menu); DockPanel.SetDock(menu, Dock.Top); this.textBlock = new TextBlock(); this.textBlock.Text = "샘플 클립보드 텍스트"; this.textBlock.HorizontalAlignment = HorizontalAlignment.Center; this.textBlock.VerticalAlignment = VerticalAlignment.Center; this.textBlock.FontSize = 32; this.textBlock.TextWrapping = TextWrapping.Wrap; dockPanel.Children.Add(this.textBlock); #region Edit 메뉴 MenuItem editMenuItem = new MenuItem(); editMenuItem.Header = "_Edit"; editMenuItem.SubmenuOpened += editMenuItem_SubmenuOpened; menu.Items.Add(editMenuItem); #endregion #region Cut 메뉴 this.cutMenuItem = new MenuItem(); this.cutMenuItem.Header = "Cu_t"; this.cutMenuItem.Click += cutMenuItem_Click; Image cutImage = new Image(); cutImage.Source = new BitmapImage(new Uri("pack://application:,,,/IMAGE/CutHS.png")); this.cutMenuItem.Icon = cutImage; editMenuItem.Items.Add(this.cutMenuItem); #endregion #region Copy 메뉴 this.copyMenuItem = new MenuItem(); this.copyMenuItem.Header = "_Copy"; this.copyMenuItem.Click += copyMenuItem_Click; Image copyImage = new Image(); copyImage.Source = new BitmapImage(new Uri("pack://application:,,,/IMAGE/CopyHS.png")); this.copyMenuItem.Icon = copyImage; editMenuItem.Items.Add(this.copyMenuItem); #endregion #region Paste 메뉴 this.pasteMenuItem = new MenuItem(); this.pasteMenuItem.Header = "_Paste"; this.pasteMenuItem.Click += pasteMenuItem_Click; Image pasteImage = new Image(); pasteImage.Source = new BitmapImage(new Uri("pack://application:,,,/IMAGE/PasteHS.png")); this.pasteMenuItem.Icon = pasteImage; editMenuItem.Items.Add(this.pasteMenuItem); #endregion #region Delete 메뉴 this.deleteMenuItem = new MenuItem(); this.deleteMenuItem.Header = "_Delete"; this.deleteMenuItem.Click += deleteMenuItem_Click; Image deleteImage = new Image(); deleteImage.Source = new BitmapImage(new Uri("pack://application:,,,/IMAGE/DeleteHS.png")); this.deleteMenuItem.Icon = deleteImage; editMenuItem.Items.Add(this.deleteMenuItem); #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> [STAThread] public static void Main() { Application application = new Application(); application.Run(new MainWindow()); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Protected #region Cut 메뉴 클릭시 처리하기 - cutMenuItem_Click(sender, e) /// <summary> /// Cut 메뉴 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> protected void cutMenuItem_Click(object sender, RoutedEventArgs e) { copyMenuItem_Click(sender, e); deleteMenuItem_Click(sender, e); } #endregion #region Copy 메뉴 클릭시 처리하기 - copyMenuItem_Click(sender, e) /// <summary> /// Copy 메뉴 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> protected void copyMenuItem_Click(object sender, RoutedEventArgs e) { if(this.textBlock.Text != null && this.textBlock.Text.Length > 0) { Clipboard.SetText(this.textBlock.Text); } } #endregion #region Paste 메뉴 클릭시 처리하기 - pasteMenuItem_Click(sender, e) /// <summary> /// Paste 메뉴 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> protected void pasteMenuItem_Click(object sender, RoutedEventArgs e) { if(Clipboard.ContainsText()) { this.textBlock.Text = Clipboard.GetText(); } } #endregion #region Delete 메뉴 클릭시 처리하기 - deleteMenuItem_Click(sender, e) /// <summary> /// Delete 메뉴 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> protected void deleteMenuItem_Click(object sender, RoutedEventArgs e) { this.textBlock.Text = null; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region Edit 메뉴 하위 메뉴 오픈시 처리하기 - editMenuItem_SubmenuOpened(sender, e) /// <summary> /// Edit 메뉴 하위 메뉴 오픈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void editMenuItem_SubmenuOpened(object sender, RoutedEventArgs e) { this.cutMenuItem.IsEnabled = this.copyMenuItem.IsEnabled = this.deleteMenuItem.IsEnabled = this.textBlock.Text != null && this.textBlock.Text.Length > 0; this.pasteMenuItem.IsEnabled = Clipboard.ContainsText(); } #endregion } } |