抱歉,如果这是一个模糊的问题,这是一个非常具体的案例,很难解释。这就是我想要做的(顺便说一下,这是一个 64 位 Windows 应用程序)
- 查看应用程序中是否打开了特定的保存文件对话框窗口(下图,它是导出某些内容时在应用程序内部弹出的对话框)
- 一旦我有一个指向该窗口的指针,以某种方式访问和使用它的元素,以便我能够命名我正在保存的文件,导航到所需的文件路径,然后保存它,全部通过代码
这是我试图通过代码控制的窗口的照片(供参考) 图片
到目前为止,我已经能够找到为我提供所有活动窗口的代码,包括我所针对的窗口。这是该代码:
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# 知识有限。这是正确的方法吗?如果是,我将如何去做?