2014-07-13 6 views
2

임은 SendMessage 기능을 이해하려고 노력하고 여기 내 실제 코드입니다 :C# sendMessage 첨부 클릭 버튼

[DllImport("user32.dll")] 
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); 

[DllImport("user32.dll")] 
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); 

static void Main(string[] args) 
{ 
    Process test = Process.GetProcessesByName("calc")[0]; 
    IntPtr hwndChild = FindWindowEx(test.MainWindowHandle, IntPtr.Zero, "Button", "2"); 
    SendMessage(hwndChild, 245, IntPtr.Zero, IntPtr.Zero); 
    Console.ReadKey(); 
} 

가 매우 간단하고, 난 그냥 CALC 버튼 2를 클릭합니다,하지만 난 더 성공이없는거야.

+0

왜 자동화를 사용하지 않습니까? –

답변

3

winapi 기능을 pinvoke 할 때 오류 검사는 선택 사항입니다. C API입니다. 문제를 일으키지 않도록 예외를 던지지는 않습니다. 너 혼자해야 해. 적절한 코드는 다음과 같습니다 : 프로그램이 작동하지 않는 이유

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
    private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, 
               string className, string windowTitle); 

    ... 
    IntPtr hwndChild = FindWindowEx(test.MainWindowHandle, IntPtr.Zero, "Button", "2"); 
    if (hwndChild == IntPtr.Zero) throw new System.ComponentModel.Win32Exception(); 

지금 당신이을 알고 . 다음으로 할 일은 Spy ++ 유틸리티를 실행하고 계산기 창에서 살펴 봅니다. 중첩 된 단추까지 드릴 다운하려면 FindWindowEx() 호출을 많이 만들어야한다는 것을 알게 될 것입니다.

이렇게하려면 UI 자동화 라이브러리를 사용해보십시오.