• Can C # manage another program?
  • How much worse or better than C ++ in this regard?
  • Will VB.NET cope with this task?

Required.

  1. Resize a third-party application window.
  2. Send the application to the lowest Z level so that it does not
    Appeared on top of other open windows, while some actions will take place on it, for example button presses.
  3. You need to select the desired item in the drop-down list and then click the button to apply the settings.

I understand that all this can be done through WinApi but I do not understand where to get the descriptions of functions (preferably in Russian) and how to embed them in C # code.
For example, I remember how it was done in VB 6.0.

  • Search for WinAPI functions in MSDN - Vladimir Martyanov

2 answers 2

  • c # handle it. Any language can handle winapi
  • The only worse thing is to declare winapi functions and possibly structures.
  • vb has the same features as c #

You are here http://www.pinvoke.net/ for how to declare winapi functions in c # and vb.net

Something higher-level might be suitable, like Cruciatus from 2GIS

    A standard micro-UI Automation is quite suitable for your task. Here is an example without P / Invoke:

    var notepadProcess = Process.GetProcessesByName("Notepad").FirstOrDefault(); var window = AutomationElement.FromHandle(notepadProcess.MainWindowHandle); var transformPattern = (TransformPattern)window.GetCurrentPattern(TransformPattern.Pattern); transformPattern.Resize(300, 300); 

    (changes the size of the notepad.exe window to 300x300).

    Do not forget to connect the UIAutomationClient and UIAutomationTypes assemblies and add error control.

    MSDN has documentation on the rest of the UI Automation functionality. For example, how to activate the control (click on the button, select the menu item, etc.).

    The list of all subwindows and their available properties can be easily found using the Inspect.exe utility from the Windows SDK.


    After a little exercise in a google search on a stack flow, here’s a more serious example:

     // Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ Π±Π΅Π³ΡƒΡ‰ΠΈΠΉ notepad var notepadProcess = Process.GetProcessesByName("Notepad").FirstOrDefault(); var window = AutomationElement.FromHandle(notepadProcess.MainWindowHandle); // мСняСм Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΎΠΊΠ½Π° window.GetPattern<TransformPattern>().Resize(300, 300); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ старый тСкст var edit = window.FirstChildByType(ControlType.Document); TextPattern textPattern = edit.GetPattern<TextPattern>(); var oldText = textPattern.DocumentRange.GetText(-1); // замСняСм Π΅Π³ΠΎ Π½Π° Π½ΠΎΠ²Ρ‹ΠΉ. для этого придётся ΠΏΠΎΡΡ‹Π»Π°Ρ‚ΡŒ наТатия Π½Π° ΠΊΠ»Π°Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρƒ // основано Π½Π° этом ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅: https://msdn.microsoft.com/en-us/library/ms750582.aspx textPattern.DocumentRange.Select(); edit.SetFocus(); Thread.Sleep(100); System.Windows.Forms.SendKeys.SendWait("{DEL}"); System.Windows.Forms.SendKeys.SendWait( "Π’ΠΎΡ€ΠΌΠΎΠ·ΠΈΡ‚Π΅ прямо Π² ΠΏΠ°ΠΏΡƒ. Папа мягкий. Он простит."); // сохраним ΠΈΠ·ΠΌΠ΅Π½Ρ‘Π½Π½Ρ‹ΠΉ Ρ„Π°ΠΉΠ» ΠΏΠΎΠ΄ Π½ΠΎΠ²Ρ‹ΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ // Π²Ρ‹Π·ΠΎΠ²Π΅ΠΌ File -> Save as... (Π½Π° русскоязычной систСмС понадобятся Π΄Ρ€ΡƒΠ³ΠΈΠ΅ строки!) var menuBar = window.FirstChildByType(ControlType.MenuBar); var fileMenu = menuBar.FirstDescendantByTypeAndName(ControlType.MenuItem, "File"); // раскрыли мСню File: fileMenu.GetPattern<ExpandCollapsePattern>().Expand(); Thread.Sleep(100); // нашли ΠΏΡƒΠ½ΠΊΡ‚ Save As var saveAsMenu = fileMenu.FirstDescendantByTypeAndName(ControlType.MenuItem, "Save As..."); // ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»ΠΈ Π΅Π³ΠΎ saveAsMenu.GetPattern<InvokePattern>().Invoke(); Thread.Sleep(100); // Π·Π°ΠΏΠΎΠΌΠ½ΠΈΠ»ΠΈ элСмСнт с фокусом, это edit box для Π²Π²ΠΎΠ΄Π° ΠΈΠΌΠ΅Π½ΠΈ Ρ„Π°ΠΉΠ»Π° var saveAsEditBox = AutomationElement.FocusedElement; // помСняСм Π΅Ρ‰Ρ‘ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΡƒ Ρ‡Π΅Ρ€Π΅Π· комбобокс Encoding // нашли ΠΎΠΊΠ½ΠΎ: var saveAsDialog = AutomationHelpers.FindWindowFrom(saveAsEditBox); // ΠΈ комбобокс var encodingCombobox = saveAsDialog.FirstDescendantByTypeAndName(ControlType.ComboBox, "Encoding:"); // раскрыли Π΅Π³ΠΎ: encodingCombobox.GetPattern<ExpandCollapsePattern>().Expand(); // нашли ΠΏΡƒΠ½ΠΊΡ‚ UTF-8 ΠΈ Π²Ρ‹Π±Ρ€Π°Π»ΠΈ Π΅Π³ΠΎ: var utf8item = encodingCombobox.FirstDescendantByTypeAndName(ControlType.ListItem, "UTF-8"); utf8item.GetPattern<SelectionItemPattern>().Select(); // Π²Π΅Ρ€Π½ΡƒΠ»ΠΈ фокус Π² edit box saveAsEditBox.SetFocus(); Thread.Sleep(100); // послали Π½Π°Π·Π²Π°Π½ΠΈΠ΅ Ρ„Π°ΠΉΠ»Π° ΠΈ Enter System.Windows.Forms.SendKeys.SendWait(@"{%}TEMP{%}\oster.txt{ENTER}"); 

    The code uses the following simple helper:

     static class AutomationHelpers { static public T GetPattern<T>(this AutomationElement element) where T : BasePattern { var pattern = (AutomationPattern)typeof(T).GetField("Pattern").GetValue(null); return (T)element.GetCurrentPattern(pattern); } static public AutomationElement FirstChildByType( this AutomationElement element, ControlType ct) { return element.FindFirst( TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ct)); } static public AutomationElement FirstDescendantByTypeAndName( this AutomationElement element, ControlType ct, string name) { return element.FindFirst( TreeScope.Descendants, new AndCondition( new PropertyCondition(AutomationElement.ControlTypeProperty, ct), new PropertyCondition(AutomationElement.NameProperty, name))); } static public AutomationElement FindWindowFrom(AutomationElement control) { var walker = TreeWalker.ControlViewWalker; while (control.Current.ControlType != ControlType.Window) control = walker.GetParent(control); return control; } } 

    VB.NET will do the same; UI Automation functionality is available in both languages.


    Added pauses per second between actions and adapted to the Russian system (other menu item names), the result:

    big cartoon

    • How much does WinApi have in this respect richer opportunities compared to UI Automation? - iluxa1810
    • @ iluxa1810: Well, UI Automation cannot do something, but I didn’t make a detailed comparison. For example, with UI Automation, you cannot register someone else’s window. But with WinAPI, you can’t click on the WPF-ov button so easily (although you can probably do it somehow). - VladD
    • A similar (or very similar) thing is in jscript (or rather, in ActiveX WSchipt.Shell). - nick_n_a
    • @nick_n_a: Interesting, did not know. - VladD