[C# 윈폼] Transparent Panel (투명 패널) 만들기


안녕하세요.

 

오늘은 C# 윈폼에서 투명 패널 컨트롤 만드는 방법에 대해서 알려 드리려고 합니다.

 

제가 직접 구현한 것은 아니고, Stack OverFlow에서 찾은 내용을 정리한 것이기 때문에 이점 참고해 주시기 바랍니다.^^

 

이렇게 정리하면, 향후에 필요할 때 찾아서 쓰기 편할 것 같아서 블로그에 포스팅 합니다.

 

그럼 바로 Transparent Panel 컨트롤 만드는 소스코드를 올려 드리겠습니다.


 

소스코드


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

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace TestRaritan

{

    public class GlassyPanel : Panel

    {

        const int WS_EX_TRANSPARENT = 0x20;

 

        int opacity = 50;

 

        public int Opacity

        {

            get

            {

                return opacity;

            }

            set

            {

                if (value < 0 || value > 100

throw new ArgumentException("Value must be between 0 and 100");

                opacity = value;

            }

        }

 

        protected override CreateParams CreateParams

        {

            get

            {

                var cp = base.CreateParams;

                cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;

 

                return cp;

            }

        }

 

        protected override void OnPaint(PaintEventArgs e)

        {

            using (var b = new SolidBrush(Color.FromArgb(opacity * 255 / 100, BackColor)))

            {

                e.Graphics.FillRectangle(b, ClientRectangle);

            }

 

            base.OnPaint(e);

        }

    }

}

 

Colored by Color Scripter

cs

 

이 소스코드를 이용하여 Transparent Panel 컨트롤을 만들 수 있습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY