Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Core / CSharp / MS / Internal / Automation / ElementUtil.cs / 1 / ElementUtil.cs
//---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: // // History: // 07/16/2003 : BrendanM Ported to WCP // //--------------------------------------------------------------------------- using System; using System.Windows.Threading; using System.Threading; using System.Windows; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Automation; using System.Windows.Automation.Provider; using System.Windows.Automation.Peers; using System.Security; using MS.Win32; using MS.Internal.Media; using System.Runtime.InteropServices; using System.Globalization; using MS.Internal.PresentationCore; // SafeSecurityHelper using SR=MS.Internal.PresentationCore.SR; using SRID=MS.Internal.PresentationCore.SRID; namespace MS.Internal.Automation { // static class providing utility information for working with WCP elements internal class ElementUtil { //----------------------------------------------------- // // Constructors // //----------------------------------------------------- #region Constructors // static class, so use private ctor private ElementUtil() { } #endregion Constructors //------------------------------------------------------ // // Internal Methods // //----------------------------------------------------- #region Internal Methods internal static Visual GetParent( Visual el ) { return VisualTreeHelper.GetParent(el) as Visual; } internal static Visual GetFirstChild( Visual el ) { if (el == null) { return null; } return FindVisibleSibling ( el, 0, true ); } internal static Visual GetLastChild( Visual el ) { if (el == null) { return null; } return FindVisibleSibling ( el, el.InternalVisualChildrenCount - 1, false ); } // Warning: Method is O(N). See FindVisibleSibling function for more information. internal static Visual GetNextSibling( Visual el ) { // To get next/previous sibling, have to find out where we // are in our parent's children collection (ie. our siblings) Visual parent = VisualTreeHelper.GetParent(el) as Visual; // If parent is null, we're at root, so have no siblings if (parent == null) { return null; } return FindVisibleSibling ( parent, el, true /* Next */); } // Warning: Method is O(N). See FindVisibleSibling function for more information. internal static Visual GetPreviousSibling( Visual el ) { // To get next/previous sibling, have to find out where we // are in our parent's children collection (ie. our siblings) Visual parent = VisualTreeHelper.GetParent(el) as Visual; // If parent is null, we're at root, so have no siblings if (parent == null) { return null; } return FindVisibleSibling ( parent, el, false /* Previous */); } internal static Visual GetRoot( Visual el ) { // Keep moving up parent chain till we reach the top... Visual scan = el; for( ; ; ) { Visual test = VisualTreeHelper.GetParent(scan) as Visual; if( test == null ) break; scan = test; } return scan; } // Get bounding rectangle, in coords relative to root (not screen) internal static Rect GetLocalRect( UIElement element ) { // Get top-most visual. Visual parent = GetRoot( element ); // Get the points for the rectangle and transform them. double height = element.RenderSize.Height; double width = element.RenderSize.Width; Rect rect = new Rect(0, 0, width, height); GeneralTransform g = element.TransformToAncestor(parent); return g.TransformBounds(rect); } // Get bounding rectangle, relative to screen internal static Rect GetScreenRect( IntPtr hwnd, UIElement el ) { Rect rc = GetLocalRect( el ); // Map from local to screen coords... NativeMethods.RECT rcWin32 = new NativeMethods.RECT( (int) rc.Left, (int) rc.Top, (int) rc.Right, (int) rc.Bottom ); try { SafeSecurityHelper.TransformLocalRectToScreen(new HandleRef(null, hwnd), ref rcWin32); } catch (System.ComponentModel.Win32Exception) { return Rect.Empty; } rc = new Rect( rcWin32.left, rcWin32.top, rcWin32.right - rcWin32.left, rcWin32.bottom - rcWin32.top ); return rc; } // Get element at given point (screen coords) ////// Critical - accepts critical data. Hwnd was created under an elevation. /// - calls HwndSource.CriticalFromHwnd to get the HwndSource for this visual /// TreatAsSafe - returning an element is considered safe. /// - the hwndSource is not exposed. /// [SecurityCritical, SecurityTreatAsSafe ] internal static Visual GetElementFromPoint( IntPtr hwnd, Visual root, Point pointScreen ) { HwndSource hwndSource = HwndSource.CriticalFromHwnd(hwnd); if(hwndSource == null) return null; Point pointClient = PointUtil.ScreenToClient( pointScreen, hwndSource ); Point pointRoot = PointUtil.ClientToRoot(pointClient, hwndSource); PointHitTestResult result = VisualTreeUtils.AsNearestPointHitTestResult(VisualTreeHelper.HitTest(root, pointRoot)); Visual visual = (result != null) ? result.VisualHit : null; return visual; } // Ensures that an element is enabled; throws exception otherwise //[CodeAnalysis("AptcaMethodsShouldOnlyCallAptcaMethods")] //Tracking Bug: 29647 internal static void CheckEnabled(Visual visual) { UIElement el = visual as UIElement; if( el != null && ! el.IsEnabled ) { throw new ElementNotEnabledException(); } } //[CodeAnalysis("AptcaMethodsShouldOnlyCallAptcaMethods")] //Tracking Bug: 29647 internal static object Invoke(AutomationPeer peer, DispatcherOperationCallback work, object arg) { Dispatcher dispatcher = peer.Dispatcher; // Null dispatcher likely means the visual is in bad shape! if( dispatcher == null ) { throw new ElementNotAvailableException(); } Exception remoteException = null; bool completed = false; object retVal = dispatcher.Invoke( DispatcherPriority.Send, TimeSpan.FromMinutes(3), (DispatcherOperationCallback) delegate(object unused) { try { return work(arg); } catch(Exception e) { remoteException = e; return null; } catch //for non-CLS Compliant exceptions { remoteException = null; return null; } finally { completed = true; } }, null); if(completed) { if(remoteException != null) { throw remoteException; } } else { bool dispatcherInShutdown = dispatcher.HasShutdownStarted; if(dispatcherInShutdown) { throw new InvalidOperationException(SR.Get(SRID.AutomationDispatcherShutdown)); } else { throw new TimeoutException(SR.Get(SRID.AutomationTimeout)); } } return retVal; } #endregion Internal Methods //------------------------------------------------------ // // Private Methods // //------------------------------------------------------ // private static Visual FindVisibleSibling ( Visual parent, int start, bool searchForwards) { int index = start; int childrenCount = parent.InternalVisualChildrenCount; while ( index >= 0 && index < childrenCount ) { Visual sibling = parent.InternalGetVisualChild(index); // if its visible or something other than a UIElement keep it if ( !(sibling is UIElement) || (((UIElement)sibling).Visibility == Visibility.Visible ) ) return sibling; index += searchForwards ? 1 : -1; } return null; } // private static Visual FindVisibleSibling(Visual parent, Visual child, bool searchForwards) { // // First we figure out the index of the specified child Visual. This is why the runtime // of this method is O(n). int childrenCount = parent.InternalVisualChildrenCount; int childIndex; for (childIndex = 0; childIndex < childrenCount; childIndex++) { Visual current = parent.InternalGetVisualChild(childIndex); if (current == child) { // Found the child. break; } } // // Now that we have the child index, we can go and lookup the sibling. if(searchForwards) return FindVisibleSibling(parent, childIndex+1, searchForwards); // (FindVisibleSibling can deal with out of range indices). else return FindVisibleSibling(parent, childIndex-1, searchForwards); // (FindVisibleSibling can deal with out of range indices). } //----------------------------------------------------- // // Private Fields // //------------------------------------------------------ #region Private Fields // static class, so no private fields #endregion Private Fields } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: // // History: // 07/16/2003 : BrendanM Ported to WCP // //--------------------------------------------------------------------------- using System; using System.Windows.Threading; using System.Threading; using System.Windows; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Automation; using System.Windows.Automation.Provider; using System.Windows.Automation.Peers; using System.Security; using MS.Win32; using MS.Internal.Media; using System.Runtime.InteropServices; using System.Globalization; using MS.Internal.PresentationCore; // SafeSecurityHelper using SR=MS.Internal.PresentationCore.SR; using SRID=MS.Internal.PresentationCore.SRID; namespace MS.Internal.Automation { // static class providing utility information for working with WCP elements internal class ElementUtil { //----------------------------------------------------- // // Constructors // //----------------------------------------------------- #region Constructors // static class, so use private ctor private ElementUtil() { } #endregion Constructors //------------------------------------------------------ // // Internal Methods // //----------------------------------------------------- #region Internal Methods internal static Visual GetParent( Visual el ) { return VisualTreeHelper.GetParent(el) as Visual; } internal static Visual GetFirstChild( Visual el ) { if (el == null) { return null; } return FindVisibleSibling ( el, 0, true ); } internal static Visual GetLastChild( Visual el ) { if (el == null) { return null; } return FindVisibleSibling ( el, el.InternalVisualChildrenCount - 1, false ); } // Warning: Method is O(N). See FindVisibleSibling function for more information. internal static Visual GetNextSibling( Visual el ) { // To get next/previous sibling, have to find out where we // are in our parent's children collection (ie. our siblings) Visual parent = VisualTreeHelper.GetParent(el) as Visual; // If parent is null, we're at root, so have no siblings if (parent == null) { return null; } return FindVisibleSibling ( parent, el, true /* Next */); } // Warning: Method is O(N). See FindVisibleSibling function for more information. internal static Visual GetPreviousSibling( Visual el ) { // To get next/previous sibling, have to find out where we // are in our parent's children collection (ie. our siblings) Visual parent = VisualTreeHelper.GetParent(el) as Visual; // If parent is null, we're at root, so have no siblings if (parent == null) { return null; } return FindVisibleSibling ( parent, el, false /* Previous */); } internal static Visual GetRoot( Visual el ) { // Keep moving up parent chain till we reach the top... Visual scan = el; for( ; ; ) { Visual test = VisualTreeHelper.GetParent(scan) as Visual; if( test == null ) break; scan = test; } return scan; } // Get bounding rectangle, in coords relative to root (not screen) internal static Rect GetLocalRect( UIElement element ) { // Get top-most visual. Visual parent = GetRoot( element ); // Get the points for the rectangle and transform them. double height = element.RenderSize.Height; double width = element.RenderSize.Width; Rect rect = new Rect(0, 0, width, height); GeneralTransform g = element.TransformToAncestor(parent); return g.TransformBounds(rect); } // Get bounding rectangle, relative to screen internal static Rect GetScreenRect( IntPtr hwnd, UIElement el ) { Rect rc = GetLocalRect( el ); // Map from local to screen coords... NativeMethods.RECT rcWin32 = new NativeMethods.RECT( (int) rc.Left, (int) rc.Top, (int) rc.Right, (int) rc.Bottom ); try { SafeSecurityHelper.TransformLocalRectToScreen(new HandleRef(null, hwnd), ref rcWin32); } catch (System.ComponentModel.Win32Exception) { return Rect.Empty; } rc = new Rect( rcWin32.left, rcWin32.top, rcWin32.right - rcWin32.left, rcWin32.bottom - rcWin32.top ); return rc; } // Get element at given point (screen coords) ////// Critical - accepts critical data. Hwnd was created under an elevation. /// - calls HwndSource.CriticalFromHwnd to get the HwndSource for this visual /// TreatAsSafe - returning an element is considered safe. /// - the hwndSource is not exposed. /// [SecurityCritical, SecurityTreatAsSafe ] internal static Visual GetElementFromPoint( IntPtr hwnd, Visual root, Point pointScreen ) { HwndSource hwndSource = HwndSource.CriticalFromHwnd(hwnd); if(hwndSource == null) return null; Point pointClient = PointUtil.ScreenToClient( pointScreen, hwndSource ); Point pointRoot = PointUtil.ClientToRoot(pointClient, hwndSource); PointHitTestResult result = VisualTreeUtils.AsNearestPointHitTestResult(VisualTreeHelper.HitTest(root, pointRoot)); Visual visual = (result != null) ? result.VisualHit : null; return visual; } // Ensures that an element is enabled; throws exception otherwise //[CodeAnalysis("AptcaMethodsShouldOnlyCallAptcaMethods")] //Tracking Bug: 29647 internal static void CheckEnabled(Visual visual) { UIElement el = visual as UIElement; if( el != null && ! el.IsEnabled ) { throw new ElementNotEnabledException(); } } //[CodeAnalysis("AptcaMethodsShouldOnlyCallAptcaMethods")] //Tracking Bug: 29647 internal static object Invoke(AutomationPeer peer, DispatcherOperationCallback work, object arg) { Dispatcher dispatcher = peer.Dispatcher; // Null dispatcher likely means the visual is in bad shape! if( dispatcher == null ) { throw new ElementNotAvailableException(); } Exception remoteException = null; bool completed = false; object retVal = dispatcher.Invoke( DispatcherPriority.Send, TimeSpan.FromMinutes(3), (DispatcherOperationCallback) delegate(object unused) { try { return work(arg); } catch(Exception e) { remoteException = e; return null; } catch //for non-CLS Compliant exceptions { remoteException = null; return null; } finally { completed = true; } }, null); if(completed) { if(remoteException != null) { throw remoteException; } } else { bool dispatcherInShutdown = dispatcher.HasShutdownStarted; if(dispatcherInShutdown) { throw new InvalidOperationException(SR.Get(SRID.AutomationDispatcherShutdown)); } else { throw new TimeoutException(SR.Get(SRID.AutomationTimeout)); } } return retVal; } #endregion Internal Methods //------------------------------------------------------ // // Private Methods // //------------------------------------------------------ // private static Visual FindVisibleSibling ( Visual parent, int start, bool searchForwards) { int index = start; int childrenCount = parent.InternalVisualChildrenCount; while ( index >= 0 && index < childrenCount ) { Visual sibling = parent.InternalGetVisualChild(index); // if its visible or something other than a UIElement keep it if ( !(sibling is UIElement) || (((UIElement)sibling).Visibility == Visibility.Visible ) ) return sibling; index += searchForwards ? 1 : -1; } return null; } // private static Visual FindVisibleSibling(Visual parent, Visual child, bool searchForwards) { // // First we figure out the index of the specified child Visual. This is why the runtime // of this method is O(n). int childrenCount = parent.InternalVisualChildrenCount; int childIndex; for (childIndex = 0; childIndex < childrenCount; childIndex++) { Visual current = parent.InternalGetVisualChild(childIndex); if (current == child) { // Found the child. break; } } // // Now that we have the child index, we can go and lookup the sibling. if(searchForwards) return FindVisibleSibling(parent, childIndex+1, searchForwards); // (FindVisibleSibling can deal with out of range indices). else return FindVisibleSibling(parent, childIndex-1, searchForwards); // (FindVisibleSibling can deal with out of range indices). } //----------------------------------------------------- // // Private Fields // //------------------------------------------------------ #region Private Fields // static class, so no private fields #endregion Private Fields } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TableSectionStyle.cs
- DataGridTemplateColumn.cs
- SHA1.cs
- NativeMethods.cs
- Geometry.cs
- BamlMapTable.cs
- FocusWithinProperty.cs
- EpmCustomContentDeSerializer.cs
- ConnectionPointGlyph.cs
- NullReferenceException.cs
- RenderDataDrawingContext.cs
- XPathChildIterator.cs
- DataGridViewRowsAddedEventArgs.cs
- BuildManager.cs
- Serializer.cs
- SQlBooleanStorage.cs
- ModifiableIteratorCollection.cs
- CompositeDuplexBindingElement.cs
- InstalledVoice.cs
- ArgumentsParser.cs
- ByteKeyFrameCollection.cs
- TreePrinter.cs
- ToolboxDataAttribute.cs
- _SpnDictionary.cs
- GenericsInstances.cs
- MsmqTransportElement.cs
- unsafenativemethodstextservices.cs
- TextFormatterHost.cs
- CodeStatement.cs
- DataContext.cs
- BoolExpression.cs
- CollectionContainer.cs
- SerialPort.cs
- TextFormatter.cs
- WebPartDeleteVerb.cs
- DataGridViewColumnDesignTimeVisibleAttribute.cs
- DataGridRowAutomationPeer.cs
- ZipFileInfoCollection.cs
- WrappedDispatcherException.cs
- ContentElement.cs
- PrePrepareMethodAttribute.cs
- EventLogTraceListener.cs
- ItemsControlAutomationPeer.cs
- CalculatedColumn.cs
- FileClassifier.cs
- HttpModuleAction.cs
- BamlResourceContent.cs
- NetworkStream.cs
- ProtocolsConfigurationEntry.cs
- SafeTokenHandle.cs
- ListViewItemEventArgs.cs
- TextTreeRootTextBlock.cs
- SignedXmlDebugLog.cs
- ECDiffieHellmanCng.cs
- DataGridViewButtonColumn.cs
- SerializationException.cs
- SizeKeyFrameCollection.cs
- SHA384.cs
- GroupDescription.cs
- ValidationSummary.cs
- DetailsViewInsertedEventArgs.cs
- CopyNamespacesAction.cs
- CustomValidator.cs
- PreviewPrintController.cs
- _AutoWebProxyScriptHelper.cs
- WindowsToolbar.cs
- EntityDataSourceEntityTypeFilterConverter.cs
- QilReplaceVisitor.cs
- TextElementEnumerator.cs
- TraceData.cs
- PersistencePipeline.cs
- DataContractJsonSerializer.cs
- QilLoop.cs
- SearchForVirtualItemEventArgs.cs
- NumberSubstitution.cs
- LineBreak.cs
- WebPartZoneDesigner.cs
- DataObjectPastingEventArgs.cs
- Encoder.cs
- MenuDesigner.cs
- BitmapSource.cs
- XmlUnspecifiedAttribute.cs
- WebPartTracker.cs
- Helper.cs
- PrivilegedConfigurationManager.cs
- PipeStream.cs
- XPathException.cs
- WebPartDescription.cs
- AddInActivator.cs
- AtlasWeb.Designer.cs
- PassportAuthenticationModule.cs
- WsdlInspector.cs
- AspCompat.cs
- WebPartDescription.cs
- DBConnectionString.cs
- FactoryRecord.cs
- SqlFlattener.cs
- FixedTextSelectionProcessor.cs
- ComponentChangedEvent.cs
- _NetworkingPerfCounters.cs