본문 바로가기

3.구현/VC++

윈도우 애플리케이션 제어

아래는 C#을 이용해서 윈도우 애플리케이션(메신저)을 제어하는 예제코드이다.
재미있기때문에 한번해보길 바란다. 잘만하면 다양한 응용방법이 나올 수 있다.

대충적인 그림은 윈도우 애플리케이션(이하 메신저로 간주)를 다른 프로그램에서 임의의 버튼을 클릭하거나 마우스 조작을 하거나 특정 입력이 하여 작업을 수행하게 한다.

아래 코드는 C#으로 되어 있지만 기본적으로 Win32 API를 이용했기 때문에 다른 곳에서도 응용해서 실행할 수 있다.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace Test01
{
   public partial class Test1 : Form
   {
       [DllImport("user32.dll")]
       public static extern int FindWindow(string lpClassName, string lpWindowName);

       [DllImport("user32.dll")]
       public static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);

       [DllImport("user32.dll")]
       public static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);

       [DllImport("user32.dll")]
       public static extern IntPtr GetWindow(IntPtr hwnd, int uCmd);

       [DllImport("user32.dll")]
       public static extern int GetWindowText(int hWnd, StringBuilder title, int size);

       [DllImport("user32.dll")]
       private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

       private const int SW_HIDE = 0;
       private const int SW_SHOWNORMAL = 1;
       private const int SW_SHOWMINIMIZED = 2;
       private const int SW_SHOWMAXIMIZED = 3;
       private const int SW_SHOWACTIVATE = 4;
       private const int SW_RESTORE = 9;
       private const int SW_SHOWDEFAULT = 10;

       public Test1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           string strBuf = "";

           strBuf += FindWindow(null, "NateOn").ToString();
           strBuf += "\r\n";
           strBuf += FindWindow("Afx:00400000:0:00010013:00000000:000803E5", null).ToString();
           strBuf += "\r\n";

           int hw1 = FindWindow(null, "NateOn");
           int hw2 = FindWindowEx(hw1, 0, "Afx:00400000:0:00010013:00000006:00000000", null);
           int hw3 = FindWindowEx(hw2, 0, "#32770", null);

           //const int GW_HWNDFIRST = 0;
           //const int GW_HWNDLAST = 1;
           //const int GW_HWNDNEXT = 2;
           //const int GW_HWNDPREV = 3;
           //const int GW_OWNER = 4;
           //const int GW_CHILD = 5;
           // 아래는 핸들러 이름이 동일할 경우에 사용한다.
           //int hwnd_first = GetWindow(hw2, GW_CHILD); // dw2의 첫번째 자식 핸들러
           //int hwnd_second = GetWindow(hwnd_first, GW_HWNDNEXT); // dw2의 다음 자식 핸들러(두번째 핸들러)

           int hw4 = FindWindowEx(hw3, 0, "Button", "친구");
           //int hw5 = FindWindowEx(hw4, 0, "Button", "친구");
           int hw5 = hw4;
           strBuf += "핸들1:" + hw1.ToString() + "\r\n";
           strBuf += "핸들2:" + hw2.ToString() + "\r\n";
           strBuf += "핸들3:" + hw3.ToString() + "\r\n";
           strBuf += "핸들4:" + hw4.ToString() + "\r\n";
           strBuf += "핸들5:" + hw5.ToString() + "\r\n";

           textBox1.Text = strBuf;

           //const int WM_LBUTTONDOWN = 0x0201;
           //const int WM_LBUTTONUP = 0x0202;
           const int BM_CLICK = 0x00F5;

           SendMessage(hw5, BM_CLICK, 0, 1);

           //SendMessage(hw5, WM_LBUTTONDOWN, 0, 1);
           //SendMessage(hw5, WM_LBUTTONUP, 0, 1);
       }

   }
}
반응형