Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get fast instance number of given controlname
#1
I'm working on a tool which use generell AutotextTrigger. Raised trigger function is looking for active window and control to look inside a configuration database which action should be activated. If are more than one ControlName inside the window is used the database use the instance counter inside the parent window to distinguish between the controls and find the right action rule for focused control. Enclosed I put my current solution, which is working but seems to me a little bit to complicated and therefore I would like to ask for reviewing and showing me a better approach. Thanks in advance for your hint.
 
Code:
Copy      Help
// script "TestTrigger.cs"


using System.ComponentModel;
using Au.Triggers;
using System.Windows;

#region Environment
string Computer = Environment.GetEnvironmentVariable("COMPUTERNAME");
string User = Environment.GetEnvironmentVariable("USERNAME");
#endregion


#region
Trigger
ActionTriggers Triggers = new();
var tt = Triggers.Autotext;
Triggers.Of.AllWindows(); // trigger for all windows
tt.DefaultPostfixType = TAPostfix.None;
tt["##"] = o => {
    wnd a = o.Window; // wnd.active;
    if (a.Is0)
        return;
    wnd w = a.IsChild ? a.Window : a;
    wnd c = a.IsChild ? a : wnd.focused;
    string wndName = w.ClassName;
    string ctrlName = c.ClassName;     // need to append instance number inside window

    // Is a faster solution possible than following?

    int i=1;
    wndChildFinder cf = new wndChildFinder(ctrlName);
    foreach (var item in new wndChildFinder().FindAll(w)) {
        if (item.ClassName != ctrlName)
            continue;
        print.it($"item={item}");
        if (c == item)
            break;
        else
            i++;
    }

    ctrlName += $"{i}";
    print.it($"Window={wndName} Control={ctrlName}");
    
    Dictionary<string, string> criteria = new() {
        {
"Computer", Computer },
        {
"User", User },
        {
"Window", wndName },
        {
"Control",  ctrlName },
    };

    //if (tb.TextbausteinDialog(criteria, out prTextbaustein selectedItem)) {
    //    prLog.Log(prLogMode.LogTrace, $"choosen item {selectedItem.Name}");
    //    // ... Replacement or InputAction etc ...
    //}

};

Triggers.Run();
#endregion
#2
The code is good, and I can't make it better or faster.
The foreach part can be slightly shorter.
 
Code:
Copy      Help
    int i=1;
    foreach (var item in w.ChildAll(cn: ctrlName)) {
        print.it($"item={item}");
        if (c == item)
            break;
        else
            i++;
    }

or
Code:
Copy      Help
    int i = Array.IndexOf(w.ChildAll(cn: ctrlName), c) + 1;
#3
Thank you Gintaras for your time and your wonderfull piece of software. I'm switching from Autohotkey due to its awful syntax and I'm verry happy to take LibreAutomate instead.


Forum Jump:


Users browsing this thread: 1 Guest(s)