在另一个窗口中设置编辑控件的文本

3
我正在尝试编程地更改“另存为”对话框中编辑控件的文本。第一部分很容易 - 获取“另存为”窗口的句柄。但是找到编辑控件呢?不是那么容易。
无论如何,这是我目前拥有的代码。有人可以告诉我哪里出了问题吗?
[DllImport("user32.dll")]
public static extern Int32 SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, String lParam);

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

IntPtr dialog = FindWindow(null, "Save As");
if (dialog.ToInt32() != 0)
{
    IntPtr edit = FindWindowEx(dialog, IntPtr.Zero, "Edit", "");
    SendMessage(edit, 0x000C /* WM_SETTEXT */, IntPtr.Zero, "...");
}

提前感谢您!


发生了什么?它抛出了异常吗?你能否发布你正在使用的user32.dll函数的签名? - svick
只返回翻译后的文本:没有例外或任何东西,编辑只返回“0”...并编辑以添加我的签名。 - mattsven
4个回答

8
是的,这确实是一个复杂的问题。当我被分配处理这个问题时,我花了一周的时间才使其可行。以下是代码部分,使用该代码,您可以将文本设置为“保存/打开”对话框的编辑控件中。不仅限于这些任务,您还可以在该窗口上执行任何想要的操作。
请查看。
  public delegate int CallbackToFindChildWindow(int hWnd, int lParam);


    private void AccessEditControlOfDialog()
    {
        IntPtr winHandle = IntPtr.Zero;
        winHandle = GetActiveWindow();

        const int NumberOfChars = 256;
        string dialogCaption = string.Empty;
        StringBuilder buff = new StringBuilder(NumberOfChars);

        ////Getting the caption of window..eg.  Open/Save/Save as
        if (GetWindowText((int)winHandle, buff, NumberOfChars) > 0)
        {
            dialogCaption = buff.ToString();
        }

        ////Getting the ClassName of the Dialog Window handle
        StringBuilder winClassName = new StringBuilder();
        int numChars = GetClassName((IntPtr)winHandle, winClassName, winClassName.Capacity);

        int targetControlWinHandle;

        CallbackToFindChildWindow myCallBack = new CallbackToFindChildWindow(this.EnumChildGetValue);

        ////Find the window handle by using its caption
        targetControlWinHandle = FindWindow(null, dialogCaption);

        if (targetControlWinHandle == 0)
        {
            Logger.Error("No handle value is found in the Common Doalog");
        }
        else
        {
            EnumChildWindows(targetControlWinHandle, myCallBack, 0);
        }

    }

    private int EnumChildGetValue(int handleWnd, int param)
    {
        StringBuilder controlWinClassName = new StringBuilder();

        ////Getting the ClassName of the Control Window handle
        int numChars = GetClassName((IntPtr)handleWnd, controlWinClassName, controlWinClassName.Capacity);

        if (controlWinClassName != null)
        {
            string text = "hi";

            ////For Normal Common Dialog box, the class name of Edit box is "Edit" which is for office 2007 "RichEdit20W"
            if ((!string.Equals(controlWinClassName.ToString().Trim(), ""))
                && (controlWinClassName.ToString().Equals("Edit") || controlWinClassName.ToString().Equals("RichEdit20W")))
            {
                if (controlWinClassName.ToString().Equals("Edit"))
                {

                    //// Set Text to the Edit box                  
                    SendMessage(handleWnd, WM_SETTEXT, text.Length, text);
                }
                else if (controlWinClassName.ToString().ToLower().Equals("richedit20w"))
                {
                    SendMessage(handleWnd, WM_SETTEXT, 1, "");

                    ////Set the path to the RichEdit20W Which is specially for office 2007 and winxp                                                    
                    this.SetRichEditText((IntPtr)handleWnd, text);
                }
            }
        }
    }



    private void SetRichEditText(IntPtr handleWnd, string text)
    {
        try
        {
            const uint WM_USER = 0x0400;

            SETTEXTEX setextex = new SETTEXTEX();
            setextex.codepage = 1200;
            setextex.flags = RTBW_FLAGS.RTBW_SELECTION;
            IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.InteropServices.Marshal.SizeOf(setextex.GetType()));
            System.Runtime.InteropServices.Marshal.StructureToPtr(setextex, ptr, true);
            IntPtr stringPtr = System.Runtime.InteropServices.Marshal.StringToBSTR(text);
            int result = SendMessage((int)handleWnd, (int)WM_USER + 97, ptr.ToInt32(), text);
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr);
            System.Runtime.InteropServices.Marshal.FreeBSTR(stringPtr);
        }
        catch (Exception oEx)
        {
            Logger.Exception(oEx, "SetRichEditText");
            throw;
        }
    }

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

    [DllImport("User32.dll")]
    public static extern Int32 SendMessage(int hWnd, int Msg, int wParam, string lParam);

    [DllImport("User32.dll")]
    public static extern Int32 SendMessage(int hWnd, int Msg, int wParam, string lParam);

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

    [DllImport("User32.dll")]
    public static extern Boolean EnumChildWindows(int hWndParent, Delegate lpEnumFunc, int lParam);

    [DllImport("user32.dll")]
    public static extern IntPtr GetActiveWindow();

    [DllImport("User32.dll")]
    public static extern Int32 GetWindowText(int hWnd, StringBuilder s, int nMaxCount);

    [DllImport("User32.dll")]
    public static extern Int32 SendMessage(int hWnd, int Msg, int wParam, string lParam);

    [DllImport("User32.dll")]
    public static extern Int32 SendMessage(int hWnd, int Msg, int wParam, string lParam);

    public const Int32 WM_SETTEXT = 0x000C;

    [StructLayout(LayoutKind.Sequential)]
    public struct SETTEXTEX
    {
        public ABC.FileSystemBrowser.CustomView.CommonEnum.RTBW_FLAGS flags;
        public long codepage;
    }
    public enum RTBW_FLAGS
    {
        RTBW_DEFAULT = 0,
        RTBW_KEEPUNDO = 1,
        RTBW_SELECTION = 2
    }

非常好,你的帖子真的帮助我解决了很多问题。有一个细节 - 当我在编辑窗口中设置文本后,单击对话框保存按钮后,原始值被使用而我的新值被忽略了,你有什么其他想法可以让对话框使用新值吗? - Vojtiik
ABC.FileSystemBrowser.CustomView.CommonEnum.RTBW_FLAGS 是从哪里引用的? - Nomesh Gajare

3

只需使用自动化接口,这就是它的作用。而且这只需要一行代码(虽然很长,但仍然只有一行)。

using System.Windows.Automation;

    (AutomationElement.RootElement
        .FindFirst(TreeScope.Descendants,
                   new PropertyCondition(AutomationElement.NameProperty, "Save As"))
        .FindFirst(TreeScope.Descendants,
                   new AndCondition(
                       new PropertyCondition(AutomationElement.NameProperty,
                                             "File name:"),
                       new PropertyCondition(AutomationElement.ControlTypeProperty,
                                             ControlType.Edit)))
        .GetCurrentPattern(ValuePattern.Pattern) as ValuePattern).SetValue("Booga");

这句话的意思是“从根开始,找到第一个名为“另存为”的子孙,然后找到第一个名为“文件名:”且为编辑控件的子孙,将值设置为“Booga”。”


1

不,我没有创建保存对话框。无论如何,谢谢。 - mattsven

0

最终我使用Autoit DLL提供的功能,因为它简单高效。

但另一种替代方案(并且是本地C#)是Md. Rashim Uddin的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接