1

抱歉,如果这是一个模糊的问题,这是一个非常具体的案例,很难解释。这就是我想要做的(顺便说一下,这是一个 64 位 Windows 应用程序)

  1. 查看应用程序中是否打开了特定的保存文件对话框窗口(下图,它是导出某些内容时在应用程序内部弹出的对话框)
  2. 一旦我有一个指向该窗口的指针,以某种方式访问​​和使用它的元素,以便我能够命名我正在保存的文件,导航到所需的文件路径,然后保存它,全部通过代码

这是我试图通过代码控制的窗口的照片(供参考) 图片

到目前为止,我已经能够找到为我提供所有活动窗口的代码,包括我所针对的窗口。这是该代码:

using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;



using System.Runtime.InteropServices;
using HWND = System.IntPtr;
using System.Text;

/// <summary>Contains functionality to get all the open windows.</summary>
public static class OpenWindowGetter
{
    /// <summary>Returns a dictionary that contains the handle and title of all the open windows.</summary>
    /// <returns>A dictionary that contains the handle and title of all the open windows.</returns>
    public static IDictionary<HWND, string> GetOpenWindows()
    {
        HWND shellWindow = GetShellWindow();
        Dictionary<HWND, string> windows = new Dictionary<HWND, string>();

        EnumWindows(delegate (HWND hWnd, int lParam)
        {
            if (hWnd == shellWindow) return true;
            if (!IsWindowVisible(hWnd)) return true;

            int length = GetWindowTextLength(hWnd);
            if (length == 0) return true;

            StringBuilder builder = new StringBuilder(length);
            GetWindowText(hWnd, builder, length + 1);

            windows[hWnd] = builder.ToString();
            return true;

        }, 0);

        return windows;
    }

    private delegate bool EnumWindowsProc(HWND hWnd, int lParam);

    [DllImport("USER32.DLL")]
    private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);

    [DllImport("USER32.DLL")]
    private static extern int GetWindowText(HWND hWnd, StringBuilder lpString, int nMaxCount);

    [DllImport("USER32.DLL")]
    private static extern int GetWindowTextLength(HWND hWnd);

    [DllImport("USER32.DLL")]
    private static extern bool IsWindowVisible(HWND hWnd);

    [DllImport("USER32.DLL")]
    private static extern IntPtr GetShellWindow();
}


namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (KeyValuePair<IntPtr, string> window in OpenWindowGetter.GetOpenWindows())
            {
                IntPtr handle = window.Key;
                string title = window.Value;

                Console.WriteLine("{0}: {1}", handle, title);
            }
        }
    }
}

我不知道从这里做什么。我现在需要的帮助是检查我所针对的特定窗口(如图所示名为“导出选择”)。然后以某种方式获取对其组件的引用并控制它们。

我已经研究过使用 Spy++ 来获取有关组件的信息,然后使用 FindWindowEx 和 SendMessage 来控制它们。这不是我完全理解的东西,因为我的 C# 知识有限。这是正确的方法吗?如果是,我将如何去做?

4

1 回答 1

0

是的,使用 Spy++ 是了解该应用程序及其控件的好方法。由于您似乎已经能够检测到正确的窗口,下一步是使用该SendMessage函数将数据发送到窗口的各个控件。在 Win32 API 文档中查找以找到要发送的正确消息。即,您将使用 WM_SETTEXT 消息将文本放入“文件名”输入框中。请参阅此处:https ://docs.microsoft.com/en-US/windows/win32/winmsg/wm-settext 。

或者您使用SetWindowText函数,它的作用基本相同,但更易于使用。您已经拥有GetWindowText代码中声明的方法的原型。的 C# 声明SetWindowText应该是

[DllImport("USER32.DLL")]
private static extern int SetWindowText(HWND hWnd, String lpString);
于 2021-04-20T05:51:31.457 回答