WPF如何写出可以移动的(Shape如图所示) 最近在搞图形的程序,说真的翻书有些数据都是点到为止,没有完美的范例解说,可能是我理解能力太差了…先前一直使用Win form C# 来做做数据处理也画一些报表,突然要用WPF来写程序真的很不习惯,尤其是绘图的部分,请教有经验的前辈要如何写(我用PhotoShop画的如图所示)?能给一段范例跟说明吗~感谢… 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在 WPF 中实现这个东西肯定要比 WinForm 要简单。将一个 Polygon 放到 Canvas 中,Polygon 的Points通过一系列点来控制(绑定) 做了一个简单的例子:<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> 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; } }} 谢谢9楼,可是的convert在哪用呢----我比较笨 串口程序问题,open时报错。 在工控机上使用程序出现找不到system.windows.forms c# 文件追加数据 急!急!急!请教高手,程序非正常运行问题 DataGrid能满足这样的要求吗? 问一个正则表达式的写法 问个XP下IIS的问题,我在WIN2003下做的一个网站,为何放到XP下只有数据库读取的权限,没有修改和删除的权限?如何解决? asp里C#怎样调用自己C#写的DLL类库 C#简单问题? 谁能给我推荐几个好一点的网上书库 Spring.Net构造方法带参实例化对象问题--高分奖励 数字前如何自动+0
在 WPF 中实现这个东西肯定要比 WinForm 要简单。将一个 Polygon 放到 Canvas 中,Polygon 的Points通过一系列点来控制(绑定)
<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>
{
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;
}
}}