티스토리 뷰

우리가 할게 앱플레이어 게임화면을 캡쳐해서 특정 이미지를 찾아 가는 것이니

1강에서는 앱플레이어를 찾아서 캡쳐하는 것 까지 진행하도록 하겠습니다.~!

 

앱플레이어를 찾기 위해서는 User32 FindWindow API를 사용해야 합니다.

해당 API는 DllImport 선언만 해주면 쉽게 사용할수 있습니다.

단순히 내 프로젝트에서 해당 api 를 사용할것이니 프로젝트 내로 포함되어라 정도로 이해해주시면 됩니다.

 

1
2
3
4
5
6
7
8
9
10
    public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("User32", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
        public Form1()
        {
            InitializeComponent();
        }
 
cs

 

해당 위치에 FindWindow 함수를 추가해주세요. 3,4 번줄 입니다.

이제 가져올 앱플레이어 이름을 지정해줍시다.

Ctrl+Shift+ESC 키를 누르면 윈도우 작업관리자가 나옵니다. 거기서 MOMO PLAYER 를 찾습니다.

 

우리가 사용할 앱플레이어 이름은 "[MOMO]앱플레이어" 입니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
    public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("User32", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
        String AppPlayerName = "[MOMO]앱플레이어";
 
        public Form1()
        {
            InitializeComponent();
        }
 
cs

 

소스코드에 해당 이름을 선언해주도록 합시다. 6번 줄입니다.

 

실제 캡쳐하기 전에 Form1 에 버튼과 픽쳐박스 하나씩 추가하도록 하겠습니다.

Form1.cs [디자인] 탭에 들어가주세요.

그리고 아래 Button , PictureBox 를 드래그 해서 Form1 창안에 놔주세요.

 

 

 

button1 을 더블클릭하게 되면 다시 소스코드로 돌아가게 되고 button1_Click 함수가 생기게 됩니다.~!

button1 버튼을 누를 경우 해당 함수가 호출되어 동작하게 됩니다.

 

자 이제 button1 을 누르면

위에서 추가한 FindWindow 로 앱플이어를 찾아서 PictureBox 에 캡쳐한 화면을 표시해보도록 하겠습니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr findwindow = FindWindow(null, AppPlayerName);
            if (findwindow != IntPtr.Zero)
            {
                //플레이어를 찾았을 경우
                Debug.WriteLine("앱플레이어 찾았습니다.");
            }
            else
            {
                //플레이어를 못찾을경우
                Debug.WriteLine("못찾았어요");
            }
        }
cs

 

자 위에서 3번째 줄을 보게 되면 AppPlayerName을 가진 프로그램을 FindWindow 찾아라를 실행하고 있는 부분입니다.

 

4번째 줄 if 문이 해당 프로그램을 찾았을 경우와

9번째줄 못찾았을 경우를 if문으로 분기하고 있습니다.

 

F5를 눌러서 빌드를 하고 버튼을 눌렀을때 정상적으로 찾았다면

 

 

위와 같이 출력 화면에 "앱플레이어 찾았습니다." 가 나옵니다.

 

자 여기까지 진행하셨으면 이제 찾은 앱플레이어를 캡쳐해보도록 하겠습니다.

 

FindWindow 와 마찬가지로 PrintWindow 를 추가해주도록 하겠습니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("User32", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        internal static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);
 
        String AppPlayerName = "[MOMO]앱플레이어";
 
        public Form1()
        {
            InitializeComponent();
        }
cs

 

6,7 번 줄을 추가 해주었습니다. 자 이제 캡쳐할 함수도 불러왔으니 켭쳐해보도록 하죠

 

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
        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr findwindow = FindWindow(null, AppPlayerName);
            if (findwindow != IntPtr.Zero)
            {
                //플레이어를 찾았을 경우
                Debug.WriteLine("앱플레이어 찾았습니다.");
 
                //찾은 플레이어를 바탕으로 Graphics 정보를 가져옵니다.
                Graphics Graphicsdata = Graphics.FromHwnd(findwindow);
 
                //찾은 플레이어 창 크기 및 위치를 가져옵니다. 
                Rectangle rect = Rectangle.Round(Graphicsdata.VisibleClipBounds);
 
                //플레이어 창 크기 만큼의 비트맵을 선언해줍니다.
                Bitmap bmp = new Bitmap(rect.Width, rect.Height);
 
                //비트맵을 바탕으로 그래픽스 함수로 선언해줍니다.
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    //찾은 플레이어의 크기만큼 화면을 캡쳐합니다.
                    IntPtr hdc = g.GetHdc();
                    PrintWindow(findwindow, hdc, 0x2);
                    g.ReleaseHdc(hdc);
                }
 
                // pictureBox1 이미지를 표시해줍니다.
                pictureBox1.Image = bmp;
            }
            else
            {
                //플레이어를 못찾을경우
                Debug.WriteLine("못찾았어요");
            }
        }
cs

 

주석으로 설명을 달아놨습니다.

 

이제 캡쳐화면이 정상적으로 나타나는 지 확인해보도록 하죠~!

F5 -> button 클릭~!

 

 

위와 같이 화면이 잘 나오나요?

 

다음 강의에서는 원하는 이미지를 찾는거까지 진행하겠습니다.~!

 

 

#

이 블로그가 도움이 되었다면, 후원을 부탁드립니다
http://yc0345.tistory.com/218

댓글