이번에는
비주얼 스튜디오 2017 C# 으로 진행할 예정입니다.
https://developer.microsoft.com/ko-kr/windows/downloads
Visual Studio Community 설치하시면 됩니다.
설치시에 .net 데스크톱 개발을 선택해주세요~
프로젝트-> Windows Forms 로 생성 해주세요
폼 디자인으로 들어가셔서 아래와 같이 현재 에너지를 보여줄 PictureBox 하나랑
에너지 게이지 방 위치를 지정할 button1 하나를 생성해주세요
button1 번을 누르면 마우스 커서를 변경하고 에너지 위치를 지정해주는 부분을 만들어 보겠습니다.
먼저 button1 번을 더블클릭해주세요~
더블클릭하면 위와같이 함수가 생성됩니다.
폼1 에서 에너지바 위치를 저장할 변수를 선언합니다. x,y width, heigt
1
2
3
4 |
public int mX = 0;
public int mY = 0;
public int mWidth = 0;
public int mHeigt = 0; |
cs |
폼1 위에 전체화면으로 폼2를 그려줄려고 합니다.
아래와 같이 폼2를 추가해주세요
Form2 는 전체화면이면서 약간 투명한 화면을 가지고 그위에서 사용자가
리니지 에너지 바를 드래그해서 선택할수 있게 하려고 합니다.
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 |
public partial class Form2 : Form
{
Pen pen = new Pen(Color.Red);
Rectangle rect;
int mX = 0;
int mY = 0;
Boolean isHold = false;
public Form2()
{
InitializeComponent();
this.Opacity = 0.73;
//전체화면 설정 부분
this.StartPosition = FormStartPosition.Manual;
Rectangle fullScrenn_bounds = Rectangle.Empty;
foreach (var screen in Screen.AllScreens)
{
fullScrenn_bounds = Rectangle.Union(fullScrenn_bounds, screen.Bounds);
}
this.ClientSize = new Size(fullScrenn_bounds.Width, fullScrenn_bounds.Height);
this.Location = new Point(fullScrenn_bounds.Left, fullScrenn_bounds.Top);
//전체화면 설정 부분
Cursor = Cursors.Cross;
this.Paint += new System.Windows.Forms.PaintEventHandler(Form2_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(Form2_MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(Form2_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(Form2_MouseUp);
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(pen, rect);
}
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
isHold = true;
rect.Location = e.Location;
mX = Cursor.Position.X;
mY = Cursor.Position.Y;
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
if (isHold)
{
int width = e.Location.X - rect.Location.X;
int height = e.Location.Y - rect.Location.Y;
rect.Width = width;
rect.Height = height;
this.Invalidate();
}
}
private void Form2_MouseUp(object sender, MouseEventArgs e)
{
isHold = false;
this.Opacity = 0.001;
int width = e.Location.X - rect.Location.X;
int height = e.Location.Y - rect.Location.Y;
rect.Width = width;
rect.Height = height;
((Form1)(this.Owner)).mX = mX;
((Form1)(this.Owner)).mY = mY;
((Form1)(this.Owner)).mWidth = rect.Width;
((Form1)(this.Owner)).mHeigt = rect.Height;
Close();
}
|
cs |
마우스 다운/무브/업 을 체크해서 빨간색 사각형을 그려주는 소스 코드 입니다.
마우스가 업이 들어왔을때 FORM2를 종료하고 X,Y.Width, Height 값을 Form1으로 넘겨줍니다.
1
2
3
4
5
6 |
((Form1)(this.Owner)).mX = mX;
((Form1)(this.Owner)).mY = mY;
((Form1)(this.Owner)).mWidth = rect.Width;
((Form1)(this.Owner)).mHeigt = rect.Height;
|
cs |
폼2가 할일은 여기서 끝입니다. 이제 사용자가 지정한 위치에 화면을 가져와서
Form 1에서 추가한 PictureBox 에 보여주도록 하겠습니다.
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 |
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog(this);
//Form2 가 close 된 후 아래 호출 됨
System.Drawing.Size mSize = new System.Drawing.Size(mWidth, mHeigt);
Bitmap image = new Bitmap(mWidth, mHeigt);
Graphics g = Graphics.FromImage(image);
try
{
g.CopyFromScreen(mX, mY, 0, 0, mSize);
}
catch (Win32Exception w)
{
Console.WriteLine(w.Message);
}
image = ResizeImage(image, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = image;
}
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
|
cs |
PictureBox 크기에 맞게 이미지를 조정하기 위해 ResizeImage 함수를 하나 추가했습니다.
폼2에서 가져온 위치와 크기만큼 캡쳐한 후 PictureBox 보여줍니다.
제 1강은 여기 까지 C#의 기본적인 사항으로 마치도록 하겠습니다.
다음 포스팅에서는 해당 이미지를 숫자인식이 잘되도록 흑백으로 변경 후
tessnet2 DLL 통해 숫자 인식 결과까지 나오게 하도록 하겠습니다.
궁금한점은 댓글로 달아주세요~