Windows窗体 - > WPF图像控制转换问题

时间:2011-05-14 13:40:39

标签: c# wpf winforms picturebox

我一直在使用Windows Forms,但现在我正在尝试学习WPF,因为它具有优势。前段时间我创建了一个图片框控件(在Damien here的帮助下)。对我来说,将此控件转换为WPF的Image控件非常困难。我没有在互联网上找到任何适当的帮助。

我的控件用于在书的扫描图像上显示(之前建立的)两页之间的中间位置。它由两个可移动的点组成,它们之间的线和左边和右边的区域填充半透明多边形。 问题是WPF 非常不同。在Image控件上画一个圆圈很难。

这是我的代码清单:

public partial class SplitPictureBox : System.Windows.Forms.PictureBox
{
    public SplitPictureBox()
    {
        InitializeComponent();
    }

    private int mPointMoveInProgress = 0;

    private int handleRadius = 5;
    public int HandleRaduis
    {
        get { return handleRadius; }
        set { handleRadius = value; }
    }

    private int middleTop = 0;
    private int middleBottom = 0;

    private int middle;
    public int Middle
    {
        get 
        {    
            return (middleTop + middleBottom) /2; 
        }
        set { middle = value; }
    }

    private double theta;
    public double Theta
    {
        get
        {
            return (Math.Atan(((middleTop - middleBottom) / (double)this.Height)) * 180) / Math.PI;
        }
        set 
        {
            theta = value;
            int deltaX = (int)((Math.Tan((Math.PI / 180) * value)) * this.Height / 2);
            middleTop = middle + deltaX;
            middleBottom = middle - deltaX;
        }
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
        SolidBrush left = new SolidBrush(Color.FromArgb(80, Color.Blue));
        SolidBrush right = new SolidBrush(Color.FromArgb(80, Color.Green));
        SolidBrush brush = new SolidBrush(Color.Red);
        pe.Graphics.FillPolygon(left, new Point[4] { new Point(0,0), new Point(middleTop,0),
            new Point(middleBottom, this.Height), new Point(0, this.Height)
        });
        pe.Graphics.FillPolygon(right, new Point[4] { new Point(this.Width,0), new Point(middleTop,0),
            new Point(middleBottom, this.Height), new Point(this.Width, this.Height)
        });
        // Draw line        
        pe.Graphics.DrawLine(new Pen(Color.Red, 2), new Point(middleTop, handleRadius), new Point(middleBottom, this.Height - handleRadius));
        Rectangle rectangle;
        // Draw first handle
        rectangle = new Rectangle(middleTop - handleRadius, 0, handleRadius * 2, handleRadius * 2);
        pe.Graphics.FillEllipse(brush, rectangle);
        // Draw second handle
        rectangle = new Rectangle(middleBottom - handleRadius, this.Height - handleRadius * 2, handleRadius * 2, handleRadius * 2);
        pe.Graphics.FillEllipse(brush, rectangle);
    }

    private Point moveLineTop;
    private Point moveLineBottom;

    protected override void OnMouseDown(MouseEventArgs e)
    {
        moveLineTop = new Point(e.X - middleTop, 0);
        moveLineBottom = new Point(e.X - middleBottom, this.Height);
        if (Math.Abs(e.X - middleTop) < handleRadius && Math.Abs(e.Y) <= handleRadius * 2)
        {
            Cursor.Current = Cursors.Hand;
            mPointMoveInProgress = 1;
        }
        else if (Math.Abs(e.X - middleBottom) < handleRadius && Math.Abs(e.Y - this.Height) <= handleRadius * 2)
        {
            Cursor.Current = Cursors.Hand;
            mPointMoveInProgress = 2;
        }
        else if (Math.Abs(e.X - x) < handleRadius && e.Y > handleRadius * 2 && e.Y < this.Height - handleRadius * 2)
        {
            Cursor.Current = Cursors.SizeWE;
            mPointMoveInProgress = 3;
        }
        else mPointMoveInProgress = 0;
        base.OnMouseDown(e);
    }

    private int x = 0;

    protected override void OnMouseMove(MouseEventArgs e)
    {
        x = middleTop - (int)((e.Y * (middleTop - middleBottom)) / (double)this.Height);

        if (mPointMoveInProgress == 1)      
        {
            Cursor.Current = Cursors.Hand;
            if (e.X > 0 && e.X < this.Width)
            {
                middleTop = e.X;
                Refresh();
            }
        }
        else if (mPointMoveInProgress == 2)      
        {
            Cursor.Current = Cursors.Hand;
            if (e.X > 0 && e.X < this.Width)
            {
                middleBottom = e.X;
                Refresh();
            }
        }
        else if (mPointMoveInProgress == 3)
        {
            if (e.X - moveLineTop.X >= 0 && e.X - moveLineTop.X <= this.Width &&
                e.X - moveLineBottom.X >= 0 && e.X - moveLineBottom.X <= this.Width)
            {
                Cursor.Current = Cursors.SizeWE;
                middleTop = e.X - moveLineTop.X;
                middleBottom = e.X - moveLineBottom.X;

                Refresh();
            }
        }

        else       
        {
            if (Math.Abs(e.X - middleTop) < handleRadius && Math.Abs(e.Y) <= handleRadius * 2)
                Cursor.Current = Cursors.Hand;
            else if (Math.Abs(e.X - middleBottom) < handleRadius && Math.Abs(e.Y - this.Height) <= handleRadius * 2)
                Cursor.Current = Cursors.Hand;
            else if (Math.Abs(e.X - x) < handleRadius && e.Y > handleRadius * 2 && e.Y < this.Height - handleRadius * 2)
                Cursor.Current = Cursors.SizeWE;
            else Cursor.Current = Cursors.Default;
        }
        base.OnMouseMove(e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {      
        mPointMoveInProgress = 0;
        middle = (middleTop + middleBottom) / 2;
        base.OnMouseUp(e);
    }
}

有人可以帮我这个吗?给我一些有用的链接或代码示例。 谢谢!

1 个答案:

答案 0 :(得分:1)

您正在寻找Adorners,他们可以通过其他控件绘制项目,还可以处理事件等。

一些提示:

How to: Implement an Adorner

Adorners How-To Topics