2011-05-14 107 views
0

我一直使用Windows窗体,但现在我试图学习WPF,因为它的优点。前段时间我创建了一个picturebox控件(在Damienhere的帮助下)。对我而言,将这个控件转换为WPF的Image控件非常困难。我在互联网上找不到任何适当的帮助。Windows窗体 - > WPF图像控制转换问题

我的控件用于在书的扫描图像上的两页之间显示(创建之前)中间。它由两个可移动的点组成,它们之间的线和左侧和右侧的区域都充满了半透明的多边形。 问题是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); 
    } 
} 

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

回答