Programming/C#

창 핸들값 구하는 API 함수

blueasa 2010. 5. 28. 21:33

윈도우에 열려있는 창을 제어하기 위한 핸들값을 가져오는 API 함수
창의 클래스명이나 캡션명은 Spy++ 로 알 수 있다.
※주의사항
함수명의 대소문자가 틀리면 안된다.
FindWindowEx() 함수를 FindWindowEX() 로 했더니 에러 발생.

[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
-> 창의 클래스명이나 창 캡션명으로 최상위 핸들값을 가져옴.
예)
int i = FindWindow(null, "Windows Messenger");  // 창의 캡션명으로 찾기
int j = FindWindow("MSBLClass", null);  // 창의 클래스명으로 찾기

[DllImport("user32.dll")]
public static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);
-> 인자값으로 받은 핸들의 자식 핸들을 가져옴. lpsz1 은 클래스명, lpsz2 는 캡션명.
예)
int hw2 = FindWindowEx(hw1, 0, "PluginHostClass", null);  // PluginHostClass 의 핸들값 가져옴.
int hw3 = FindWindowEx(hw2, 0, "MSBLGeneric", null);  // MSBLGeneric 클래스의 핸들값.
int hw4 = FindWindowEx(hw3, 0, MSBLGeneric", "Task List");  // MSBLGeneric 클래스이며 Task List 캡션명의 핸들값. 
반응형