2025년, 코딩은 선택이 아닌 필수!

2025년 모든 학교에서 코딩이 시작 됩니다. 먼저 준비하는 사람만이 기술을 선도해 갑니다~

강의자료/C#

[C#] 비트맵 이미지를 활용하여 폼의 디자인에 날개를 달아 보자.

원당컴퓨터학원 2021. 1. 4. 13:17

 

목표

- 비트맵 이미지를 활용해서 폼의 디자인을 변경해 보자

- 폼을 마우스로 드래그 하여 이동하는 방법을 확인해 보자.

 

 

 

 

폼구성

- 폼에 버튼을 올려서 종료로 Text를 변경

- 적당한 이미지를 다운로드 후 BMP 파일로 변환

- 탐색기를 띄워서 해당 BMP 파일을 드래그인 드래그하여 참조에 추가 후 해당 BMP 파일의 빌드작업 - 포함리소스 로 변경

- 솔루션탐색기->추가->클래스 선택해서 BitmapFormClass 클래스 생성

 

 

소스코드

- BitmapFormClass : 이미지의 path를 따서 폼과 버튼의 그림을 그려 주자

 

namespace BitmapForm 안에 다음과 같이 코딩

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
    class BitmapFormClass
    {
        public static void CreateControlRegion(Control control, Bitmap bitmap)
        {
            /*
             * 해당 Control 에 bitMap 이미지를 올리는 작업을 수행 
             */
            if (control == null || bitmap == null)
                return;
            control.Width = bitmap.Width;   // 컨트롤 크기를 비트맵 크기로 설정
            control.Height = bitmap.Height;
 
            if (control is System.Windows.Forms.Form)
            {
                ///Control이 폼이라고 하면
                ///폼의 백그라운드 이미지를 해당 이미지로 바꾼다.
                Form form = (Form)control;
 
                form.Width += 20;
                form.Height += 80;
                form.FormBorderStyle = FormBorderStyle.None;
                form.BackgroundImage = bitmap;
                GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
                form.Region = new Region(graphicsPath);
            }
            else if (control is System.Windows.Forms.Button)
            {
                //버튼 처리
                Button button = (Button)control;
                button.Text = "";
                button.Cursor = Cursors.Hand;
                button.BackgroundImage = bitmap;
                GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
                button.Region = new Region(graphicsPath);
            }
        }
 
        private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)
        {
            GraphicsPath graphicsPath = new GraphicsPath();
            Color colorTransparent = bitmap.GetPixel(00);
 
            int colOpaquePixel = 0;
 
            // 비트맵 이미지의 Path를 그리자
 
            for (int row = 0; row < bitmap.Height; row++)
            {
                colOpaquePixel = 0;
                for (int col = 0; col < bitmap.Width; col++)
                {
                    if (bitmap.GetPixel(col, row) != colorTransparent)
                    {
                        colOpaquePixel = col;
                        int colNext = col;
                        for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)
                            if (bitmap.GetPixel(colNext, row) == colorTransparent)
                                break;
                        graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));
                        col = colNext;
                    }
                }
            }
            return graphicsPath;
        }
    }
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace BitmapForm
{
    public partial class Form1 : Form
    {
        private Bitmap bmpFormImage= new Bitmap(typeof(Form1), "background.bmp");
        private Bitmap bmpcloseImage = new Bitmap(typeof(Form1), "Close.bmp");
 
        private bool FormMouseDown = false//폼을 마우스로 끌어서 이동시킬때 왼쪽 마우스가 눌려져 있는지 판단
        Point ptMouseCurrentPos; // 마우스 클릭 좌표 지정
        Point ptMouseNewPos; // 이동시 마우스 좌표
        Point ptFormCurrentPos; // 폼 위치 좌표 지정
        Point ptFormNewPos; // 이동시 폼 위치 좌표
 
        public Form1()
        {
            InitializeComponent();
            BitmapFormClass.CreateControlRegion(this, bmpFormImage); ///폼의 배경을 backgrond.bmp 이미지로
            BitmapFormClass.CreateControlRegion(btnClose, bmpcloseImage); ///버튼의 이미지를 close.bmp 이미지로
            btnClose.Top = 20;
            btnClose.Left = this.Width - btnClose.Width - 20///닫기 버튼을 오른쪽 상단으로 이동시키자.
        }
 
        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }
 
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (FormMouseDown == true// 왼쪽 마우스 클릭시
            {
                ptMouseNewPos = Control.MousePosition;
                ptFormNewPos.X = ptMouseNewPos.X - ptMouseCurrentPos.X + ptFormCurrentPos.X; //마우스 이동시 가로 좌표
                ptFormNewPos.Y = ptMouseNewPos.Y - ptMouseCurrentPos.Y + ptFormCurrentPos.Y; //마우스 이동시 세로 좌표
                this.Location = ptFormNewPos;
                ptFormCurrentPos = ptFormNewPos;
                ptMouseCurrentPos = ptMouseNewPos;
            }
 
        }
 
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                FormMouseDown = true// 왼쪽 마우스 클릭 체크
                ptMouseCurrentPos = Control.MousePosition; // 마우스 클릭 좌표
                ptFormCurrentPos = this.Location; // 폼의 위치 좌표
            }
        }
 
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                FormMouseDown = false// 왼쪽 마우스 클릭 해체 체크
        }
    }
}
 
cs

 

 

활용

폼의 디자인을 이미지를 활용해서 예쁘게 꾸밀 수 있다.

 

 

 

BitmapForm.zip
1.15MB

사업자 정보 표시
원당컴퓨터학원 | 기희경 | 인천 서구 당하동 1028-2 장원프라자 502호 | 사업자 등록번호 : 301-96-83080 | TEL : 032-565-5497 | Mail : icon001@naver.com | 통신판매신고번호 : 호 | 사이버몰의 이용약관 바로가기