Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A few questions
#2
Code:
Copy      Help
// script ""

//if (!Capture.WindowAndRect(out var r, CWRNeed.Window, true)) return;
//if (!Capture.WindowAndRect(out var r, CWRNeed.Window, false)) return;
//if (!Capture.WindowAndRect(out var r, CWRNeed.Rect)) return;
//if (!Capture.WindowAndRect(out var r, CWRNeed.WindowAndRect, true)) return;
//if (!Capture.WindowAndRect(out var r, CWRNeed.WindowAndRect, false)) return;

if (!Capture.WindowAndRect(out var r, CWRNeed.WindowOrRect, true)) return;
//if (!Capture.WindowAndRect(out var r, CWRNeed.WindowOrRect, false)) return;
print.it(r);

/// <summary>
///
Helps to capture various objects on screen.
/// </summary>
public static class Capture {
    /// <summary>
    ///
Waits for Shift key and then captures a window or/and rectangle on screen.
    /// </summary>
    ///
<returns>true if captured, false if pressed Esc.</returns>
    ///
<param name="results"></param>
    ///
<param name="need"></param>
    ///
<param name="rectInClient">Get rectangle in window client area.</param>
    ///
<param name="wxyFlags"></param>
    public static bool WindowAndRect(out CWRResults results, CWRNeed need, bool rectInClient = false, WXYFlags wxyFlags = 0) {
        results = default;
        
        var s = need switch {
            CWRNeed.Window => "Press Shift to capture window or control.",
            CWRNeed.Rect => "Shift+mouse move to capture rectangle on screen.",
            CWRNeed.WindowAndRect => "Shift+mouse move to capture rectangle in a window or control.",
            _ => "Press Shift to capture window or control.\nOr Shift+mouse move to capture rectangle in a window or control."
        };
        s += "\nOr press Esc to cancel.";
        using var osd = osdText.showText(s, -1);
        
        wnd w = default, wMain = default;
        bool needWindow = need != CWRNeed.Rect;
        if (needWindow) {
            using var osrw = new osdRect {  };
            osrw.Show();
            for (; ; ) {
                wait.doEvents(12);
                if (keys.isPressed(KKey.Escape)) return false;
                w = wnd.fromMouse(wxyFlags);
                osrw.Rect = w.Rect;
                if (keys.isShift) break;
            }
        }
else {
            for (; ; ) {
                wait.doEvents(12);
                if (keys.isPressed(KKey.Escape)) return false;
                if (keys.isShift) break;
            }
        }

        
        var p = mouse.xy;
        RECT r = new(p.x, p.y, 0, 0);
        
        if (need != CWRNeed.Window) {
            if (needWindow) {
                wMain = w.Window;
                var rw = rectInClient ? wMain.ClientRectInScreen : wMain.Rect;
                unsafe { api.ClipCursor(&rw); }
            }

            
            try {
                //using var osrr = new osdRect { Color = 0xff, Opacity = .5, Rect = r };
                using var osrr = new osdRect { Color = 0xff0000, Thickness = 1, Rect = r };
                osrr.Show();
                for (var r1 = r; ;) {
                    wait.doEvents(12);
                    if (keys.isPressed(KKey.Escape)) return false;
                    if (!keys.isShift) break;
                    p = mouse.xy;
                    r1.right = p.x; r1.bottom = p.y;
                    r = r1; r.Normalize(true);
                    osrr.Rect = r;
                }
            }

            finally { if (needWindow) unsafe { api.ClipCursor(null); } }
            
            if (needWindow && w != wMain) { //if rect spans multiple controls, get top-level window
                var w2 = wnd.fromMouse(wxyFlags);
                if (w2 != w) w = wMain;
            }
        }

        
        if (needWindow && rectInClient) w.MapScreenToClient(ref r);
        
        results = new(w, !r.NoArea, r);
        return true;
    }

    
    unsafe class api : NativeApi {
        [
DllImport("user32.dll")]
        internal static extern bool ClipCursor(RECT* lpRect);
    }
}


/// <summary>
///
Used with <see cref="Capture.WindowAndRect"/>.
/// </summary>
public enum CWRNeed {
    /// <summary>Need only window.</summary>
    Window,
    
    /// <summary>Need only rectangle.</summary>
    Rect,
    
    /// <summary>Need rectangle in window.</summary>
    WindowAndRect,
    
    /// <summary>Need window and optionally rectangle in it.</summary>
    WindowOrRect
}

/// <summary>
///
Used with <see cref="Capture.WindowAndRect"/>.
/// </summary>
public record struct CWRResults(wnd w, bool hasRect, RECT r);


Messages In This Thread
A few questions - by marktech - 12-10-2022, 08:13 PM
RE: A few questions - by Gintaras - 12-11-2022, 12:16 PM
RE: A few questions - by Gintaras - 12-11-2022, 12:17 PM
RE: A few questions - by marktech - 12-11-2022, 01:14 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)