windows 在另一个窗口中设置编辑控件的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8271813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting text of an edit control in another window
提问by mattsven
I'm trying to programatically change the text of an edit control in a 'Save As' Dialog. The first part was easy - getting the "Save As" window's handle. But finding the edit control's? Not so easy.
我正在尝试以编程方式更改“另存为”对话框中编辑控件的文本。第一部分很简单 - 获取“另存为”窗口的句柄。但是找到编辑控件的?没那么容易。
Anyway, this is the code I've go to so far. Can someone tell me where I'm going wrong?
无论如何,这是我到目前为止所使用的代码。有人能告诉我我哪里错了吗?
[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, "...");
}
Thanks in advance!
提前致谢!
采纳答案by mattsven
I ended up using the functionality provided by the Autoit DLL, for it's simplicity and efficiency.
我最终使用了Autoit DLL提供的功能,因为它的简单性和效率。
But an alternative solution (and native C#) is Md. Rashim Uddin's.
但是另一种解决方案(和原生 C#)是 Md. Rashim Uddin's。
回答by Md. Rashim Uddin
Yes.it's really a complicated one. It took for me 1 week to make it workable when i was assigned to work with that. Any way I have given you the code portion below. With that code you are able set the text to the Edit control of the Save/Open Dialog. Not restricted to only these task, with that you might do anything whatever you want of that window,
是的,这真的很复杂。当我被指派使用它时,我花了 1 周的时间才使它可行。无论如何,我已经给了你下面的代码部分。使用该代码,您可以将文本设置为保存/打开对话框的编辑控件。不仅限于这些任务,你可以在那个窗口做任何你想做的事情,
Have a look pls.
请看一下。
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
}
回答by Raymond Chen
Just use the automation interface. That's what it's for. And it's a one-liner. (A long line, but still one line.)
只需使用自动化界面。这就是它的用途。而且是单行的。(一行很长,但仍然是一行。)
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");
This says "Starting at the root, find the first descendant whose name is "Save As"
, then find the first descendant whose name is "File name:"
and which is an edit control, then set the value to "Booga"
.
这表示“从根开始,找到名称为 的第一个后代"Save As"
,然后找到名称为"File name:"
并且是编辑控件的第一个后代,然后将值设置为"Booga"
。
回答by Tom Carver
If this is a Save dialog you created yourself from C#, you can just use the API to set certain parts of the save file dialog (e.g. Title, valid extensions), does this do what you need?
如果这是您从 C# 自己创建的保存对话框,您可以只使用 API 设置保存文件对话框的某些部分(例如标题、有效扩展名),这是否满足您的需求?
http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx