从代码中更改RenderTransformOrigin

时间:2015-03-10 11:15:33

标签: c# wpf annotations

我需要更改某些注释对象的RenderTransformOrigin属性,以便它们始终围绕另一侧(x)的中心(y)旋转。

因此,让我们说用户点击对象的右侧,然后旋转点将是(0.0,0.5),但是如果用户希望从项目的另一侧更改旋转,然后他可以点击它的左侧,在这种情况下,旋转点变为(1.0,0.5)。

问题:我要求帮助的原因是,只要发生这种情况,项目就会跳到屏幕上。它的移动量取决于物品的角度(如果它根本不旋转,那么它根本不会移动)。角度本身在运动过程中不会改变,只有位置。 (我意识到在没有图像的情况下理解这个问题有点困难,但由于stackoverflow的规则,我不允许附加图像以使事情变得更容易。)所以这只会在选择另一方时发生(例如单击右侧后的左侧),移动/跳跃取决于角度(角度越大,跳跃越大)。

我想要做的是更改提到的属性,以便在对象的选定边缘周围发生旋转,而不会在更改边时对象保持上下跳跃。

感谢您的帮助!

修改[2015-03-10_12-17-21]

class XYZ  
{  
...  
private void RotateThumb_DragStarted(object sender, DragStartedEventArgs e) {
    m_rotateTransform = m_designerItem.RenderTransform as RotateTransform;

    if (m_rotateTransform == null) {
        m_designerItem.RenderTransform = new RotateTransform(0);
        m_rotateTransform = m_designerItem.RenderTransform as RotateTransform;
    }
    Point positionWithinAnnotation = Mouse.GetPosition(m_designerItem);
    var selectedThumb = GetThumbPosition(m_designerItem, positionWithinAnnotation);

    double relativeCenterX = ((selectedThumb == ThumbPosition.Left) ? (1.0D) : (0.0D));
    double relativeCenterY = 0.5D;

    m_designerItem.RenderTransformOrigin = new Point(relativeCenterX, relativeCenterY);
    m_transformOrigin = m_designerItem.RenderTransformOrigin;

    m_rotationPoint = m_designerItem.TranslatePoint(
        new Point(relativeCenterX * m_designerItem.ActualWidth,
                  relativeCenterY * m_designerItem.ActualHeight),
                  m_canvas);

    Point startPoint = Mouse.GetPosition(m_canvas);
    m_startVector = Point.Subtract(startPoint, m_rotationPoint);
}

private void handleRotate(DragDeltaEventArgs e) {
    Point currentPoint = Mouse.GetPosition(m_canvas);
    Vector deltaVector = Point.Subtract(currentPoint, m_rotationPoint);

    double angle = Vector.AngleBetween(m_startVector, deltaVector);
    m_rotateTransform.Angle += Math.Round(angle, 0);
    m_startVector = deltaVector;

    m_designerItem.InvalidateMeasure();
}

...
}

1 个答案:

答案 0 :(得分:0)

如果没有a good, minimal, complete code example,很难确定您需要做些什么来修复代码。

也就是说,从描述中我觉得你应该使用RotateTransform.CenterXRotateTransform.CenterY属性,而不是设置RenderTransformOrigin

例如:

private void RotateThumb_DragStarted(object sender, DragStartedEventArgs e) {
    m_rotateTransform = m_designerItem.RenderTransform as RotateTransform;

    if (m_rotateTransform == null) {
        m_designerItem.RenderTransform = new RotateTransform();
        m_rotateTransform = m_designerItem.RenderTransform as RotateTransform;
    }
    Point positionWithinAnnotation = Mouse.GetPosition(m_designerItem);
    var selectedThumb = GetThumbPosition(m_designerItem, positionWithinAnnotation);

    double relativeCenterX = ((selectedThumb == ThumbPosition.Left) ? (1.0D) : (0.0D));
    double relativeCenterY = 0.5D;

    m_rotateTransform.CenterX = relativeCenterX;
    m_rotateTransform.CenterY = relativeCenterY;

    // Without a complete example, I'm not sure I know what this part is
    // supposed to be doing. I think it's fine, but maybe it needs to be
    // fixed too?
    m_rotationPoint = m_designerItem.TranslatePoint(
        new Point(relativeCenterX * m_designerItem.ActualWidth,
                  relativeCenterY * m_designerItem.ActualHeight),
                  m_canvas);

    Point startPoint = Mouse.GetPosition(m_canvas);
    m_startVector = Point.Subtract(startPoint, m_rotationPoint);
}