[C# 윈폼] C# 윈폼 Pnael 컨트롤 그라데이션 효과 주기


안녕하세요.

 

오늘은 C# 윈폼에서 Panel 컨트롤에 Back 컬러에서 그라데이션 효과 주는 방법에 대해서 알려 드리려고 합니다.

 

이 소스코드는 제가 직접 구현한 것이 아니라, 저도 구글링을 하다가 알게된 소스코드를 따라 작성해서 프로그램을 작성한다는 점 참고해 주시면 감사하겠습니다!


 

먼저 빈 윈폼 프로젝트를 생성해 주시고 Panel 컨트롤을 아래와 같이 배치해 주시기 바랍니다.

 

빈 윈폼 프로젝트 생성 및 Panel 컨트롤 배치



위와 같이 빈 윈폼 프로젝트에 Panel 컨트롤을 배치해 주시기 바랍니다.

Panel 컨트롤의 Name “uiPanel_Main” 이라고 정하고 바로 예제 코드를 작성해 보도록 하겠습니다.


예제 코드


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

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace GradientTest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

 

            //이벤트 선언

            InitEvent();

        }

 

        /// <summary>

        /// 이벤트 선언 메서드

        /// </summary>

        private void InitEvent()

        {

            //Panel 그라데이션 이벤트 선언

            this.Paint += 

new System.Windows.Forms.PaintEventHandler(this.Form_Gradient);

            this.uiPanel_Main.Paint += 

new System.Windows.Forms.PaintEventHandler(this.Panel_Gradient);

        }

 

        /// <summary>

        /// Panel 컨트롤 그라데이션 효과

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        public void Form_Gradient(object sender, PaintEventArgs e)

        {

            LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle,

                                                             Color.DarkTurquoise,

                                                             Color.DarkTurquoise,

                                                             0,

                                                             false);

            e.Graphics.FillRectangle(br, this.ClientRectangle);

        }

 

        /// <summary>

        /// Panel 컨트롤 그라데이션 효과

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Panel_Gradient(object sender, PaintEventArgs e)

        {

            Color startColor = System.Drawing.ColorTranslator.FromHtml("#e0c3fc");

            Color middleColor = System.Drawing.ColorTranslator.FromHtml("#8ec5fc");

            Color endColor = Color.FromArgb(000);

 

            LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle,

                                                 System.Drawing.Color.Black,

                                                 System.Drawing.Color.Black,

                                                 0,

                                                 false);

 

            ColorBlend cb = new ColorBlend();

            cb.Positions = new[] { 01 / 2f, 1 };

            cb.Colors = new[] { startColor, middleColor, endColor };

 

            br.InterpolationColors = cb;

            br.RotateTransform(45);

            e.Graphics.FillRectangle(br, this.ClientRectangle);

        }

    }

}

 

Colored by Color Scripter

cs

 

실행 결과



위와 같이 Panel 컨트롤에 그라데이션 효과가 적용된 것을 확인하실 수 있습니다.

해당 컬러는 제가 임의로 16진수 값의 Color를 적용한 것이구요, 이 부분은 사용자분께서 원하시는 색상으로 변경하여 디자인 하시면 되겠습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY