本文整理汇总了C#中System.Windows.Media.Visual.TransformToAncestor方法的典型用法代码示例。如果您正苦于以下问题:C# Visual.TransformToAncestor方法的具体用法?C# Visual.TransformToAncestor怎么用?C# Visual.TransformToAncestor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Visual
的用法示例。
在下文中一共展示了Visual.TransformToAncestor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ElementToRoot
internal static Rect ElementToRoot(Rect rectElement, Visual element, PresentationSource presentationSource)
{
GeneralTransform transformElementToRoot = element.TransformToAncestor(presentationSource.RootVisual);
Rect rectRoot = transformElementToRoot.TransformBounds(rectElement);
return rectRoot;
}
示例2: SetCaretPosition
/// <summary>
/// Sets the position of the caret previously created using <see cref="CreateCaret"/>. position is relative to the owner visual.
/// </summary>
public static bool SetCaretPosition(Visual owner, Point position)
{
if (owner == null)
throw new ArgumentNullException("owner");
HwndSource source = PresentationSource.FromVisual(owner) as HwndSource;
if (source != null) {
Point pointOnRootVisual = owner.TransformToAncestor(source.RootVisual).Transform(position);
Point pointOnHwnd = pointOnRootVisual.TransformToDevice(source.RootVisual);
return SafeNativeMethods.SetCaretPos((int)pointOnHwnd.X, (int)pointOnHwnd.Y);
} else {
return false;
}
}
示例3: GetMousePosition
public static Point GetMousePosition(Visual relativeTo)
{
Win32Point mouse = new Win32Point();
GetCursorPos(ref mouse);
System.Windows.Interop.HwndSource presentationSource =
(System.Windows.Interop.HwndSource)PresentationSource.FromVisual(relativeTo);
ScreenToClient(presentationSource.Handle, ref mouse);
GeneralTransform transform = relativeTo.TransformToAncestor(presentationSource.RootVisual);
Point offset = transform.Transform(new Point(0, 0));
return new Point(mouse.X - offset.X, mouse.Y - offset.Y);
}
示例4: GetTransformGlobal
// Transform in relation to the root SurfaceWindow
public static GeneralTransform GetTransformGlobal(Visual visual)
{
SurfaceWindow window = GetSurfaceWindow(visual);
if (window != null)
{
try
{
return visual.TransformToAncestor(window);
}
catch (InvalidOperationException)
{
return null;
}
}
return null;
//return visual.TransformToAncestor(Application.Current.MainWindow);
}
示例5: GetMousePosition
public static Point GetMousePosition(Visual relativeTo)
{
Win32Point mouse = new Win32Point();
GetCursorPos(ref mouse);
System.Windows.Interop.HwndSource presentationSource =
(System.Windows.Interop.HwndSource)PresentationSource.FromVisual(relativeTo);
ScreenToClient(presentationSource.Handle, ref mouse);
GeneralTransform transform = relativeTo.TransformToAncestor(presentationSource.RootVisual);
Point offset = transform.Transform(new Point(0, 0));
//
// Point p = new Point(mouse.X - offset.X, mouse.Y - offset.Y);
// System.Diagnostics.Debug.WriteLine(string.Format("mouse {0:0.0}|{1:0.0} offset {2:0.0}|{3:0.0} res {4:0.0}|{5:0.0}",
// mouse.X, mouse.Y, offset.X, offset.Y, p.X, p.Y));
return new Point(mouse.X - offset.X, mouse.Y - offset.Y);
}
示例6: TransformToScreen
public static Point TransformToScreen(Point point, Visual relativeTo)
{
var hwndSource = PresentationSource.FromVisual(relativeTo) as HwndSource;
var root = hwndSource.RootVisual;
// Translate the point from the visual to the root.
var transformToRoot = relativeTo.TransformToAncestor(root);
var pointRoot = transformToRoot.Transform(point);
// Transform the point from the root to client coordinates.
var m = Matrix.Identity;
var transform = VisualTreeHelper.GetTransform(root);
if (transform != null)
{
m = Matrix.Multiply(m, transform.Value);
}
var offset = VisualTreeHelper.GetOffset(root);
m.Translate(offset.X, offset.Y);
var pointClient = m.Transform(pointRoot);
// Convert from “device-independent pixels” into pixels.
pointClient = hwndSource.CompositionTarget.TransformToDevice.Transform(pointClient);
var pointClientPixels = new Native.POINT();
pointClientPixels.x = (0 < pointClient.X) ? (int)(pointClient.X + 0.5) : (int)(pointClient.X - 0.5);
pointClientPixels.y = (0 < pointClient.Y) ? (int)(pointClient.Y + 0.5) : (int)(pointClient.Y - 0.5);
// Transform the point into screen coordinates.
var pointScreenPixels = pointClientPixels;
Native.ClientToScreen(hwndSource.Handle, ref pointScreenPixels);
//Native.GetCurrentPositionEx(hwndSource.Handle, out pointScreenPixels);
//Native.GetWindowOrgEx(hwndSource.Handle, out pointScreenPixels);
return new Point(pointScreenPixels.x, pointScreenPixels.y);
}
示例7: GetTransformToAncestor
/// <summary>
/// Gets transform to ancestor for inner scope
/// </summary>
private Transform GetTransformToAncestor(Visual innerScope)
{
// NOTE: TransformToAncestor is safe (will never throw an exception).
Transform transform = innerScope.TransformToAncestor(_renderScope) as Transform;
if (transform == null)
{
transform = Transform.Identity;
}
return transform;
}
示例8: ScreenToClient
private Point ScreenToClient(Point point, Visual visual)
{
PresentationSource presentationSource = PresentationSource.CriticalFromVisual(visual);
point = PointUtil.ScreenToClient(point, presentationSource);
if (presentationSource != null)
{
GeneralTransform transform = visual.TransformToAncestor(presentationSource.RootVisual);
if (transform != null)
{
transform = transform.Inverse;
if (transform != null)
{
point = transform.Transform(point);
}
}
}
return point;
}
示例9: ClientToScreen
private Point ClientToScreen(Point point, Visual visual)
{
PresentationSource presentationSource = PresentationSource.CriticalFromVisual(visual);
if (presentationSource != null)
{
GeneralTransform transform = visual.TransformToAncestor(presentationSource.RootVisual);
if (transform != null)
{
point = transform.Transform(point);
}
}
return PointUtil.ClientToScreen(point, presentationSource);
}
示例10: PrepareAttributes
private void PrepareAttributes(InputScope inputScope, double fontSize, FontFamily fontFamily, XmlLanguage language, Visual visual, int count, Guid[] filterAttributes)
{
if (_preparedattributes == null)
{
_preparedattributes = new ArrayList(count);
}
else
{
_preparedattributes.Clear();
}
int i;
for (i = 0; i < _supportingattributes.Length; i++)
{
if (count != 0)
{
int j;
bool found = false;
for (j = 0; j < count; j++)
{
if (_supportingattributes[i].Guid.Equals(filterAttributes[j]))
found = true;
}
if (!found)
continue;
}
UnsafeNativeMethods.TS_ATTRVAL attrval = new UnsafeNativeMethods.TS_ATTRVAL();
attrval.attributeId = _supportingattributes[i].Guid;
attrval.overlappedId = (int)_supportingattributes[i].Style;
attrval.val = new NativeMethods.VARIANT();
// This VARIANT is returned to the caller, which supposed to call VariantClear().
// GC does not have to clear it.
attrval.val.SuppressFinalize();
switch (_supportingattributes[i].Style)
{
case AttributeStyle.InputScope:
object obj = new InputScopeAttribute(inputScope);
attrval.val.vt = (short)NativeMethods.tagVT.VT_UNKNOWN;
attrval.val.data1.Value = Marshal.GetIUnknownForObject(obj);
break;
case AttributeStyle.Font_Style_Height:
// We always evaluate the font size and returns a value.
attrval.val.vt = (short)NativeMethods.tagVT.VT_I4;
attrval.val.data1.Value = (IntPtr)(int)fontSize;
break;
case AttributeStyle.Font_FaceName:
{
string familyName = GetFontFamilyName(fontFamily, language);
if (familyName != null)
{
attrval.val.vt = (short)NativeMethods.tagVT.VT_BSTR;
attrval.val.data1.Value = Marshal.StringToBSTR(familyName);
}
}
break;
case AttributeStyle.Font_SizePts:
attrval.val.vt = (short)NativeMethods.tagVT.VT_I4;
attrval.val.data1.Value = (IntPtr)(int)(fontSize / 96.0 * 72.0);
break;
case AttributeStyle.Text_ReadOnly:
attrval.val.vt = (short)NativeMethods.tagVT.VT_BOOL;
attrval.val.data1.Value = IsReadOnly ? (IntPtr)1 : (IntPtr)0;
break;
case AttributeStyle.Text_Orientation:
attrval.val.vt = (short)NativeMethods.tagVT.VT_I4;
attrval.val.data1.Value = (IntPtr)0;
// Get the transformation that is relative from source.
PresentationSource source = null;
source = PresentationSource.CriticalFromVisual((Visual)RenderScope);
if (source != null)
{
Visual root = source.RootVisual;
if ((root != null) && (visual != null))
{
//
// Calc radian from Matirix. This is approximate calculation from the first row.
// If tf.M12 is 0, angle will be 0. So we don't have to calc it.
//
GeneralTransform transform = visual.TransformToAncestor(root);
Transform t = transform.AffineTransform;
//
if (t != null)
{
Matrix tf = t.Value;
if ((tf.M11 != 0) || (tf.M12 != 0))
{
double radSin = Math.Asin(tf.M12 / Math.Sqrt((tf.M11 * tf.M11) + (tf.M12 * tf.M12)));
double radCos = Math.Acos(tf.M11 / Math.Sqrt((tf.M11 * tf.M11) + (tf.M12 * tf.M12)));
// double angleSin = Math.Round((radSin * 180) / Math.PI, 0);
//.........这里部分代码省略.........
示例11: CalculateVisibleRect
/// <summary>
/// Calculates visible rectangle taking into account all clips and transforms
/// in the visual ancestors chain.
/// </summary>
/// <param name="visibleRect">Original rectangle relative to 'visual'.</param>
/// <param name="originalVisual">Originating visual element.</param>
internal static Rect CalculateVisibleRect(Rect visibleRect, Visual originalVisual)
{
Visual visual = VisualTreeHelper.GetParent(originalVisual) as Visual;
while (visual != null && visibleRect != Rect.Empty)
{
if (VisualTreeHelper.GetClip(visual) != null)
{
GeneralTransform transform = originalVisual.TransformToAncestor(visual).Inverse;
// Safer version of transform to descendent (doing the inverse ourself),
// we want the rect inside of our space. (Which is always rectangular and much nicer to work with)
if (transform != null)
{
Rect rectBounds = VisualTreeHelper.GetClip(visual).Bounds;
rectBounds = transform.TransformBounds(rectBounds);
visibleRect.Intersect(rectBounds);
}
else
{
// No visibility if non-invertable transform exists.
visibleRect = Rect.Empty;
}
}
visual = VisualTreeHelper.GetParent(visual) as Visual;
}
return visibleRect;
}
示例12: MakeVisible
/// <summary>
/// StackPanel implementation of <seealso cref="IScrollInfo.MakeVisible" />.
/// </summary>
// The goal is to change offsets to bring the child into view, and return a rectangle in our space to make visible.
// The rectangle we return is in the physical dimension the input target rect transformed into our pace.
// In the logical dimension, it is our immediate child's rect.
// Note: This code presently assumes we/children are layout clean. See work item 22269 for more detail.
public Rect MakeVisible(Visual visual, Rect rectangle)
{
Vector newOffset = new Vector();
Rect newRect = new Rect();
// We can only work on visuals that are us or children.
// An empty rect has no size or position. We can't meaningfully use it.
if ( rectangle.IsEmpty
|| visual == null
|| visual == (Visual)this
|| !this.IsAncestorOf(visual))
{
return Rect.Empty;
}
#pragma warning disable 1634, 1691
#pragma warning disable 56506
// Compute the child's rect relative to (0,0) in our coordinate space.
// This is a false positive by PreSharp. visual cannot be null because of the 'if' check above
GeneralTransform childTransform = visual.TransformToAncestor(this);
#pragma warning restore 56506
#pragma warning restore 1634, 1691
rectangle = childTransform.TransformBounds(rectangle);
// We can't do any work unless we're scrolling.
if (!IsScrolling)
{
return rectangle;
}
// Bring the target rect into view in the physical dimension.
MakeVisiblePhysicalHelper(rectangle, ref newOffset, ref newRect);
// Bring our child containing the visual into view.
int childIndex = FindChildIndexThatParentsVisual(visual);
MakeVisibleLogicalHelper(childIndex, ref newOffset, ref newRect);
// We have computed the scrolling offsets; validate and scroll to them.
newOffset.X = ScrollContentPresenter.CoerceOffset(newOffset.X, _scrollData._extent.Width, _scrollData._viewport.Width);
newOffset.Y = ScrollContentPresenter.CoerceOffset(newOffset.Y, _scrollData._extent.Height, _scrollData._viewport.Height);
if (!DoubleUtil.AreClose(newOffset, _scrollData._offset))
{
_scrollData._offset = newOffset;
InvalidateMeasure();
OnScrollChange();
}
// Return the rectangle
return newRect;
}
示例13: MakeVisible
public Rect MakeVisible(Visual visual, Rect rectangle)
{
if (rectangle.IsEmpty ||
visual == null ||
visual == this ||
!IsAncestorOf(visual))
{
return Rect.Empty;
}
rectangle = visual.TransformToAncestor(this).TransformBounds(rectangle);
var viewRect = new Rect(HorizontalOffset, VerticalOffset, ViewportWidth, ViewportHeight);
rectangle.X += viewRect.X;
rectangle.Y += viewRect.Y;
viewRect.X = CalculateNewScrollOffset(viewRect.Left, viewRect.Right, rectangle.Left, rectangle.Right);
viewRect.Y = CalculateNewScrollOffset(viewRect.Top, viewRect.Bottom, rectangle.Top, rectangle.Bottom);
SetHorizontalOffset(viewRect.X);
SetVerticalOffset(viewRect.Y);
rectangle.Intersect(viewRect);
rectangle.X -= viewRect.X;
rectangle.Y -= viewRect.Y;
return rectangle;
}
示例14: MakeVisible
/// <summary>
/// <see cref="IScrollInfo.MakeVisible"/>
/// </summary>
internal Rect MakeVisible(UIElement owner, Visual visual, Rect rectangle)
{
// We can only work on visuals that are us or children.
// An empty rect has no size or position. We can't meaningfully use it.
if (rectangle.IsEmpty ||
visual == null ||
(visual != owner && !owner.IsAncestorOf(visual)))
{
return Rect.Empty;
}
// Compute the child's rect relative to (0,0) in our coordinate space.
GeneralTransform childTransform = visual.TransformToAncestor(owner);
rectangle = childTransform.TransformBounds(rectangle);
// Initialize the viewport
Rect viewport = new Rect(_offset.X, _offset.Y, _viewport.Width, _viewport.Height);
rectangle.X += viewport.X;
rectangle.Y += viewport.Y;
// Compute the offsets required to minimally scroll the child maximally into view.
double minX = ComputeScrollOffset(viewport.Left, viewport.Right, rectangle.Left, rectangle.Right);
double minY = ComputeScrollOffset(viewport.Top, viewport.Bottom, rectangle.Top, rectangle.Bottom);
// We have computed the scrolling offsets; scroll to them.
SetHorizontalOffset(owner, minX);
SetVerticalOffset(owner, minY);
// Compute the visible rectangle of the child relative to the viewport.
if (this.CanHorizontallyScroll)
{
viewport.X = minX;
}
else
{
// munge the intersection
rectangle.X = viewport.X;
}
if (this.CanVerticallyScroll)
{
viewport.Y = minY;
}
else
{
// munge the intersection
rectangle.Y = viewport.Y;
}
rectangle.Intersect(viewport);
if (!rectangle.IsEmpty)
{
rectangle.X -= viewport.X;
rectangle.Y -= viewport.Y;
}
// Return the rectangle
return rectangle;
}
示例15: MakeVisible
/// <summary>
/// Bring the specified rectangle to view.
/// </summary>
public Rect MakeVisible(Visual visual, Rect rectangle)
{
if (visual == null)
{
throw new ArgumentNullResourceException("visual", Properties.Resources.General_Given_Parameter_Cannot_Be_Null);
}
if (this.content.IsAncestorOf(visual))
{
Rect transformedRect = visual.TransformToAncestor(this.content).TransformBounds(rectangle);
var viewportRect = new Rect(ContentOffsetX, ContentOffsetY, ContentViewportWidth, ContentViewportHeight);
if (!transformedRect.Contains(viewportRect))
{
double horizOffset = 0;
double vertOffset = 0;
if (transformedRect.Left < viewportRect.Left)
{
//
// Want to move viewport left.
//
horizOffset = transformedRect.Left - viewportRect.Left;
} else if (transformedRect.Right > viewportRect.Right)
{
//
// Want to move viewport right.
//
horizOffset = transformedRect.Right - viewportRect.Right;
}
if (transformedRect.Top < viewportRect.Top)
{
//
// Want to move viewport up.
//
vertOffset = transformedRect.Top - viewportRect.Top;
} else if (transformedRect.Bottom > viewportRect.Bottom)
{
//
// Want to move viewport down.
//
vertOffset = transformedRect.Bottom - viewportRect.Bottom;
}
SnapContentOffsetTo(new Point(ContentOffsetX + horizOffset, ContentOffsetY + vertOffset));
}
}
return rectangle;
}