컴퓨터잡담

다른 윈도우 창 프로그램 제어

by 디케 posted Dec 03, 2009
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄

다른 윈도우 창 프로그램 제어



HOONS님은 참 부지런 하신 분 같다.. 양질의 많은 자료를 잘 정리해 두신 분...

http://www.hoons.kr/Lecture/LectureView.aspx?BoardIdx=1&kind=26 - 1. 서론
http://www.hoons.kr/Lecture/LectureView.aspx?BoardIdx=2&kind=26 - 2. 핸들값 찾기
http://www.hoons.kr/Lecture/LectureView.aspx?BoardIdx=3&kind=26 - 3. MSN 5.0 제어하기
http://www.hoons.kr/Lecture/LectureView.aspx?BoardIdx=4&kind=26 - 4. 핸들값 찾기2
http://www.hoons.kr/Lecture/LectureView.aspx?BoardIdx=5&kind=26 - 5. 마우스 이벤트
http://www.hoons.kr/Lecture/LectureView.aspx?BoardIdx=6&kind=26 - 6. 키보드 이벤트




윈도우즈 프로그래머에게 가장 기초적이고 필수적인 도구인 스파이(Spy)에 대한 강좌

http://www.winapi.co.kr/toollec/Spy/Spy-1.htm

http://www.winapi.co.kr/toollec/Spy/Spy-3.htm

http://www.winapi.co.kr/toollec/Spy/Spy-4.htm

http://www.winapi.co.kr/toollec/Spy/Spy-5.htm

http://www.winapi.co.kr/toollec/Spy/Spy-6.htm


http://www.devpia.com/MAEUL/Contents/List.aspx?boardID=13&MAEULNo=6




.Net 다른창(윈도우) 제어하기.. 값 받아오기.. SPY++ 구현

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 WindowsApplication1
{

    public partial class Form1 : Form
    {
        //사용할 API함수를 임포트 한다.
        [DllImport("USER32.DLL")]
        public static extern uint FindWindow(string lpClassName,
            string lpWindowName);

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

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

        [DllImport("user32.dll")]
        public static extern uint PostMessage(uint hwnd, uint wMsg, uint wParam, uint lParam);

        uint handle;


        private void button3_Click(object sender, EventArgs e)
        {
           //핸들을 찾는다. Spy+를 통해 찾은 클래스 이름과 캡션을 이용하면 된다. 둘 중 하나만 알경우에도 찾을 수 있다. 그때는 하나의 인자를 null로 넘겨 주면된다.
            handle = FindWindow("SciCalc", "계산기");
          //찾은 핸들에서 자식 윈도우 핸들을 찾기 위해서는 FindWindowEx를 이용한다.
            handle = FindWindowEx(handle, 0, "Shell DocObject View", null);
            handle = FindWindowEx(handle, 0, "Internet Explorer_Server", null);
            label1.Text = handle.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //찾은 핸들을 통해 메세지를 보낸다. 여기서는 아래 16진수들 의 값은 스파이 메세지 로그에서 찾은 값을 이용하면 된다. 여기서는 키를 누른 메세지(탭키누른후 엔터키를 누른다.)를 보내는 예제 이다.
            PostMessage(handle, 0x0100, 0x9, 0xF0001);
            PostMessage(handle, 0x0101, 0x9, 0xC00F0001);
            PostMessage(handle, 0x0100, 0xD, 0x1C001);
            PostMessage(handle, 0x0102, 0xD, 0xC01C001);
            
        }
}










델파이


1. 다른 프로그램의 핸들 값을 얻어온다.(MyHandle : THandle)

   MyHandle := Findwindow(nil, 'program의 타이틀');
   
2. 다른 프로그램에 포커스를 준다.
   SetActiveWindow(MyHandle);
   SendMessage(MyHandle, WM_SETFOCUS, 0, 0);

3. 다른 프로그램에 ENTER 키를 보낸다.
   (디폴트 버튼을 클릭하고자 하는 경우에 사용할 수 있다.)
   PostMessage(MyHandle, WM_KEYDOWNVK_RETURN, 0);  

4. 윈도우의 텍스트(프로그램의 타이틀) 알아내기 (sValue : array[0..255] of char)
   GetWindowText(MyHandle, sValue, SizeOf(sValue));

5. 윈도우의 클래스 명 알아내기(ClassName : array[0..255] of char)
  GetClassName(MyHandle, ClassName, SizeOf(ClassName));

6. 다른 프로그램의 특정 위치로 마우스 이동하기 (MyRect : TRect)
   (버튼, 라디오버튼 등을 클릭하고자 할 때 이용)
  GetWindowRect(MyHandle, MyRect); // 윈도우의 위치를 가져온다.
  SetCursorPos(MyRect.left + x, MyRect.top + y);

  x : 윈도우에서의 이동하고자 하는 곳의 x좌표
  y : 윈도우에서의 이동하고자 하는 곳의 y좌표

  x와 y의 값은 VC++의 Spy나 AutoHotKey 프로그램으로 확인할 수 있습니다.

7. 윈도우의 텍스트 값을 변경하거나 설정하기(sValue : array[0..255] of char)
  sValue := '변경할 문자열';
  SendMessage(MyHandle, WM_SETTEXT, SizeOf(sValue), LongInt(@sValue));



A프로그램으로 B프로그램의 단축키 alt+j, alt+o등의 명령을 실행하도록하려했으나....대략 난감하게 모두 안되고...

마우스로 클릭하는 효과를 만들어 볼려고 게시판을 뒤지던중...성공사례가 있어서 응용중입니다.

::PostMessage(hWnd, WM_COMMAND, MAKELONG(IDOK, BN_CLICKED), (LPARAM)GetSafeHwnd());

이렇게 하셔서..성공했다고 하는데...이때는 해당 버튼은 ID가 IDOK인 것을 알았을 때이고...

저의경우에는 해당 버튼의 ID를 알 수가 없어서 다음과같이 접근했습니다.

::PostMessage(hWndButtonJoin, WM_SYSCOMMAND, BN_CLICKED, (LPARAM)GetSafeHwnd());

버튼의 윈도우핸들을 가져와서 작업했는데..

이명령을 수행하면 해당 버튼이 사사삭 사라져버리네요..ㅎㅎ

이거원 ㅠㅠ

혹시 alt+j등의 명령을 주는 깨끗한 방법아시면 좀 알려주세요..게시판 검색해보도 적당히 성공사례가 없네요...

혹은 ID를 모를때 버튼을 누를 수 있는 방법이라도...ㅠㅠ

즐씨하세요~^^저장하기

 

                 //::SendMessage((HWND)LOWORD(hWnd), WM_SYSCOMMAND, SC_KEYMENU, 'O');

                 //::SendMessage((HWND)LOWORD(hWnd), HKM_SETHOTKEY, MAKEWORD(0x4F, HOTKEYF_CONTROL | HOTKEYF_ALT), 0);

                 //::SendMessage((HWND)LOWORD(hWnd), HKM_SETHOTKEY, 0x4F, 0);

                 //::SetActiveWindow(HWND(hWnd));

                 //HWND hWndButton= ::FindWindowEx(hWnd, NULL, "Button", NULL);

                 //::SendMessage(hWndButton, WM_COMMAND, BN_CLICKED, NULL);

                 //::SendMessage(hWndButton, WM_COMMAND, BN_CLICKED, NULL);

                

                 //mouse_event(MOUSEEVENTF_LEFTDOWN, 43, 18, NULL, NULL);

                 //mouse_event(MOUSEEVENTF_LEFTUP, 43, 18, NULL, NULL);






B프로그램 윈도우의 핸들은 구하셨다면 그 윈도우를 Foreground로 놓고

 

keybd_event로 Alt+J 등의 이벤트를 날려주는게 제일 쉬워 보이는데요..