最近在搞图形的程序,说真的翻书有些数据都是点到为止,没有完美的范例解说,可能是我理解能力太差了…
先前一直使用Win form C# 来做做数据处理也画一些报表,突然要用WPF来写程序真的很不习惯,尤其是绘图的部分,请教有经验的前辈要如何写(我用PhotoShop画的如图所示)?
能给一段范例跟说明吗~感谢… 

解决方案 »

  1.   


    在 WPF 中实现这个东西肯定要比 WinForm 要简单。将一个 Polygon 放到 Canvas 中,Polygon 的Points通过一系列点来控制(绑定)
      

  2.   

    做了一个简单的例子:
    <Canvas xmlns:local="clr-namespace:WPFTester">
        <Canvas.Resources>
            <Style TargetType="Ellipse">
                <Setter Property="Width" Value="{x:Static local:PointsConverter.PointWidth}" />
                <Setter Property="Height" Value="{x:Static local:PointsConverter.PointWidth}" />
                <Setter Property="Cursor" Value="SizeAll" />
                <Setter Property="Fill" Value="Blue" />
                <EventSetter Event="MouseLeftButtonDown" Handler="Element_MouseLeftButtonDown" />
                <EventSetter Event="MouseMove" Handler="Element_MouseMove" />
                <EventSetter Event="MouseLeftButtonUp" Handler="Element_MouseLeftButtonUp" />
            </Style>
            <local:PointsConverter x:Key="PointsConverter" />
        </Canvas.Resources>
        <Polygon Fill="Red">
            <Polygon.Points>
                <MultiBinding Mode="TwoWay" Converter="{StaticResource PointsConverter}">
                    <Binding ElementName="P1" Path="(Canvas.Left)" />
                    <Binding ElementName="P1" Path="(Canvas.Top)" />
                    <Binding ElementName="P2" Path="(Canvas.Left)" />
                    <Binding ElementName="P2" Path="(Canvas.Top)" />
                    <Binding ElementName="P3" Path="(Canvas.Left)" />
                    <Binding ElementName="P3" Path="(Canvas.Top)" />
                    <Binding ElementName="P4" Path="(Canvas.Left)" />
                    <Binding ElementName="P4" Path="(Canvas.Top)" />
                    <Binding ElementName="P5" Path="(Canvas.Left)" />
                    <Binding ElementName="P5" Path="(Canvas.Top)" />
                </MultiBinding>
            </Polygon.Points>
        </Polygon>
        <Ellipse Name="P1" Canvas.Left="50" Canvas.Top="50" />
        <Ellipse Name="P2" Canvas.Left="150" Canvas.Top="150" />
        <Ellipse Name="P3" Canvas.Left="250" Canvas.Top="250" />
        <Ellipse Name="P4" Canvas.Left="50" Canvas.Top="150" />
        <Ellipse Name="P5" Canvas.Left="150" Canvas.Top="50" />
    </Canvas>
      

  3.   

    namespace WPFTester
    {
        public partial class Window1 : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }        System.Windows.Point offset;
            bool isInDrag = false;        private void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                FrameworkElement element = sender as FrameworkElement;
                offset = e.GetPosition(element);
                element.CaptureMouse();
                isInDrag = true;
                e.Handled = true;
            }        private void Element_MouseMove(object sender, MouseEventArgs e)
            {
                if (isInDrag)
                {
                    FrameworkElement element = sender as FrameworkElement;
                    System.Windows.Point currentPoint = e.GetPosition(this);
                    Canvas.SetLeft(element, currentPoint.X - offset.X);
                    Canvas.SetTop(element, currentPoint.Y - offset.Y);
                }
            }        private void Element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                if (isInDrag)
                {
                    FrameworkElement element = sender as FrameworkElement;
                    element.ReleaseMouseCapture();
                    isInDrag = false;
                    e.Handled = true;
                }
            }
        }    public class PointsConverter : IMultiValueConverter
        {
            public readonly static double PointWidth = 6;
            public readonly static double Offest = PointWidth / 2;        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                PointCollection points = new PointCollection();
                for (int i = 0; i < values.Length; i += 2)
                {
                    points.Add(new System.Windows.Point((double)values[i] + Offest, (double)values[i + 1] + Offest));
                }
                return points;
            }        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                PointCollection points = (PointCollection)value;
                object[] positions = new object[points.Count * 2];
                for (int i = 0; i < points.Count; i++)
                {
                    positions[i * 2] = points[i].X - Offest;
                    positions[i * 2 + 1] = points[i].Y - Offest;
                }
                return positions;
            }
        }}
      

  4.   

    谢谢9楼,可是的convert在哪用呢----我比较笨