■ UserControl 클래스를 사용해 이미지 버튼을 만드는 방법을 보여준다.
▶ ImageButton.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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 |
using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace TestProject { /// <summary> /// 이미지 버튼 /// </summary> public class ImageButton : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이미지 버튼 스타일 - ImageButtonStyle /// <summary> /// 이미지 버튼 스타일 /// </summary> public enum ImageButtonStyle { /// <summary> /// DEFAULT /// </summary> Default, /// <summary> /// FLAT /// </summary> Flat }; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 이미지 버튼 상태 - ImageButtonState /// <summary> /// 이미지 버튼 상태 /// </summary> private enum ImageButtonState { None, Hover, Pressed }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 컨테이너 /// </summary> private Container container = null; /// <summary> /// 버튼 스타일 /// </summary> private ImageButtonStyle buttonStyle = ImageButtonStyle.Default; /// <summary> /// 버튼 상태 /// </summary> private ImageButtonState buttonState = ImageButtonState.None; /// <summary> /// 코너 반지름 /// </summary> private int cornerRadius = 8; /// <summary> /// 배경 이미지 /// </summary> private Image backImage; /// <summary> /// 이미지 크기 /// </summary> private Size imageSize = new Size(24, 24); /// <summary> /// 이미지 정렬 /// </summary> private ContentAlignment imageAlign = ContentAlignment.MiddleLeft; /// <summary> /// 이미지 /// </summary> private Image image; /// <summary> /// 기본 색상 /// </summary> private Color baseColor = Color.Black; /// <summary> /// 버튼 색상 /// </summary> private Color buttonColor = Color.Black; /// <summary> /// 하이라이트 색상 /// </summary> private Color highlightColor = Color.White; /// <summary> /// GLOW 투명도 /// </summary> private int glowAlpha = 0; /// <summary> /// GLOW 색상 /// </summary> private Color glowColor = Color.FromArgb(141, 189, 255); /// <summary> /// 전경색 /// </summary> private Color foreColor = Color.White; /// <summary> /// 텍스트 정렬 /// </summary> private ContentAlignment textAlign = ContentAlignment.MiddleCenter; /// <summary> /// 버튼 텍스트 /// </summary> private string buttonText; /// <summary> /// 대화 상자 결과 /// </summary> private DialogResult dialogResult = DialogResult.None; /// <summary> /// FADE IN 타이머 /// </summary> private Timer fadeInTimer = null; /// <summary> /// FADE OUT 타이머 /// </summary> private Timer fadeOutTimer = null; /// <summary> /// 키로 호출 여부 /// </summary> private bool calledByKey = false; /// <summary> /// 클릭 여부 /// </summary> private bool isClicked = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 버튼 스타일 - ButtonStyle /// <summary> /// 버튼 스타일 /// </summary> [Category("ImageButton")] [DefaultValue(typeof(ImageButtonStyle), "Default")] [Description("마우스가 클라이언트 영역 밖에 있는 동안 버튼 배경을 그릴지 여부를 설정합니다.")] public ImageButtonStyle ButtonStyle { get { return this.buttonStyle; } set { this.buttonStyle = value; Invalidate(); } } #endregion #region 코너 반지름 - CornerRadius /// <summary> /// 코너 반지름 /// </summary> [Category("ImageButton")] [DefaultValue(8)] [Description("버튼 모서리의 반지름입니다. 이 값이 클수록 모퉁이가 부드럽습니다. " + "이 속성은 컨트롤 높이의 절반보다 커서는 안됩니다.")] public int CornerRadius { get { return this.cornerRadius; } set { this.cornerRadius = value; Invalidate(); } } #endregion #region 배경 이미지 - BackImage /// <summary> /// 배경 이미지 /// </summary> [Category("ImageButton")] [DefaultValue(null)] [Description("버튼의 배경 이미지로, 이 이미지는 버튼의 기본 색상 위에 그려집니다.")] public Image BackImage { get { return this.backImage; } set { this.backImage = value; Invalidate(); } } #endregion #region 이미지 크기 - ImageSize /// <summary> /// 이미지 크기 /// </summary> [Category("ImageButton")] [DefaultValue(typeof(Size), "24, 24")] [Description("버튼에 표시할 이미지의 크기입니다. 이 속성의 기본값은 24x24입니다.")] public Size ImageSize { get { return this.imageSize; } set { this.imageSize = value; Invalidate(); } } #endregion #region 이미지 정렬 - ImageAlign /// <summary> /// 이미지 정렬 /// </summary> [Category("ImageButton")] [DefaultValue(typeof(ContentAlignment), "MiddleLeft")] [Description("버튼에 관련한 이미지의 정렬 방법입니다.")] public ContentAlignment ImageAlign { get { return this.imageAlign; } set { this.imageAlign = value; Invalidate(); } } #endregion #region 이미지 - Image /// <summary> /// 이미지 /// </summary> [Category("ImageButton")] [DefaultValue(null)] [Description("이미지의 정렬 단추에 표시된 이미지는 단추와 관련하여 텍스트가 모호한 경우 " + "사용자가 기능을 식별하는 데 도움이 됩니다.")] public Image Image { get { return this.image; } set { this.image = value; Invalidate(); } } #endregion #region 기본 색상 - BaseColor [Category("ImageButton")] [DefaultValue(typeof(Color), "Black")] [Description("버튼의 나머지 부분이 그려지는 바탕 색상입니다. " + "글래스 효과를 얻으려면 이 속성을 Transparent로 설정하십시오.")] public Color BaseColor { get { return this.baseColor; } set { this.baseColor = value; Invalidate(); } } #endregion #region 버튼 색상 - ButtonColor /// <summary> /// 버튼 색상 /// </summary> [Category("ImageButton")] [DefaultValue(typeof(Color), "Black")] [Description("기본 색상 위에 그려지는 버튼의 아래쪽 색상입니다.")] public Color ButtonColor { get { return this.buttonColor; } set { this.buttonColor = value; Invalidate(); } } #endregion #region 하이라이트 색상 - HighlightColor /// <summary> /// 하이라이트 색상 /// </summary> [Category("ImageButton")] [DefaultValue(typeof(Color), "White")] [Description("버튼 맨 위의 강조 표시 색상입니다.")] public Color HighlightColor { get { return this.highlightColor; } set { this.highlightColor = value; Invalidate(); } } #endregion #region GLOW 색상 - GlowColor /// <summary> /// GLOW 색상 /// </summary> [Category("ImageButton")] [DefaultValue(typeof(Color), "141,189,255")] [Description("마우스가 클라이언트 영역 안에 있을 때 버튼이 빛나는 색입니다.")] public Color GlowColor { get { return this.glowColor; } set { this.glowColor = value; Invalidate(); } } #endregion #region 전경색 - ForeColor /// <summary> /// 전경색 /// </summary> [Category("ImageButton")] [Browsable(true)] [DefaultValue(typeof(Color), "White")] [Description("텍스트가 그려지는 색 입니다.")] public override Color ForeColor { get { return this.foreColor; } set { this.foreColor = value; Invalidate(); } } #endregion #region 텍스트 정렬 - TextAlign /// <summary> /// 텍스트 정렬 /// </summary> [Category("ImageButton")] [DefaultValue(typeof(ContentAlignment), "MiddleCenter")] [Description("컨트롤에 표시되는 버튼 텍스트의 정렬 방법입니다.")] public ContentAlignment TextAlign { get { return this.textAlign; } set { this.textAlign = value; Invalidate(); } } #endregion #region 버튼 텍스트 - ButtonText /// <summary> /// 버튼 텍스트 /// </summary> [Category("ImageButton")] [Description("버튼에 표시되는 텍스트입니다.")] public String ButtonText { get { return this.buttonText; } set { this.buttonText = value; Invalidate(); } } #endregion #region 대화 상자 결과 - DialogResult /// <summary> /// 대화 상자 결과 /// </summary> [Category("ImageButton")] [DefaultValue(typeof(DialogResult), "DialogResult.None")] public DialogResult DialogResult { get { return this.dialogResult; } set { this.dialogResult = value; Invalidate(); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ImageButton() /// <summary> /// 생성자 /// </summary> public ImageButton() { Name = "ImageButton"; Size = new Size(100, 32); BackColor = Color.Transparent; this.SetStyle(ControlStyles.AllPaintingInWmPaint , true); this.SetStyle(ControlStyles.DoubleBuffer , true); this.SetStyle(ControlStyles.ResizeRedraw , true); this.SetStyle(ControlStyles.Selectable , true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.SetStyle(ControlStyles.UserPaint , true); this.fadeInTimer = new Timer(); this.fadeInTimer.Interval = 30; this.fadeOutTimer = new Timer(); this.fadeOutTimer.Interval = 30; Resize += UserControl_Resize; Paint += UserControl_Paint; KeyUp += UserControl_KeyUp; KeyDown += UserControl_KeyDown; MouseEnter += UserControl_MouseEnter; MouseDown += UserControl_MouseDown; MouseUp += UserControl_MouseUp; MouseLeave += UserControl_MouseLeave; GotFocus += UserControl_MouseEnter; LostFocus += UserControl_MouseLeave; this.fadeInTimer.Tick += fadeInTimer_Tick; this.fadeOutTimer.Tick += fadeOutTimer_Tick; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 리소스 해제하기 - Dispose(disposing) /// <summary> /// 리소스 해제하기 /// </summary> protected override void Dispose(bool disposing) { if(disposing) { if(this.container != null) { this.container.Dispose(); } } base.Dispose(disposing); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 사용자 컨트롤 크기 변경시 처리하기 - UserControl_Resize(sender, e) /// <summary> /// 사용자 컨트롤 크기 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_Resize(object sender, EventArgs e) { Rectangle rectangle = ClientRectangle; rectangle.X -= 1; rectangle.Y -= 1; rectangle.Width += 2; rectangle.Height += 2; using ( GraphicsPath graphicsPath = GetRoundRectangleGraphicsPath ( rectangle, CornerRadius, CornerRadius, CornerRadius, CornerRadius ) ) { Region = new Region(graphicsPath); } } #endregion #region 사용자 컨트롤 페인트시 처리하기 - UserControl_Paint(sender, e) /// <summary> /// 사용자 컨트롤 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_Paint(object sender, PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; DrawBackground(e.Graphics); DrawHighlight(e.Graphics); DrawImage(e.Graphics); DrawText(e.Graphics); DrawGlow(e.Graphics); DrawOuterBorder(e.Graphics); DrawInnerBorder(e.Graphics); } #endregion #region 사용자 컨트롤 키 UP시 처리하기 - UserControl_KeyUp(sender, e) /// <summary> /// 사용자 컨트롤 키 UP시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_KeyUp(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { if(!this.isClicked) { return; } MouseEventArgs mouseEventArgs = new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0); this.calledByKey = true; UserControl_MouseUp(sender, mouseEventArgs); } } #endregion #region 사용자 컨트롤 키 DOWN시 처리하기 - UserControl_KeyDown(sender, e) /// <summary> /// 사용자 컨트롤 키 DOWN시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { MouseEventArgs mouseEventArgs = new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0); UserControl_MouseDown(sender, mouseEventArgs); } } #endregion #region 사용자 컨트롤 마우스 진입시 처리하기 - UserControl_MouseEnter(sender, e) /// <summary> /// 사용자 컨트롤 마우스 진입시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_MouseEnter(object sender, EventArgs e) { this.buttonState = ImageButtonState.Hover; this.fadeOutTimer.Stop(); this.fadeInTimer.Start(); } #endregion #region 사용자 컨트롤 마우스 이탈시 처리하기 - UserControl_MouseLeave(sender, e) /// <summary> /// 사용자 컨트롤 마우스 이탈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_MouseLeave(object sender, EventArgs e) { this.buttonState = ImageButtonState.None; if(this.buttonStyle == ImageButtonStyle.Flat) { this.glowAlpha = 0; } this.fadeInTimer.Stop(); this.fadeOutTimer.Start(); this.isClicked = false; } #endregion #region 사용자 컨트롤 마우스 DOWN시 처리하기 - UserControl_MouseDown(sender, e) /// <summary> /// 사용자 컨트롤 마우스 DOWN시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_MouseDown(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Left) { if(this.isClicked) { return; } this.isClicked = true; this.buttonState = ImageButtonState.Pressed; if(this.buttonStyle != ImageButtonStyle.Flat) { this.glowAlpha = 255; } this.fadeInTimer.Stop(); this.fadeOutTimer.Stop(); Invalidate(); } } #endregion #region 사용자 컨트롤 마우스 UP시 처리하기 - UserControl_MouseUp(sender, e) /// <summary> /// 사용자 컨트롤 마우스 UP시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_MouseUp(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Left) { this.buttonState = ImageButtonState.Hover; this.fadeInTimer.Stop(); this.fadeOutTimer.Stop(); Invalidate(); if(this.calledByKey == true) { OnClick(EventArgs.Empty); this.calledByKey = false; } this.isClicked = false; } } #endregion #region FADE IN 타이머 틱 처리하기 - fadeInTimer_Tick(sender, e) /// <summary> /// FADE IN 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fadeInTimer_Tick(object sender, EventArgs e) { if(this.buttonStyle == ImageButtonStyle.Flat) { this.glowAlpha = 0; } if(this.glowAlpha + 30 >= 255) { this.glowAlpha = 255; this.fadeInTimer.Stop(); } else { this.glowAlpha += 30; } Invalidate(); } #endregion #region FADE OUT 타이머 틱 처리하기 - fadeOutTimer_Tick(sender, e) /// <summary> /// FADE OUT 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fadeOutTimer_Tick(object sender, EventArgs e) { if(this.buttonStyle == ImageButtonStyle.Flat) { this.glowAlpha = 0; } if(this.glowAlpha - 30 <= 0) { this.glowAlpha = 0; this.fadeOutTimer.Stop(); } else { this.glowAlpha -= 30; } this.Invalidate(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 둥근 사각형 그래픽스 패스 구하기 - GetRoundRectangleGraphicsPath(rectangle, r1, r2, r3, r4) /// <summary> /// 둥근 사각형 그래픽스 패스 구하기 /// </summary> /// <param name="rectangle">사각형</param> /// <param name="r1">R1</param> /// <param name="r2">R2</param> /// <param name="r3">R3</param> /// <param name="r4">R4</param> /// <returns>둥근 사각형 그래픽스 패스</returns> private GraphicsPath GetRoundRectangleGraphicsPath(RectangleF rectangle, float r1, float r2, float r3, float r4) { float x = rectangle.X; float y = rectangle.Y; float width = rectangle.Width; float height = rectangle.Height; GraphicsPath graphicsPath = new GraphicsPath(); graphicsPath.AddBezier(x, y + r1, x, y, x + r1, y, x + r1, y); graphicsPath.AddLine(x + r1, y, x + width - r2, y); graphicsPath.AddBezier(x + width - r2, y, x + width, y, x + width, y + r2, x + width, y + r2); graphicsPath.AddLine(x + width, y + r2, x + width, y + height - r3); graphicsPath.AddBezier ( x + width, y + height - r3, x + width, y + height, x + width - r3, y + height, x + width - r3, y + height ); graphicsPath.AddLine(x + width - r3, y + height, x + r4, y + height); graphicsPath.AddBezier(x + r4, y + height, x, y + height, x, y + height - r4, x, y + height - r4); graphicsPath.AddLine(x, y + height - r4, x, y + r1); return graphicsPath; } #endregion #region 문자열 포맷 구하기 - GetStringFormat(textAlignment) /// <summary> /// 문자열 포맷 구하기 /// </summary> /// <param name="textAlignment">텍스트 정렬</param> /// <returns>문자열 포맷</returns> private StringFormat GetStringFormat(ContentAlignment textAlignment) { StringFormat stringFormat = new StringFormat(); switch(textAlignment) { case ContentAlignment.TopLeft : case ContentAlignment.TopCenter : case ContentAlignment.TopRight : stringFormat.LineAlignment = StringAlignment.Near; break; case ContentAlignment.MiddleLeft : case ContentAlignment.MiddleCenter : case ContentAlignment.MiddleRight : stringFormat.LineAlignment = StringAlignment.Center; break; case ContentAlignment.BottomLeft : case ContentAlignment.BottomCenter : case ContentAlignment.BottomRight : stringFormat.LineAlignment = StringAlignment.Far; break; } switch(textAlignment) { case ContentAlignment.TopLeft : case ContentAlignment.MiddleLeft : case ContentAlignment.BottomLeft : stringFormat.Alignment = StringAlignment.Near; break; case ContentAlignment.TopCenter : case ContentAlignment.MiddleCenter : case ContentAlignment.BottomCenter : stringFormat.Alignment = StringAlignment.Center; break; case ContentAlignment.TopRight : case ContentAlignment.MiddleRight : case ContentAlignment.BottomRight : stringFormat.Alignment = StringAlignment.Far; break; } return stringFormat; } #endregion #region 클리핑 영역 설정하기 - SetClippingArea(graphics) /// <summary> /// 클리핑 영역 설정하기 /// </summary> /// <param name="graphics">그래픽스</param> private void SetClippingArea(Graphics graphics) { Rectangle rectangle = ClientRectangle; rectangle.X++; rectangle.Y++; rectangle.Width -= 3; rectangle.Height -= 3; using ( GraphicsPath graphicsPath = GetRoundRectangleGraphicsPath ( rectangle, CornerRadius, CornerRadius, CornerRadius, CornerRadius ) ) { graphics.SetClip(graphicsPath); } } #endregion #region 외부 테두리 그리기 - DrawOuterBorder(graphics) /// <summary> /// 외부 테두리 그리기 /// </summary> /// <param name="graphics">그래픽스</param> private void DrawOuterBorder(Graphics graphics) { if(this.buttonStyle == ImageButtonStyle.Flat && this.buttonState == ImageButtonState.None) { return; } Rectangle rectangle = ClientRectangle; rectangle.Width -= 1; rectangle.Height -= 1; using ( GraphicsPath graphicsPath = GetRoundRectangleGraphicsPath ( rectangle, CornerRadius, CornerRadius, CornerRadius, CornerRadius ) ) { using(Pen pen = new Pen(this.buttonColor)) { graphics.DrawPath(pen, graphicsPath); } } } #endregion #region 내부 테두리 그리기 - DrawInnerBorder(graphics) /// <summary> /// 내부 테두리 그리기 /// </summary> /// <param name="graphics">그래픽스</param> private void DrawInnerBorder(Graphics graphics) { if(this.buttonStyle == ImageButtonStyle.Flat && this.buttonState == ImageButtonState.None) { return; } Rectangle rectangle = ClientRectangle; rectangle.X++; rectangle.Y++; rectangle.Width -= 3; rectangle.Height -= 3; using ( GraphicsPath graphicsPath = GetRoundRectangleGraphicsPath ( rectangle, CornerRadius, CornerRadius, CornerRadius, CornerRadius ) ) { using(Pen pen = new Pen(this.highlightColor)) { graphics.DrawPath(pen, graphicsPath); } } } #endregion #region 배경 그리기 - DrawBackground(graphics) /// <summary> /// 배경 그리기 /// </summary> /// <param name="graphics">그래픽스</param> private void DrawBackground(Graphics graphics) { if(this.buttonStyle == ImageButtonStyle.Flat && this.buttonState == ImageButtonState.None) { return; } int alpha = (this.buttonState == ImageButtonState.Pressed) ? 204 : 127; Rectangle rectangle = ClientRectangle; rectangle.Width--; rectangle.Height--; using ( GraphicsPath graphicsPath = GetRoundRectangleGraphicsPath ( rectangle, CornerRadius, CornerRadius, CornerRadius, CornerRadius ) ) { using(SolidBrush solidBrush = new SolidBrush(this.baseColor)) { graphics.FillPath(solidBrush, graphicsPath); } SetClippingArea(graphics); if(this.backImage != null) { graphics.DrawImage(this.backImage, ClientRectangle); } graphics.ResetClip(); using(SolidBrush solidBrush = new SolidBrush(Color.FromArgb(alpha, this.ButtonColor))) { graphics.FillPath(solidBrush, graphicsPath); } } } #endregion #region 하이라이트 그리기 - DrawHighlight(graphics) /// <summary> /// 하이라이트 그리기 /// </summary> /// <param name="graphics">그래픽스</param> private void DrawHighlight(Graphics graphics) { if(this.buttonStyle == ImageButtonStyle.Flat && this.buttonState == ImageButtonState.None) { return; } int alpha = (this.buttonState == ImageButtonState.Pressed) ? 60 : 150; Rectangle rectangle = new Rectangle(0, 0, Width, Height / 2); GraphicsPath graphicsPath = GetRoundRectangleGraphicsPath ( rectangle, CornerRadius, CornerRadius, 0, 0 ); LinearGradientBrush linearGradientBrush = new LinearGradientBrush ( graphicsPath.GetBounds(), Color.FromArgb(alpha, this.HighlightColor), Color.FromArgb(alpha / 3, this.HighlightColor), LinearGradientMode.Vertical ); graphics.FillPath(linearGradientBrush, graphicsPath); graphicsPath.Dispose(); linearGradientBrush.Dispose(); } #endregion #region GLOW 그리기 - DrawGlow(graphics) /// <summary> /// GLOW 그리기 /// </summary> /// <param name="graphics">그래픽스</param> private void DrawGlow(Graphics graphics) { if(this.buttonState == ImageButtonState.Pressed || this.buttonState == ImageButtonState.None) { return; } SetClippingArea(graphics); using(GraphicsPath graphicsPath = new GraphicsPath()) { graphicsPath.AddEllipse(-5, this.Height / 2 - 10, Width + 11, Height + 11); using(PathGradientBrush pathGradientBrush = new PathGradientBrush(graphicsPath)) { pathGradientBrush.CenterColor = Color.FromArgb(this.glowAlpha, this.glowColor); pathGradientBrush.SurroundColors = new Color[] { Color.FromArgb(0, this.glowColor) }; graphics.FillPath(pathGradientBrush, graphicsPath); } } graphics.ResetClip(); } #endregion #region 텍스트 그리기 - DrawText(graphics) /// <summary> /// 텍스트 그리기 /// </summary> /// <param name="graphics">그래픽스</param> private void DrawText(Graphics graphics) { StringFormat stringFormat = GetStringFormat(this.textAlign); Rectangle rectangle = new Rectangle(8, 8, Width - 17, Height - 17); graphics.DrawString ( this.buttonText, Font, new SolidBrush(this.foreColor), rectangle, stringFormat ); } #endregion #region 이미지 그리기 - DrawImage(graphics) /// <summary> /// 이미지 그리기 /// </summary> /// <param name="graphics">그래픽스</param> private void DrawImage(Graphics graphics) { if(this.image == null) { return; } Rectangle rectangle = new Rectangle(8, 8, this.imageSize.Width, this.imageSize.Height); switch(this.imageAlign) { case ContentAlignment.TopCenter : rectangle = new Rectangle ( Width / 2 - this.imageSize.Width / 2, 8, this.imageSize.Width, this.imageSize.Height ); break; case ContentAlignment.TopRight : rectangle = new Rectangle ( Width - 8 - this.imageSize.Width, 8, this.imageSize.Width, this.imageSize.Height ); break; case ContentAlignment.MiddleLeft : rectangle = new Rectangle ( 8, Height / 2 - this.imageSize.Height / 2, this.imageSize.Width, this.imageSize.Height ); break; case ContentAlignment.MiddleCenter : rectangle = new Rectangle ( Width / 2 - this.imageSize.Width / 2, Height / 2 - this.imageSize.Height / 2, this.imageSize.Width, this.imageSize.Height ); break; case ContentAlignment.MiddleRight : rectangle = new Rectangle ( Width - 8 - this.imageSize.Width, Height / 2 - this.imageSize.Height / 2, this.imageSize.Width, this.imageSize.Height ); break; case ContentAlignment.BottomLeft : rectangle = new Rectangle ( 8, Height - 8 - this.imageSize.Height, this.imageSize.Width, this.imageSize.Height ); break; case ContentAlignment.BottomCenter : rectangle = new Rectangle ( Width / 2 - this.imageSize.Width / 2, Height - 8 - this.imageSize.Height, this.imageSize.Width, this.imageSize.Height ); break; case ContentAlignment.BottomRight : rectangle = new Rectangle ( Width - 8 - this.imageSize.Width, Height - 8 - this.imageSize.Height, this.imageSize.Width, this.imageSize.Height ); break; } graphics.DrawImage(this.image, rectangle); } #endregion } } |