@@ -23,8 +23,28 @@
// 框坐标以锚定顶层窗口左上角为原点存成相对偏移(Entry.Offset);RefreshMap 时若目标不可见/
// 被完全遮挡/非前台,就隐藏表面(HideSurfaces)并退出选取模式,目标恢复后自动复现。
//
// “要改什么就改哪里”:快照/几何 → ReplaceItems / RefreshMap;选取与钩子 → SetSelectionMode /
// MouseHookCallback / KeyboardHookCallback;绘制 → OnPaint。
// 侧栏(NodeMapActionsForm)新增的联动功能:
// · 父节点 / 上一个 / 下一个:用 MapItems 自身的 Node 引用推父子关系(items 覆盖整棵树,
// 每个 Node 的 Children 也在 items 里),不依赖 MainForm;点击后发 NodeSelected,并把整条
// 祖先链加粗高亮(其余框变淡)。
// · 点框联动:点/滚轮选中一个框后同样发 NodeSelected(带 Node/Index/Path/ScreenPoint),
// MainForm 负责在控件树里选中并刷新属性(现有行为,本次只是把“当前项”也记进覆盖层)。
// · 显示框的信息:类型 / 名称 / 宽×高 / (X,Y) / 层级路径,显示鼠标下优先、否则当前选中的框。
// · 过滤与搜索:搜索词 + 控件类型下拉(类型从 items 收集)+“仅选中及其祖先/后代”勾选。
// · 悬停标签:只在鼠标下的那个框旁画一个“类型:名称 宽×高”小标签(120-300ms 定时器读
// Cursor.Position 在内存里命中测试,不装鼠标钩子)。
// · 其它:一键复制当前框 path、显示/隐藏标签开关。
//
// 硬性约束(勿破坏):不抢焦点/不激活/不吞普通模式点击——覆盖层保留
// WS_EX_NOACTIVATE|WS_EX_TRANSPARENT + HTTRANSPARENT;侧栏只是 NOACTIVATE(可点按钮,不点击穿透)。
// 目标最小化/关闭/被别的程序完全遮挡时的隐藏逻辑保持不变;本工具自身窗口不算遮挡。
// 每帧只读 GetWindowRect( RefreshMap),过滤/命中测试全在内存里,不新增跨进程 UIA 调用。
// 搜索框/类型下拉在 NOACTIVATE 侧栏上通过 AttachThreadInput 抢到键盘焦点(见 NodeMapInputFocus),
// 这样能输入但不会激活侧栏、也不会抢走目标窗口的激活态。
//
// “要改什么就改哪里”:快照/几何/父子关系 → ReplaceItems / RefreshMap;选取与钩子 → SetSelectionMode /
// MouseHookCallback / KeyboardHookCallback;绘制(框、祖先链、悬停标签)→ OnPaint;过滤 → ApplyFilter /
// MatchesFilter;导航 → GoParent / GoSibling;侧栏控件与布局 → NodeMapActionsForm。
// ============================================================================
using System ;
using System.Collections.Generic ;
@@ -32,12 +52,101 @@ using System.ComponentModel;
using System.Drawing ;
using System.Drawing.Drawing2D ;
using System.Runtime.InteropServices ;
using System.Text ;
using System.Windows.Forms ;
using pacs_win_uia.Interop ;
using pacs_win_uia.Models ;
using pacs_win_uia.Services ;
namespace pacs_win_uia.UI
{
// -------------------------------------------------------------------------
// NodeMapNodeInfo —— 从覆盖层持有的 Node( object,实际是 ControlTreeNode)里只读地取显示信息。
// 只读内存快照字段(Uia/Msaa/Window),绝不触发任何跨进程调用,可安全用于每帧的过滤/标签。
// -------------------------------------------------------------------------
internal static class NodeMapNodeInfo
{
/// <summary>把 Node 安全转成 ControlTreeNode(不是则返回 null)。</summary>
internal static ControlTreeNode AsNode ( object node ) { return node as ControlTreeNode ; }
/// <summary>显示名称:UIA.Name → MSAA.Name → 窗口标题。</summary>
internal static string GetName ( object node )
{
ControlTreeNode n = node as ControlTreeNode ;
if ( n = = null ) return null ;
if ( n . Uia ! = null & & ! string . IsNullOrEmpty ( n . Uia . Name ) ) return n . Uia . Name ;
if ( n . Msaa ! = null & & ! string . IsNullOrEmpty ( n . Msaa . Name ) ) return n . Msaa . Name ;
if ( n . Window ! = null & & ! string . IsNullOrEmpty ( n . Window . Title ) ) return n . Window . Title ;
return null ;
}
/// <summary>类型文本:UIA.ControlType → MSAA.RoleText → 按来源给个通用名。</summary>
internal static string GetType ( object node )
{
ControlTreeNode n = node as ControlTreeNode ;
if ( n = = null ) return null ;
if ( n . Uia ! = null & & ! string . IsNullOrEmpty ( n . Uia . ControlType ) ) return n . Uia . ControlType ;
if ( n . Msaa ! = null & & ! string . IsNullOrEmpty ( n . Msaa . RoleText ) ) return n . Msaa . RoleText ;
switch ( n . Source )
{
case ControlTreeSource . Win32 : return "Win32窗口" ;
case ControlTreeSource . Msaa : return "MSAA节点" ;
default : return "UIA控件" ;
}
}
/// <summary>类名:UIA.ClassName → 窗口类名。</summary>
internal static string GetClassName ( object node )
{
ControlTreeNode n = node as ControlTreeNode ;
if ( n = = null ) return null ;
if ( n . Uia ! = null & & ! string . IsNullOrEmpty ( n . Uia . ClassName ) ) return n . Uia . ClassName ;
if ( n . Window ! = null ) return n . Window . ClassName ;
return null ;
}
/// <summary>AutomationId(只有 UIA 节点有)。</summary>
internal static string GetAutomationId ( object node )
{
ControlTreeNode n = node as ControlTreeNode ;
return n = = null | | n . Uia = = null ? null : n . Uia . AutomationId ;
}
}
// -------------------------------------------------------------------------
// NodeMapInputFocus —— 让 NOACTIVATE 侧栏上的输入控件(搜索框/下拉)能收到键盘输入,
// 但不激活侧栏、不抢目标窗口的激活态。做法:临时把本线程附到前台线程的输入队列,
// SetFocus 后再分离(经典技巧)。失败时静默放弃(按钮/鼠标选择仍可用)。
// -------------------------------------------------------------------------
internal static class NodeMapInputFocus
{
[DllImport("user32.dll", SetLastError = false)]
private static extern IntPtr SetFocus ( IntPtr hWnd ) ;
[DllImport("user32.dll", SetLastError = false)]
private static extern bool AttachThreadInput ( uint idAttach , uint idAttachTo , bool fAttach ) ;
[DllImport("kernel32.dll", SetLastError = false)]
private static extern uint GetCurrentThreadId ( ) ;
/// <summary>把键盘焦点强制给到指定控件(不激活其顶层窗口)。</summary>
internal static void ForceFocus ( Control control )
{
if ( control = = null | | control . IsDisposed | | ! control . IsHandleCreated ) return ;
IntPtr foreground = NativeMethods . GetForegroundWindow ( ) ;
uint processId ;
uint foregroundThread = foreground = = IntPtr . Zero ? 0 :
NativeMethods . GetWindowThreadProcessId ( foreground , out processId ) ;
uint currentThread = GetCurrentThreadId ( ) ;
bool attached = false ;
if ( foregroundThread ! = 0 & & foregroundThread ! = currentThread )
attached = AttachThreadInput ( foregroundThread , currentThread , true ) ;
try { SetFocus ( control . Handle ) ; }
finally
{
if ( attached ) AttachThreadInput ( foregroundThread , currentThread , false ) ;
}
}
}
/// <summary>An immutable tree-refresh snapshot; Bounds are screen coordinates, Node is caller-owned.</summary>
/// <summary>一次树刷新后的不可变快照:屏幕矩形 + 调用方拥有的节点对象;供覆盖层只读使用。</summary>
public class NodeMapItem
@@ -106,6 +215,17 @@ namespace pacs_win_uia.UI
internal NodeMapItem Item ;
/// <summary>相对锚定窗口左上角的偏移矩形(跟随窗口移动)。</summary>
internal Rectangle Offset ;
// ---- 以下为过滤/标签/信息面板用的预计算文本(只在 ReplaceItems 时算一次,内存内使用)----
/// <summary>显示名称(来自 Node)。</summary>
internal string Name ;
/// <summary>类型(来自 Node)。</summary>
internal string Type ;
/// <summary>类名(来自 Node)。</summary>
internal string ClassName ;
/// <summary>AutomationId(来自 Node)。</summary>
internal string AutomationId ;
/// <summary>是否通过当前过滤(false 则不画、不参与命中测试)。</summary>
internal bool Visible = true ;
}
/// <summary>UI 线程 300ms 刷新定时器。</summary>
@@ -137,6 +257,36 @@ namespace pacs_win_uia.UI
/// <summary>是否已有 _candidatePoint(用于“同点同候选”判断)。</summary>
private bool _hasCandidatePoint ;
// ---- 选中/导航/父子关系(全部由 items 自身推导,不依赖 MainForm) ----
/// <summary>Node 引用 → Entry(引用相等;用于按节点找回条目、推父子关系)。</summary>
private readonly Dictionary < object , Entry > _entryByNode = new Dictionary < object , Entry > ( ) ;
/// <summary>子 Node 引用 → 父 Entry(由每个 Node 的 Children 反查得到)。</summary>
private readonly Dictionary < object , Entry > _parentOf = new Dictionary < object , Entry > ( ) ;
/// <summary>当前通过过滤、可见且有序的条目(导航在其上移动)。</summary>
private readonly List < Entry > _visibleEntries = new List < Entry > ( ) ;
/// <summary>当前选中项及其整条祖先链的 Node 引用集合(绘制加粗高亮用)。</summary>
private readonly HashSet < object > _chainNodes = new HashSet < object > ( ) ;
/// <summary>当前选中的节点(跨快照保活,按引用/Path 重新定位)。</summary>
private object _currentNode ;
/// <summary>当前选中节点的 Path( Node 为 null 时的回退定位)。</summary>
private string _currentPath ;
/// <summary>当前选中的条目(可能在过滤后被隐藏)。</summary>
private Entry _currentEntry ;
/// <summary>祖先链里是否有可见项(否则整条链不可见时不进入“变淡”模式)。</summary>
private bool _chainActive ;
/// <summary>鼠标下的条目(用于悬停标签与信息面板)。</summary>
private Entry _hoverEntry ;
/// <summary>是否显示悬停标签。</summary>
private bool _showLabels = true ;
// ---- 过滤状态(由侧栏控件驱动,见 OnFilterChanged) ----
/// <summary>搜索词(匹配 Name/AutomationId/Class/Path/类型)。</summary>
private string _filterText ;
/// <summary>类型过滤(空 = 全部类型)。</summary>
private string _filterType ;
/// <summary>是否只保留“当前选中及其祖先/后代”。</summary>
private bool _filterSelectedOnly ;
/// <summary>初始化窗体样式、创建刷新定时器与动作面板,并接线面板的选取/退出事件。</summary>
public NodeMapOverlayForm ( )
{
@@ -160,6 +310,28 @@ namespace pacs_win_uia.UI
EventHandler handler = MapExited ;
if ( handler ! = null ) handler ( this , EventArgs . Empty ) ;
} ;
// --- 新增功能的事件接线(全部在 UI 线程,面板控件不点击穿透)---
_actions . ParentRequested + = delegate { GoParent ( ) ; } ;
_actions . PrevRequested + = delegate { GoSibling ( - 1 ) ; } ;
_actions . NextRequested + = delegate { GoSibling ( 1 ) ; } ;
_actions . CopyPathRequested + = delegate { CopyCurrentPath ( ) ; } ;
_actions . FilterChanged + = delegate { OnFilterChanged ( ) ; } ;
_actions . LabelsChanged + = delegate
{
_showLabels = _actions . LabelsChecked ;
Invalidate ( ) ;
} ;
_showLabels = _actions . LabelsChecked ;
}
/// <summary>读取侧栏的过滤控件状态并刷新过滤(搜索词 / 类型 / 仅选中链路)。</summary>
private void OnFilterChanged ( )
{
_filterText = _actions . SearchText ;
_filterType = _actions . SelectedType ;
_filterSelectedOnly = _actions . SelectedOnly ;
ApplyFilter ( ) ;
UpdatePanelInfo ( _hoverEntry ! = null ? _hoverEntry : _currentEntry ) ;
}
/// <summary>节点被选中时触发。</summary>
@@ -194,6 +366,36 @@ namespace pacs_win_uia.UI
}
}
/// <summary>是否显示鼠标下的框旁边的文字标签(默认开)。</summary>
public bool ShowLabels
{
get { return _showLabels ; }
set
{
if ( _showLabels = = value ) return ;
_showLabels = value ;
_actions . SetLabels ( value ) ;
Invalidate ( ) ;
}
}
/// <summary>
/// 可选接口:把某个节点标成“当前”(加粗高亮其祖先链)。MainForm 若想在控件树里选节点时
/// 也高亮覆盖层,可在树选中回调里调用;不调用也不影响本覆盖层自身的父子/上一个/下一个。
/// </summary>
public void SetActiveNode ( object node , string path = null )
{
_currentNode = node ;
_currentPath = path ;
Entry found ;
_currentEntry = node ! = null & & _entryByNode . TryGetValue ( node , out found ) ? found : null ;
if ( _currentEntry = = null & & ! string . IsNullOrEmpty ( path ) )
_currentEntry = FindEntryByPath ( path ) ;
ApplyFilter ( ) ;
UpdatePanelInfo ( _currentEntry ) ;
Invalidate ( ) ;
}
/// <summary>显示时不激活窗口。</summary>
protected override bool ShowWithoutActivation { get { return true ; } }
/// <summary>扩展样式:工具窗口、不激活、鼠标穿透。</summary>
@@ -290,6 +492,9 @@ namespace pacs_win_uia.UI
_version + + ;
_pressedEntry = null ;
_entries . Clear ( ) ;
_entryByNode . Clear ( ) ;
_parentOf . Clear ( ) ;
_hoverEntry = null ;
if ( items ! = null )
{
for ( int index = 0 ; index < items . Count ; index + + )
@@ -298,10 +503,86 @@ namespace pacs_win_uia.UI
if ( item = = null | | item . Bounds . Width < = 1 | | item . Bounds . Height < = 1 ) continue ;
Rectangle offset = item . Bounds ;
offset . Offset ( - bounds . Left , - bounds . Top ) ;
_entries . Add ( new Entry { Index = index , Item = item , Offset = offset } ) ;
var entry = new Entry
{
Index = index ,
Item = item ,
Offset = offset ,
Name = NodeMapNodeInfo . GetName ( item . Node ) ,
Type = NodeMapNodeInfo . GetType ( item . Node ) ,
ClassName = NodeMapNodeInfo . GetClassName ( item . Node ) ,
AutomationId = NodeMapNodeInfo . GetAutomationId ( item . Node )
} ;
_entries . Add ( entry ) ;
if ( item . Node ! = null ) _entryByNode [ item . Node ] = entry ;
}
}
BuildParentMap ( ) ;
ResolveCurrentEntry ( ) ;
UpdateTypeFilter ( ) ;
ClearCandidates ( ) ;
ApplyFilter ( ) ;
}
/// <summary>
/// 从 items 自身推父子关系:对每个节点,遍历它的 Children(也是 items 里的节点),
/// 记下“子 → 父”。引用相等即可(ControlTreeNode 未重写 Equals)。不依赖 MainForm。
/// </summary>
private void BuildParentMap ( )
{
foreach ( Entry entry in _entries )
{
ControlTreeNode node = NodeMapNodeInfo . AsNode ( entry . Item . Node ) ;
if ( node = = null ) continue ;
foreach ( ControlTreeNode child in node . Children )
{
Entry childEntry ;
if ( child ! = null & & _entryByNode . TryGetValue ( child , out childEntry ) )
_parentOf [ child ] = entry ;
}
}
}
/// <summary>按当前选中节点(引用优先、Path 回退)在新快照里重新定位 _currentEntry。</summary>
private void ResolveCurrentEntry ( )
{
Entry found ;
_currentEntry = _currentNode ! = null & & _entryByNode . TryGetValue ( _currentNode , out found ) ? found : null ;
if ( _currentEntry = = null & & ! string . IsNullOrEmpty ( _currentPath ) ) _currentEntry = FindEntryByPath ( _currentPath ) ;
RebuildChain ( ) ;
}
/// <summary>按 Path 在快照里找条目(Node 为 null 时的回退)。</summary>
private Entry FindEntryByPath ( string path )
{
foreach ( Entry entry in _entries )
if ( string . Equals ( entry . Item . Path , path , StringComparison . Ordinal ) ) return entry ;
return null ;
}
/// <summary>重建“当前选中及其祖先”的 Node 引用集合(供加粗高亮)。</summary>
private void RebuildChain ( )
{
_chainNodes . Clear ( ) ;
Entry current = _currentEntry ;
int guard = 0 ;
while ( current ! = null & & guard + + < 100000 )
{
_chainNodes . Add ( current . Item . Node ! = null ? ( object ) current . Item . Node : current ) ;
Entry parent ;
if ( current . Item . Node = = null | | ! _parentOf . TryGetValue ( current . Item . Node , out parent ) ) break ;
current = parent ;
}
}
/// <summary>把 items 里出现的类型收集去重后推给侧栏下拉(保留用户已选类型)。</summary>
private void UpdateTypeFilter ( )
{
var types = new List < string > ( ) ;
foreach ( Entry entry in _entries )
if ( ! string . IsNullOrEmpty ( entry . Type ) & & ! types . Contains ( entry . Type ) ) types . Add ( entry . Type ) ;
types . Sort ( StringComparer . Ordinal ) ;
_actions . SetTypes ( types , _filterType ) ;
}
/// <summary>结束地图会话:清空快照、退出选取模式、隐藏所有表面并停表。</summary>
@@ -311,6 +592,15 @@ namespace pacs_win_uia.UI
_version + + ;
_anchor = IntPtr . Zero ;
_entries . Clear ( ) ;
_entryByNode . Clear ( ) ;
_parentOf . Clear ( ) ;
_visibleEntries . Clear ( ) ;
_chainNodes . Clear ( ) ;
_chainActive = false ;
_currentNode = null ;
_currentPath = null ;
_currentEntry = null ;
_hoverEntry = null ;
SetSelectionMode ( false ) ;
HideSurfaces ( ) ;
_timer . Stop ( ) ;
@@ -389,11 +679,12 @@ namespace pacs_win_uia.UI
}
if ( ! Visible ) Show ( ) ;
if ( ! _actions . Visible ) _actions . Show ( ) ;
if ( _selectionMode )
{
// 每帧只读一次 Cursor.Position(本地),既驱动选取候选,也驱动悬停标签/信息面板。
NativeMethods . POINT point ;
if ( NativeMethods . GetCursorPos ( out point ) ) UpdateCandidates ( point . ToDrawingPoint ( ) , false ) ;
}
bool hasCursor = NativeMethods . GetCursorPos ( out point ) ;
Point cursor = hasCursor ? point . ToDrawingPoint ( ) : Point . Empty ;
if ( _selectionMode & & hasCursor ) UpdateCandidates ( cursor , false ) ;
if ( hasCursor ) UpdateHover ( cursor ) ;
Invalidate ( ) ;
}
}
@@ -445,7 +736,7 @@ namespace pacs_win_uia.UI
Point local = new Point ( point . X - bounds . Left , point . Y - bounds . Top ) ;
var hits = new List < Entry > ( ) ;
foreach ( Entry entry in _entries )
if ( entry . Offset . Contains ( local ) ) hits . Add ( entry ) ;
if ( entry . Visible & & entry . Offset . Contains ( local ) ) hits . Add ( entry ) ;
hits . Sort ( delegate ( Entry left , Entry right )
{
int size = ( ( long ) left . Offset . Width * left . Offset . Height ) . CompareTo ( ( long ) right . Offset . Width * right . Offset . Height ) ;
@@ -473,6 +764,236 @@ namespace pacs_win_uia.UI
get { return _candidates . Count = = 0 ? null : _candidates [ _candidateIndex ] ; }
}
// ------------------------------------------------------------------ 过滤 / 搜索
/// <summary>按当前搜索词/类型/“仅选中”重算每条目的可见性,并刷新可见列表与祖先链。</summary>
private void ApplyFilter ( )
{
_visibleEntries . Clear ( ) ;
foreach ( Entry entry in _entries )
{
entry . Visible = MatchesFilter ( entry ) ;
if ( entry . Visible ) _visibleEntries . Add ( entry ) ;
}
if ( _hoverEntry ! = null & & ! _hoverEntry . Visible ) _hoverEntry = null ;
RebuildChain ( ) ;
// 祖先链里只要有可见项就进入“其余框变淡”模式,避免整条链被过滤后还变淡。
_chainActive = false ;
if ( _chainNodes . Count > 0 )
foreach ( Entry entry in _visibleEntries )
if ( IsInChain ( entry ) ) { _chainActive = true ; break ; }
_actions . SetCounts ( _visibleEntries . Count , _entries . Count ) ;
Invalidate ( ) ;
}
/// <summary>单个条目是否通过当前过滤(搜索词/类型/仅选中链路)。</summary>
private bool MatchesFilter ( Entry entry )
{
if ( ! string . IsNullOrEmpty ( _filterType ) & & ! string . Equals ( entry . Type , _filterType , StringComparison . Ordinal ) )
return false ;
if ( ! string . IsNullOrEmpty ( _filterText ) )
{
string q = _filterText ;
if ( ! ContainsText ( entry . Name , q ) & & ! ContainsText ( entry . AutomationId , q ) & &
! ContainsText ( entry . ClassName , q ) & & ! ContainsText ( entry . Item . Path , q ) & &
! ContainsText ( entry . Type , q ) )
return false ;
}
if ( _filterSelectedOnly & & _currentEntry ! = null )
{
// 只保留:当前项本身 + 它的祖先 + 它的后代。
if ( entry ! = _currentEntry & & ! IsAncestorOf ( entry , _currentEntry ) & &
! IsAncestorOf ( _currentEntry , entry ) )
return false ;
}
return true ;
}
/// <summary>不区分大小写的包含匹配(空文本/空查询都视为不匹配)。</summary>
private static bool ContainsText ( string text , string query )
{
return ! string . IsNullOrEmpty ( text ) & & text . IndexOf ( query , StringComparison . OrdinalIgnoreCase ) > = 0 ;
}
/// <summary>ancestor 是否是 node 的祖先(不含自身)。用 _parentOf 在内存里上溯。</summary>
private bool IsAncestorOf ( Entry ancestor , Entry node )
{
if ( ancestor = = null | | node = = null ) return false ;
Entry current = node ;
int guard = 0 ;
while ( guard + + < 100000 )
{
Entry parent ;
if ( current . Item . Node = = null | | ! _parentOf . TryGetValue ( current . Item . Node , out parent ) | | parent = = null )
return false ;
if ( parent = = ancestor ) return true ;
current = parent ;
}
return false ;
}
/// <summary>条目是否属于当前选中及其祖先链。</summary>
private bool IsInChain ( Entry entry )
{
return entry . Item . Node ! = null ? _chainNodes . Contains ( entry . Item . Node ) : _chainNodes . Contains ( entry ) ;
}
// ------------------------------------------------------------------ 选中 / 导航
/// <summary>把某条目设为“当前选中”:记为当前并刷新过滤/信息面板/祖先链。</summary>
private void SetCurrentEntry ( Entry target )
{
if ( target = = null ) return ;
_currentEntry = target ;
_currentNode = target . Item . Node ;
_currentPath = target . Item . Path ;
ApplyFilter ( ) ; // “仅选中及其祖先/后代”依赖当前项
UpdatePanelInfo ( _hoverEntry ! = null ? _hoverEntry : target ) ;
}
/// <summary>导航/按钮统一入口:设为当前并发出 NodeSelected(外部负责选中控件树/刷新属性)。</summary>
private void PublishSelection ( Entry target )
{
if ( target = = null | | _disposing ) return ;
SetCurrentEntry ( target ) ;
RaiseSelected ( target , CenterOf ( target ) ) ;
}
/// <summary>发出 NodeSelected( MainForm 收到后会在控件树里选中该节点)。</summary>
private void RaiseSelected ( Entry entry , Point screenPoint )
{
EventHandler < NodeMapSelectionEventArgs > handler = NodeSelected ;
if ( handler = = null ) return ;
var args = new NodeMapSelectionEventArgs ( entry . Index , entry . Item , screenPoint ) ;
handler ( this , args ) ;
}
/// <summary>条目当前屏幕矩形的中心点(用于导航时的事件坐标)。</summary>
private Point CenterOf ( Entry entry )
{
Rectangle screen = entry . Offset ;
screen . Offset ( _anchorBounds . Left , _anchorBounds . Top ) ;
return new Point ( screen . Left + screen . Width / 2 , screen . Top + screen . Height / 2 ) ;
}
/// <summary>「父节点」:从 items 推出的父映射上溯一层;无父时给提示。</summary>
private void GoParent ( )
{
Entry start = _currentEntry ? ? CurrentCandidate ;
if ( start = = null ) { _actions . SetHint ( "先把鼠标移到或点选一个框,再点「父节点」。" ) ; return ; }
Entry parent ;
if ( start . Item . Node = = null | | ! _parentOf . TryGetValue ( start . Item . Node , out parent ) | | parent = = null )
{
_actions . SetHint ( "已经是根节点,没有父节点。" ) ;
return ;
}
PublishSelection ( parent ) ;
}
/// <summary>「上一个/下一个」:按 items 顺序(树的前序)在“可见条目”上移动。</summary>
private void GoSibling ( int direction )
{
if ( _visibleEntries . Count = = 0 )
{
_actions . SetHint ( "当前没有可移动的框(可能被搜索/过滤隐藏了)。" ) ;
return ;
}
int position = _currentEntry = = null ? - 1 : _visibleEntries . IndexOf ( _currentEntry ) ;
if ( position < 0 )
{
// 当前项不在可见集合里:按原始索引朝移动方向找第一个可见项。
int anchorIndex = _currentEntry = = null ? - 1 : _currentEntry . Index ;
if ( direction > 0 )
{
position = _visibleEntries . Count - 1 ;
for ( int i = 0 ; i < _visibleEntries . Count ; i + + )
if ( _visibleEntries [ i ] . Index > anchorIndex ) { position = i ; break ; }
}
else
{
position = 0 ;
for ( int i = _visibleEntries . Count - 1 ; i > = 0 ; i - - )
if ( _visibleEntries [ i ] . Index < anchorIndex ) { position = i ; break ; }
}
}
else
{
position = ( ( position + direction ) % _visibleEntries . Count + _visibleEntries . Count ) % _visibleEntries . Count ;
}
PublishSelection ( _visibleEntries [ position ] ) ;
}
/// <summary>一键复制当前(鼠标下优先)框的 path。</summary>
private void CopyCurrentPath ( )
{
Entry target = _hoverEntry ? ? _currentEntry ;
if ( target = = null | | string . IsNullOrEmpty ( target . Item . Path ) )
{
_actions . SetHint ( "没有可复制的 path。" ) ;
return ;
}
try
{
Clipboard . SetText ( target . Item . Path ) ;
_actions . SetHint ( "已复制 path: " + target . Item . Path ) ;
}
catch ( Exception ex ) { _actions . SetHint ( "复制失败:" + ex . Message ) ; }
}
// ------------------------------------------------------------------ 悬停 / 信息面板
/// <summary>按鼠标屏幕点更新悬停项(只在内存里命中测试,且要求点在本覆盖层可见区域内)。</summary>
private void UpdateHover ( Point screenPoint )
{
Entry hover = null ;
bool overPanel = _actions . Visible & & _actions . Bounds . Contains ( screenPoint ) ;
if ( ! overPanel & & _anchorBounds . Contains ( screenPoint ) )
{
Point local = new Point ( screenPoint . X - _anchorBounds . Left , screenPoint . Y - _anchorBounds . Top ) ;
if ( Region ! = null & & Region . IsVisible ( local ) ) hover = HitTest ( screenPoint ) ;
}
if ( hover ! = _hoverEntry )
{
_hoverEntry = hover ;
UpdatePanelInfo ( hover ! = null ? hover : _currentEntry ) ;
Invalidate ( ) ;
}
}
/// <summary>取鼠标点下最具体(面积最小)的可见条目;没有则 null。</summary>
private Entry HitTest ( Point screenPoint )
{
Point local = new Point ( screenPoint . X - _anchorBounds . Left , screenPoint . Y - _anchorBounds . Top ) ;
Entry best = null ;
long bestArea = long . MaxValue ;
foreach ( Entry entry in _entries )
{
if ( ! entry . Visible | | ! entry . Offset . Contains ( local ) ) continue ;
long area = ( long ) entry . Offset . Width * entry . Offset . Height ;
if ( area < bestArea ) { bestArea = area ; best = entry ; }
}
return best ;
}
/// <summary>把条目的类型/名称/大小/位置/路径写到侧栏信息面板(鼠标下优先)。</summary>
private void UpdatePanelInfo ( Entry entry )
{
if ( entry = = null )
{
_actions . SetInfo ( "把鼠标移到某个框上,或点框/用「父节点·上一个·下一个」选中一个节点。" ) ;
return ;
}
Rectangle screen = entry . Offset ;
screen . Offset ( _anchorBounds . Left , _anchorBounds . Top ) ;
var text = new StringBuilder ( ) ;
text . Append ( "类型: " ) . Append ( string . IsNullOrEmpty ( entry . Type ) ? "(未知)" : entry . Type ) ;
text . Append ( " 名称: " ) . Append ( string . IsNullOrEmpty ( entry . Name ) ? "(无名称)" : entry . Name ) . Append ( '\n' ) ;
text . Append ( "大小: " ) . Append ( screen . Width ) . Append ( " × " ) . Append ( screen . Height ) ;
text . Append ( " 位置: (" ) . Append ( screen . Left ) . Append ( ", " ) . Append ( screen . Top ) . Append ( ')' ) . Append ( '\n' ) ;
text . Append ( "路径: " ) . Append ( string . IsNullOrEmpty ( entry . Item . Path ) ? "(无)" : entry . Item . Path ) ;
_actions . SetInfo ( text . ToString ( ) ) ;
}
/// <summary>把鼠标消息映射成按键位掩码(左1/右2/中4/X1=8/X2=16),并输出按下/抬起标志。</summary>
private static int ButtonMask ( int message , uint data , out bool down , out bool up )
{
@@ -508,13 +1029,15 @@ namespace pacs_win_uia.UI
if ( mask = = 1 & & picked ! = null & & _pressedVersion = = _version & & CanIntercept ( point ) )
{
int version = _version ;
var args = new NodeMapSelectionEventArgs ( picked . Index , picked . Item , point ) ;
Entry target = picked ;
Point screenPoint = point ;
Post ( delegate
{
if ( _selectionMode & & version = = _version )
{
EventHandler < NodeMapSelectionEventArgs > handler = NodeSelected ;
if ( handler ! = null ) handler ( this , args ) ;
// 点框 = 选中该节点:覆盖层记为“当前”(加粗高亮祖先链),再通知外部。
SetCurrentEntry ( target ) ;
RaiseSelected ( target , screenPoint ) ;
}
} ) ;
}
@@ -599,18 +1122,73 @@ namespace pacs_win_uia.UI
if ( force ) { _suppressedButtons = 0 ; _escapeSuppressed = false ; }
}
/// <summary>把红框画进可见区域(用 Region 裁剪);选取模式下高亮当前候选。</summary>
/// <summary>
/// 把框画进可见区域(用 Region 裁剪)。有当前选中时:祖先链加粗高亮、其余框变淡;
/// 选取模式下另用蓝色标出滚轮/鼠标下的候选;最后只在鼠标下的框旁画文字标签。
/// </summary>
protected override void OnPaint ( PaintEventArgs e )
{
base . OnPaint ( e ) ;
if ( ! _shown | | Region = = null | | ! WindowVisibility . IsForegroundWindow ( _anchor ) ) return ;
e . Graphics . SmoothingMode = SmoothingMode . None ;
e . Graphics . SetClip ( Region , CombineMode . Intersect ) ;
using ( var pen = new Pen ( Color . Red , 1.6F ) )
foreach ( Entry entry in _entries ) e . Graphics . DrawRectangle ( pen , entry . Offset ) ;
bool chainMode = _chainActive ;
using ( var normal = new Pen ( Color . Red , 1.6F ) )
using ( var dim = new Pen ( Color . FromArgb ( 255 , 214 , 128 , 128 ) , 1.4F ) )
{
foreach ( Entry entry in _entries )
{
if ( ! entry . Visible ) continue ;
if ( chainMode & & IsInChain ( entry ) ) continue ; // 祖先链稍后统一加粗绘制
e . Graphics . DrawRectangle ( chainMode ? dim : normal , entry . Offset ) ;
}
}
if ( chainMode )
{
using ( var ancestor = new Pen ( Color . FromArgb ( 255 , 255 , 140 , 0 ) , 3F ) ) // 祖先:橙
using ( var current = new Pen ( Color . DodgerBlue , 3F ) ) // 当前:蓝
{
foreach ( Entry entry in _entries )
{
if ( ! entry . Visible | | ! IsInChain ( entry ) ) continue ;
e . Graphics . DrawRectangle ( entry = = _currentEntry ? current : ancestor , entry . Offset ) ;
}
}
}
// 选取模式下滚轮/鼠标下的候选(不与“当前”重复时再画一层蓝)。
Entry selected = CurrentCandidate ;
if ( _selectionMode & & selected ! = null )
if ( _selectionMode & & selected ! = null & & selected ! = _currentEntry )
using ( var pen = new Pen ( Color . DodgerBlue , 3F ) ) e . Graphics . DrawRectangle ( pen , selected . Offset ) ;
if ( _showLabels & & _hoverEntry ! = null & & _hoverEntry . Visible ) DrawHoverLabel ( e . Graphics , _hoverEntry ) ;
}
/// <summary>在鼠标下的框旁边画一个小标签:类型:名称 宽×高(用实色,避免与透明键混色)。</summary>
private void DrawHoverLabel ( Graphics g , Entry entry )
{
string type = string . IsNullOrEmpty ( entry . Type ) ? "?" : entry . Type ;
string name = string . IsNullOrEmpty ( entry . Name ) ? "(无名称)" : entry . Name ;
string text = type + ":" + name + " " + entry . Offset . Width + "× " + entry . Offset . Height ;
using ( var font = new Font ( "Microsoft YaHei UI" , 8.5F ) )
using ( var back = new SolidBrush ( Color . FromArgb ( 255 , 255 , 249 , 196 ) ) )
using ( var border = new Pen ( Color . FromArgb ( 255 , 160 , 120 , 0 ) , 1F ) )
using ( var fore = new SolidBrush ( Color . FromArgb ( 255 , 30 , 30 , 30 ) ) )
{
SizeF size = g . MeasureString ( text , font ) ;
int width = ( int ) Math . Ceiling ( size . Width ) + 8 ;
int height = ( int ) Math . Ceiling ( size . Height ) + 4 ;
int x = entry . Offset . Right + 2 ;
int y = entry . Offset . Top ;
if ( x + width > ClientSize . Width ) x = entry . Offset . Left - width - 2 ; // 右侧放不下改放左侧
if ( x < 0 ) x = 0 ;
if ( y + height > ClientSize . Height ) y = ClientSize . Height - height ;
if ( y < 0 ) y = 0 ;
var rect = new Rectangle ( x , y , width , height ) ;
g . FillRectangle ( back , rect ) ;
g . DrawRectangle ( border , rect ) ;
g . DrawString ( text , font , fore , x + 4 , y + 2 ) ;
}
}
/// <summary>取窗口屏幕矩形;句柄无效、调用失败或零尺寸返回 false。</summary>
@@ -639,6 +1217,12 @@ namespace pacs_win_uia.UI
ReplaceRegion ( null ) ;
_entries . Clear ( ) ;
_candidates . Clear ( ) ;
_entryByNode . Clear ( ) ;
_parentOf . Clear ( ) ;
_visibleEntries . Clear ( ) ;
_chainNodes . Clear ( ) ;
_currentEntry = null ;
_hoverEntry = null ;
NodeSelected = null ;
SelectionModeChanged = null ;
MapExited = null ;
@@ -648,19 +1232,61 @@ namespace pacs_win_uia.UI
}
// Kept in the already compiled overlay source so integration requires no csproj changes.
/// <summary>地图旁的小操作面板:切换选取模式、退出绘制、显示当前候选。</summary>
/// <summary>
/// 地图旁的小操作面板:切换/退出选取、父节点·上一个·下一个导航、复制 path、搜索/类型过滤、
/// 选中信息与候选显示。面板是 WS_EX_NOACTIVATE(不抢焦点、不激活),但可正常接收鼠标点击;
/// 搜索框/下拉用 NodeMapInputFocus.ForceFocus 抢键盘焦点以支持输入。
/// </summary>
internal sealed class NodeMapActionsForm : Form
{
/// <summary>类型下拉里代表“全部类型”的项。</summary>
private const string AllTypes = "(全部类型)" ;
/// <summary>“点击框获取path / 选取中”按钮。</summary>
private readonly Button _select ;
/// <summary>退出绘制按钮。</summary>
private readonly Button _exit ;
/// <summary>父节点 / 上一个 / 下一个 导航按钮。</summary>
private readonly Button _parent , _prev , _next ;
/// <summary>复制当前框 path 按钮。</summary>
private readonly Button _copy ;
/// <summary>搜索框。</summary>
private readonly TextBox _search ;
/// <summary>控件类型下拉。</summary>
private readonly ComboBox _type ;
/// <summary>“仅选中及其祖先/后代”勾选。</summary>
private readonly CheckBox _selectedOnly ;
/// <summary>“显示标签”勾选。</summary>
private readonly CheckBox _labelsCheck ;
/// <summary>当前框信息标签(类型/名称/大小/位置/路径)。</summary>
private readonly Label _info ;
/// <summary>候选信息标签(候选 i/n 与 path)。</summary>
private readonly Label _candidate ;
/// <summary>框数量标签(可见/总数)。</summary>
private readonly Label _counts ;
/// <summary>操作提示标签。</summary>
private readonly Label _hint ;
/// <summary>程序性改控件时抑制事件回传,避免重入。</summary>
private bool _suppress ;
/// <summary>请求切换选取模式。</summary>
internal event EventHandler ToggleSelection ;
/// <summary>请求退出地图。</summary>
internal event EventHandler ExitMap ;
/// <summary>请求上溯父节点。</summary>
internal event EventHandler ParentRequested ;
/// <summary>请求移动到上一个节点(树前序)。</summary>
internal event EventHandler PrevRequested ;
/// <summary>请求移动到下一个节点(树前序)。</summary>
internal event EventHandler NextRequested ;
/// <summary>请求复制当前框的 path。</summary>
internal event EventHandler CopyPathRequested ;
/// <summary>搜索词/类型/仅选中发生变化。</summary>
internal event EventHandler FilterChanged ;
/// <summary>“显示标签”勾选发生变化。</summary>
internal event EventHandler LabelsChanged ;
/// <summary>构建面板控件并接线按钮点击 (选取切换失败时在标签上 提示)。</summary>
/// <summary>构建面板控件并接线(选取切换失败时在提示里说明 )。</summary>
internal NodeMapActionsForm ( )
{
FormBorderStyle = FormBorderStyle . None ;
@@ -668,25 +1294,85 @@ namespace pacs_win_uia.UI
StartPosition = FormStartPosition . Manual ;
AutoScaleMode = AutoScaleMode . None ;
TopMost = true ;
ClientSize = new Size ( 170 , 112 ) ;
ClientSize = new Size ( 296 , 436 ) ;
BackColor = SystemColors . Control ;
_select = new Button { Text = "点击框获取path" , Bounds = new Rectangle ( 5 , 5 , 160 , 32 ) , TabStop = false } ;
var exit = new Button { Text = "退出绘制 " , Bounds = new Rectangle ( 5 , 41 , 160 , 30 ) , TabStop = false } ;
_candidate = new Label { Bounds = new Rectangle ( 5 , 76 , 160 , 32 ) , AutoEllipsis = true , Text = "选取后滚轮切换重叠节点" } ;
_select = new Button { Text = "点击框获取path " , Bounds = new Rectangle ( 6 , 6 , 284 , 30 ) , TabStop = false } ;
_exit = new Button { Text = "退出绘制" , Bounds = new Rectangle ( 6 , 40 , 284 , 26 ) , TabStop = false } ;
_parent = new Button { Text = "父节点" , Bounds = new Rectangle ( 6 , 72 , 90 , 26 ) , TabStop = false } ;
_prev = new Button { Text = "上一个" , Bounds = new Rectangle ( 100 , 72 , 90 , 26 ) , TabStop = false } ;
_next = new Button { Text = "下一个" , Bounds = new Rectangle ( 194 , 72 , 96 , 26 ) , TabStop = false } ;
_copy = new Button { Text = "复制当前框 Path" , Bounds = new Rectangle ( 6 , 102 , 284 , 26 ) , TabStop = false } ;
var searchLabel = new Label { Text = "搜索" , Bounds = new Rectangle ( 6 , 134 , 38 , 24 ) , TextAlign = ContentAlignment . MiddleLeft } ;
_search = new TextBox { Bounds = new Rectangle ( 46 , 134 , 244 , 24 ) } ;
_type = new ComboBox { Bounds = new Rectangle ( 6 , 164 , 284 , 24 ) , DropDownStyle = ComboBoxStyle . DropDownList } ;
_type . Items . Add ( AllTypes ) ;
_type . SelectedIndex = 0 ;
_selectedOnly = new CheckBox { Text = "仅选中及其祖先/后代" , Bounds = new Rectangle ( 4 , 192 , 182 , 24 ) } ;
_labelsCheck = new CheckBox { Text = "显示标签" , Bounds = new Rectangle ( 190 , 192 , 100 , 24 ) , Checked = true } ;
_info = new Label { Bounds = new Rectangle ( 6 , 220 , 284 , 90 ) , BorderStyle = BorderStyle . FixedSingle ,
AutoEllipsis = true , Text = "把鼠标移到某个框上,或点框/用「父节点·上一个·下一个」选中一个节点。" } ;
_candidate = new Label { Bounds = new Rectangle ( 6 , 314 , 284 , 44 ) , AutoEllipsis = true , Text = "选取后滚轮切换重叠节点" } ;
_counts = new Label { Bounds = new Rectangle ( 6 , 360 , 284 , 18 ) , Text = "框: 0 / 0" } ;
_hint = new Label { Bounds = new Rectangle ( 6 , 380 , 284 , 50 ) , AutoEllipsis = true , Text = "" } ;
_select . Click + = delegate
{
try
try { Raise ( ToggleSelection ) ; }
catch ( Win32Exception ) { SetHint ( "钩子安装失败,未进入选取。" ) ; }
} ;
_exit . Click + = delegate { Raise ( ExitMap ) ; } ;
_parent . Click + = delegate { Raise ( ParentRequested ) ; } ;
_prev . Click + = delegate { Raise ( PrevRequested ) ; } ;
_next . Click + = delegate { Raise ( NextRequested ) ; } ;
_copy . Click + = delegate { Raise ( CopyPathRequested ) ; } ;
// 搜索框/下拉在 NOACTIVATE 窗口上默认拿不到键盘焦点,点按时强制聚焦(不激活本窗口)。
_search . MouseDown + = delegate { NodeMapInputFocus . ForceFocus ( _search ) ; } ;
_type . MouseDown + = delegate { NodeMapInputFocus . ForceFocus ( _type ) ; } ;
_search . TextChanged + = delegate { if ( ! _suppress ) Raise ( FilterChanged ) ; } ;
_type . SelectedIndexChanged + = delegate { if ( ! _suppress ) Raise ( FilterChanged ) ; } ;
_selectedOnly . CheckedChanged + = delegate { if ( ! _suppress ) Raise ( FilterChanged ) ; } ;
_labelsCheck . CheckedChanged + = delegate { if ( ! _suppress ) Raise ( LabelsChanged ) ; } ;
Controls . Add ( _select ) ;
Controls . Add ( _exit ) ;
Controls . Add ( _parent ) ;
Controls . Add ( _prev ) ;
Controls . Add ( _next ) ;
Controls . Add ( _copy ) ;
Controls . Add ( searchLabel ) ;
Controls . Add ( _search ) ;
Controls . Add ( _type ) ;
Controls . Add ( _selectedOnly ) ;
Controls . Add ( _labelsCheck ) ;
Controls . Add ( _info ) ;
Controls . Add ( _candidate ) ;
Controls . Add ( _counts ) ;
Controls . Add ( _hint ) ;
}
/// <summary>安全地触发事件(无订阅者则忽略)。</summary>
private void Raise ( EventHandler handler )
{
EventHandler handler = ToggleSelection ;
if ( handler ! = null ) handler ( this , EventArgs . Empty ) ;
}
catch ( Win32Exception ) { _candidate . Text = "钩子安装失败,未进入选取" ; }
} ;
exit . Click + = delegate { EventHandler handler = ExitMap ; if ( handler ! = null ) handler ( this , EventArgs . Empty ) ; } ;
Controls . Add ( _select ) ;
Controls . Add ( exit ) ;
Controls . Add ( _candidate ) ;
/// <summary>搜索词。</summary>
internal string SearchText { get { return _search . Text ; } }
/// <summary>选中的类型(“(全部类型)”返回空串表示不过滤)。</summary>
internal string SelectedType
{
get
{
var selected = _type . SelectedItem as string ;
return string . IsNullOrEmpty ( selected ) | | selected = = AllTypes ? string . Empty : selected ;
}
}
/// <summary>是否只保留当前选中及其祖先/后代。</summary>
internal bool SelectedOnly { get { return _selectedOnly . Checked ; } }
/// <summary>是否显示悬停标签。</summary>
internal bool LabelsChecked { get { return _labelsCheck . Checked ; } }
/// <summary>显示时不激活窗口(面板可被点击但不抢键盘焦点)。</summary>
protected override bool ShowWithoutActivation { get { return true ; } }
/// <summary>扩展样式:工具窗口、不激活(但可接收鼠标点击)。</summary>
@@ -733,5 +1419,68 @@ namespace pacs_win_uia.UI
"候选 " + index + "/" + count + " · 滚轮切换\r\n" + ( path ? ? "点击返回节点索引" ) ;
if ( _candidate . Text ! = text ) _candidate . Text = text ;
}
/// <summary>刷新框数量标签(可见 / 总数)。</summary>
internal void SetCounts ( int visible , int total )
{
string text = "框: " + visible + " / " + total ;
if ( _counts . Text ! = text ) _counts . Text = text ;
}
/// <summary>刷新当前框信息标签。</summary>
internal void SetInfo ( string text )
{
if ( text = = null ) text = string . Empty ;
if ( _info . Text ! = text ) _info . Text = text ;
}
/// <summary>刷新操作提示。</summary>
internal void SetHint ( string text )
{
if ( text = = null ) text = string . Empty ;
if ( _hint . Text ! = text ) _hint . Text = text ;
}
/// <summary>外部同步“显示标签”勾选(不回调 LabelsChanged)。</summary>
internal void SetLabels ( bool on )
{
if ( _labelsCheck . Checked = = on ) return ;
_suppress = true ;
try { _labelsCheck . Checked = on ; }
finally { _suppress = false ; }
}
/// <summary>用 items 里收集到的类型刷新下拉(集合不变则不重建,避免闪烁/打断下拉)。</summary>
internal void SetTypes ( IList < string > types , string keep )
{
var desired = new List < string > ( ) ;
desired . Add ( AllTypes ) ;
if ( types ! = null ) foreach ( string type in types ) if ( ! string . IsNullOrEmpty ( type ) ) desired . Add ( type ) ;
bool same = _type . Items . Count = = desired . Count ;
for ( int i = 0 ; same & & i < desired . Count ; i + + )
same = string . Equals ( _type . Items [ i ] as string , desired [ i ] , StringComparison . Ordinal ) ;
string before = SelectedType ;
_suppress = true ;
try
{
if ( ! same )
{
_type . BeginUpdate ( ) ;
try
{
_type . Items . Clear ( ) ;
foreach ( string item in desired ) _type . Items . Add ( item ) ;
}
finally { _type . EndUpdate ( ) ; }
}
int index = 0 ;
if ( ! string . IsNullOrEmpty ( keep ) )
{
int found = _type . Items . IndexOf ( keep ) ;
if ( found > = 0 ) index = found ;
}
if ( _type . SelectedIndex ! = index ) _type . SelectedIndex = index ;
}
finally { _suppress = false ; }
// 选中的类型被程序性改掉(例如该类型已不在 items 里)时,通知覆盖层同步过滤状态。
if ( ! string . Equals ( before , SelectedType , StringComparison . Ordinal ) ) Raise ( FilterChanged ) ;
}
}
}