我想在文本字段/文本框元素上使用Mircosoft UI自动化框架设置文本,这意味着AutomationElement来自ControlType.Edit或ControlType.Document.
目前我正在使用TextPattern以从其中一个获取文本AutomationElements:
TextPattern tp = (TextPattern)element.GetCurrentPattern(TextPattern.Pattern);
string text = tp.DocumentRange.GetText(-1).Trim();
Run Code Online (Sandbox Code Playgroud)
但现在我想在中设置一个新文本AutomationElement.我在TextPattern课堂上找不到这方法.所以我试图使用ValuePattern但我不确定这是否是正确的方法:
ValuePattern value = element.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
value.SetValue(insertText);
Run Code Online (Sandbox Code Playgroud)
还有其他方法来设置文本值吗?
另一个问题是如何在Edit/ Document元素上更改文本时获取事件?我尝试使用TextChangedEvent但是在更改文本时我没有触发任何事件:
AutomationEventHandler ehTextChanged = new AutomationEventHandler(text_event);
Automation.AddAutomationEventHandler(TextPattern.TextChangedEvent, element, TreeScope.Element, ehTextChanged);
private void text_event(object sender, AutomationEventArgs e)
{
Console.WriteLine("Text changed");
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试用c#设置一个进程窗口到前台/焦点(从那个时候没有焦点的应用程序),因此我使用的是user32.dll static extern bool SetForegroundWindow(IntPtr hWnd)方法:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
public void setFocus()
{
SetForegroundWindow(process.MainWindowHandle);
}
Run Code Online (Sandbox Code Playgroud)
一切都工作得很好,但只有当我打开visual studio 2008时,我甚至不需要从VS08启动应用程序,它足以让项目打开它.我正在关闭项目的那一刻,我的应用程序无法将另一个窗口设置为前景.唯一的结果是,在任务栏中,另一个窗口突出显示为蓝色.我将再次使用VS08打开我的项目它的工作正常.
我没有丝毫想法为什么......我的问题可能是他不能导入dll但是它不会被高亮,而其他win32函数就像static extern bool ShowWindow(IntPtr hWnd, IntPtr status);工作即使在项目关闭时也能工作.
针对这个问题的任何解决方案或提示?
编辑:
我阅读了该函数的备注,我认为我的应用程序没有焦点,所以我尝试了这个:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
static extern bool AllowSetForegroundWindow(int procID);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
public void setAUTFocus()
{
IntPtr hWnd = GetForegroundWindow();
uint processID = 0; …Run Code Online (Sandbox Code Playgroud) 我找到了一些关于这个的主题,但这些都没有帮助我解决我的问题,我想在开始之后将重点放在新创建的winform窗口上.
我在一个自己的新线程中启动表单:
application.Run(new InvisibleForm());
Run Code Online (Sandbox Code Playgroud)
并且表单出现,但焦点仍然是从窗口最后选择的窗口设置的.此表单没有标题栏,并且不在任务面板中查看,它还具有TransparencyKey集:
this.AutoScaleDimensions = new SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.BackColor = SystemColors.AppWorkspace;
this.ClientSize = new Size(992, 992);
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InvisibleForm";
this.Opacity = 0.5;
this.ShowInTaskbar = false;
this.TransparencyKey = SystemColors.AppWorkspace;
this.Load += new EventHandler(this.InvisibleForm_Load);
Run Code Online (Sandbox Code Playgroud)
现在我尝试了一些方法,但没有一个让我专注于表单或可以在前台/所有其他窗口的顶部设置表单:
this.TopMost = true;
this.Focus();
this.BringToFront();
this.Activate();
Run Code Online (Sandbox Code Playgroud)
有没有办法将程序单击/焦点事件发送到表单,以便在焦点上设置自己的事件?
我创建了一个包含一些测试数据的数据表.我的问题是,对于我的一个列,某些值太大,导致一个奇怪的布局:

(在订单栏之后应显示另外两列)
我已经尝试了一些事情:
<p:column headerText="Orders" width="50"> 没有变化<p:column headerText="Orders" style="max-width: 30px;"> 使列更稀疏,但值在30px后被切断<br/>强制断裂线,但没有变化订单值是一个大字符串.我的目标是将orders列更改为多行列,其中字符串值被拆分.
我的数据表代码:
<h:body>
<h:head>
</h:head>
<h:form id="form">
<p:dataTable id="customerTable" var="customer" value="#{customerDataTable.allCustomer}"
rows="25" paginator="true" emptyMessage="No customer found.">
<p:column headerText="Customer ID" style="text-align: center;">
<h:outputText value="#{customer.customerNumber}" />
</p:column>
<p:column headerText="Fist Name" style="text-align: center;">
<h:outputText value="#{customer.contactFirstName}" />
</p:column>
<p:column headerText="Last Name" style="text-align: center;">
<h:outputText value="#{customer.contactLastName}" />
</p:column>
<p:column headerText="Sales Employee Nr." style="text-align: center;">
<h:outputText value="#{customer.employee.employeeNumber}" />
</p:column>
<p:column headerText="Orders">
<h:outputText value="#{customer.allOrders}"/>
</p:column>
<p:column headerText="Payments" style="text-align: center;" >
<p:commandButton id="payment_btn" …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过程序集检查其他.NET应用程序正在使用的框架版本.我找到了两种方法来获得框架的版本(首先通过ImageRunetimeVersion和程序集的FullName)但我从它得到两个不同的值,我不知道哪个是正确的:
Assembly ass = Assembly.LoadFrom(autPath);
string imageRuntimeVersion = ass.ImageRuntimeVersion;
Console.WriteLine("ImageRunetimeVersion: " + imageRuntimeVersion);
Console.WriteLine("FullName: " + ass.FullName);
Console.WriteLine("");
Console.WriteLine("----");
Console.WriteLine("Referenced Assemblies: ");
Console.WriteLine("");
AssemblyName[] referencedAssemblies = ass.GetReferencedAssemblies();
foreach (AssemblyName a in referencedAssemblies)
{
Console.WriteLine(a.FullName);
}
Run Code Online (Sandbox Code Playgroud)
如果我要用我的应用程序和例如paint.net测试这个,结果是:
就像你可以看到我不能说哪个"版本"是正确的.最大的问题是,如果我要查看我的.net应用程序的项目属性,目标平台是3.5而不是2.0或1.0-
我读了一些关于MainWindowHandle的线程,但我无法找到解决我的问题的方法,我正在启动一个gui应用程序,并希望通过进程对象获取MainWindowHandle,但如果我不打算将句柄值始终为零等待thread.sleep()直到加载gui.我也尝试使用WaitForInputIdle,但它根本没有帮助.
process.Start();
process.WaitForInputIdle(1000);
while (process.MainWindowHandle == IntPtr.Zero)
{
Thread.Sleep(100);
}
// do something with the handle
Run Code Online (Sandbox Code Playgroud)
他永远不会离开,如果我用正常的线程替换waitforinputidle.睡觉他得到正确的句柄.
简单地说:我只想继续我的代码,如果我得到一个句柄!=零,但我不想等待一个静态的时间