2012-09-12 2 views
9

SendMessage Windows API를 사용하여 Acrobat PrintDialog에서 선택한 프린터의 이름을 가져 오려고합니다.Acrobat에서 선택한 프린터의 이름을 검색하려면 어떻게합니까?

이것은 샘플 코드입니다.

static string GetWindowText(hwnd_printDialog_in_Acrobat) 
{ 
    int comboBoxCount = 0; 
    int HWND_PRINTER_NAME = 1 ; 

    List<IntPtr> ChildPtrList = GetChildWindows(hwnd_printDialog_in_Acrobat); 
    for(i=0 ; i < ChildPtrList.Size(); i++) { 
     GetClassName(ChildPtrList[i], sClass, 100); 
     if (sClass.ToString() == "ComboBox") { 
      comboBoxCount++; 
      if (comboBoxCount == HWND_PRINTER_NAME) { 
       hwnd = ChildPtrList[i]; 
       break; 
      } 
     } 
    } 
    ChildPtrList.Clear(); 

    int sSize ; 
    sSize = SendMessageW(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)+1; 
    StringBuilder sbTitle = new StringBuilder(sSize); 
    SendMessageW(hwn, WM_GETTEXT, (IntPtr)sSize, sbTitle); 
    return (sbTitle.ToString()); 
} 

sSize의 반환 값은 4입니다. sbTitle.ToString()의 값은 "? -"입니다. 예상 resu 무엇이 잘못 되었습니까?

+0

spy ++을 사용하여 그룹 상자의 하위 클래스 인 "ComboBoxEx32"클래스 인 프린터 콤보 박스를 찾을 수있었습니다. 그것은 또한 ComboBoxEx32는 "㚘 \"창 캡션 "ComboBox"를 자식 것으로 보인다 ... 이것은 귀하의 컴퓨터에서 spy ++를 사용하여 가장 잘 해결할 수 있습니다 ... 일부 창은 (어도비를 아는) 핸들이 없을 수도 있습니다 이 경우 컨트롤 속성을 가져 오기 위해 MS 액세스 가능성 API를 사용해야합니다. – devHead

답변

1

여기에 귀하의 문제에 대한 내 현재의 추측이다 :

  • HWND_PRINTER_NAME이되지 않는 한
  • 당신이 찾고있는 클래스 이름이 "콤보"목록에없는 코드에서
  • 문제되지 않습니다 :
    • 잘못된 부모 창 잡기 또는 잘못 처리하기
    • SendMessageW 같이 DllImport 잘못, 또는 매개 변수를 잘못

당신은 너무 나열된 코드의 일부 버그가 어떻게 처리하는, 내 코드는 정확히 동일하지 않습니다,하지만 여기에 코드 I입니다 당신의 문제를 파악하려고 애썼다. 당신이 묘사하는 방식대로 행동하지 못했습니다. 내 코드는 클래스 이름이 "ComboBox"인 자식 윈도우를 결코 찾지 않습니다.

using System; 
using System.Collections.Generic; 
using System.Runtime.InteropServices; 
using System.Text; 

namespace PrinterChoosenInAcrobat 
{ 
class Program 
{ 
    public const uint WM_GETTEXTLENGTH = 0x000E; 
    private const uint WM_GETTEXT = 0x000D; 

    // External OS calls 
    [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)] 
    public static extern bool SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, 
     StringBuilder lParam); 

    [DllImport("user32.dll", SetLastError = true)] 
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wparam, 
     IntPtr lparam); 

    [DllImport("User32.dll")] 
    public static extern Int32 FindWindow(String lpClassName, String lpWindowName); 

    [DllImport("user32.dll")] 
    static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, 
    int nMaxCount); 

    static void Main(string[] args) 
    { 
     try 
     { 
      IntPtr windowHandle = (IntPtr)FindWindow("AcrobatSDIWindow", null); 
      string text = GetWindowText(windowHandle); 

      Console.WriteLine(text); 

     } 
     catch (Exception ex) 
     { 
      Console.Out.WriteLine(ex.Message); 
     } 

     // Don't close before I get to read the results 
     Console.WriteLine(); 
     Console.WriteLine("Press Enter to quit."); 
     Console.ReadLine(); 
    } 

    static string GetWindowText(IntPtr hwnd_printDialog_in_Acrobat) 
    { 
     int comboBoxCount = 0; 
     int HWND_PRINTER_NAME = 1; 

     List<IntPtr> ChildPtrList = GetChildWindows(hwnd_printDialog_in_Acrobat); 
     IntPtr hwnd = IntPtr.Zero; 
     for (int i = 0; i < ChildPtrList.Count; i++) 
     { 
      StringBuilder sClass = new StringBuilder(); 
      GetClassName(ChildPtrList[i], sClass, 100); 
      if (sClass.ToString() == "ComboBox") 
      { 
       comboBoxCount++; 
       if (comboBoxCount == HWND_PRINTER_NAME) 
       { 
        hwnd = ChildPtrList[i]; 
        break; 
       } 
      } 
     } 
     ChildPtrList.Clear(); 

     int sSize; 
     sSize = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero) + 1; 

     StringBuilder sbTitle = new StringBuilder(sSize); 
     SendMessage(hwnd, WM_GETTEXT, (IntPtr)sSize, sbTitle); 
     return (sbTitle.ToString()); 
    } 

    #region Code from http://pinvoke.net/default.aspx/user32/EnumChildWindows.html 
    [DllImport("user32")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i); 

    /// <summary> 
    /// Returns a list of child windows 
    /// </summary> 
    /// <param name="parent">Parent of the windows to return</param> 
    /// <returns>List of child windows</returns> 
    public static List<IntPtr> GetChildWindows(IntPtr parent) 
    { 
     List<IntPtr> result = new List<IntPtr>(); 
     GCHandle listHandle = GCHandle.Alloc(result); 
     try 
     { 
      EnumWindowProc childProc = new EnumWindowProc(EnumWindow); 
      EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle)); 
     } 
     finally 
     { 
      if (listHandle.IsAllocated) 
       listHandle.Free(); 
     } 
     return result; 
    } 

    /// <summary> 
    /// Callback method to be used when enumerating windows. 
    /// </summary> 
    /// <param name="handle">Handle of the next window</param> 
    /// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param> 
    /// <returns>True to continue the enumeration, false to bail</returns> 
    private static bool EnumWindow(IntPtr handle, IntPtr pointer) 
    { 
     GCHandle gch = GCHandle.FromIntPtr(pointer); 
     List<IntPtr> list = gch.Target as List<IntPtr>; 
     if (list == null) 
     { 
      throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>"); 
     } 
     list.Add(handle); 
     // You can modify this to check to see if you want to cancel the operation, then return a null here 
     return true; 
    } 

    /// <summary> 
    /// Delegate for the EnumChildWindows method 
    /// </summary> 
    /// <param name="hWnd">Window handle</param> 
    /// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param> 
    /// <returns>True to continue enumerating, false to bail.</returns> 
    public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter); 
    #endregion 

} 
}