我有以下问题。让我把它放在上下文中:我在 .NET Framework 中有一个带有标签的 winform。在另一个项目(控制台应用程序)中,我希望能够访问标签并使用控制台应用程序提供的标签修改其文本。需要注意的是,先打开winform,按需执行控制台应用程序,以文本为参数,更新表单标签 我尝试了在互联网和论坛中搜索到的许多选项,但没有一个对我有用。 我想知道是否可以? 任何建议、想法或类似的。
internal static class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);
[STAThread]
static void Main()
{
const uint WM_SETTEXT = 0xC;
Process aryp = Process.GetProcessesByName("TestApp1")[0];
AutomationElement el = AutomationElement.FromHandle(aryp.MainWindowHandle);
AutomationElementCollection aryel = el.FindAll(TreeScope.Descendants, new AndCondition(new PropertyCondition(AutomationElement.IsControlElementProperty, true), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text))) ;
IntPtr hwnd = new IntPtr(aryel[0].Current.NativeWindowHandle);
SendMessage((IntPtr)hwnd, WM_SETTEXT, IntPtr.Zero, "CCCCCCCCC");
}
}