轉(zhuǎn)帖|其它|編輯:郝浩|2010-12-24 16:04:33.000|閱讀 352 次
概述:底圖控件分為預(yù)顯層和圖形層,預(yù)顯層用來(lái)輔助畫(huà)圖者更準(zhǔn)確的繪制圖型,圖形層用來(lái)顯示操作者繪制好的圖形。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
底圖控件分為預(yù)顯層和圖形層,預(yù)顯層用來(lái)輔助畫(huà)圖者更準(zhǔn)確的繪制圖型,圖形層用來(lái)顯示操作者繪制好的圖形。
底圖控件提供"捕捉鼠標(biāo)操作實(shí)現(xiàn)繪圖功能"(見(jiàn)目標(biāo)1)
預(yù)顯層需要實(shí)現(xiàn)"繪線輔助提示功能"(見(jiàn)目標(biāo)2)
圖形層需要實(shí)現(xiàn)"繪圖預(yù)顯功能"(見(jiàn)目標(biāo)3)
底圖控件(代碼如下)
1、捕捉鼠標(biāo)操作實(shí)現(xiàn)繪圖功能
<UserControl x:Class="fxhomeSoftMaps.baseMap"
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="//schemas.microsoft.com/expression/blend/2008"
xmlns:mc="//schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:my="clr-namespace:fxhomeSoftMaps">
<Grid x:Name="LayoutRoot">
<Grid x:Name="previewLayer" Background="#FF00D6B4"
MouseLeftButtonDown="previewLayer_MouseLeftButtonDown"
MouseMove="previewLayer_MouseMove"
KeyDown="previewLayer_KeyDown"
>
<my:baseMap_point VerticalAlignment="Top" x:Name="bPoint" HorizontalAlignment="Left" Visibility="Collapsed"/>
<my:baseMap_previewLine x:Name="bLine" VerticalAlignment="Top" HorizontalAlignment="Left" Visibility="Collapsed"/>
</Grid>
<Grid x:Name="graphicsLayer">
</Grid>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace fxhomeSoftMaps
{
public partial class baseMap : UserControl
{
public baseMap()
{
InitializeComponent();
}
#region 屬性
//線路集合
private List<Line> _lineList;
public List<Line> lineList
{
get
{
return _lineList;
}
set
{
_lineList = value;
}
}
//畫(huà)線開(kāi)關(guān)
private bool _lineState = true;
public bool lineState
{
get
{
return _lineState;
}
set
{
_lineState = value;
}
}
//線段開(kāi)關(guān)
private bool lineSegment = false;
#endregion
#region 預(yù)覽圖層-畫(huà)線操作
//鼠標(biāo)按下
private void previewLayer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (lineState)
{
//設(shè)置預(yù)顯開(kāi)始點(diǎn)
bPoint.X = e.GetPosition(null).X;
bPoint.Y = e.GetPosition(null).Y;
bPoint.Visibility = Visibility.Visible;
//設(shè)置預(yù)顯線
bLine.X1 = bPoint.X;
bLine.Y1 = bPoint.Y;
bLine.X2 = bPoint.X;
bLine.Y2 = bPoint.Y;
bLine.Visibility = Visibility.Visible;
//打開(kāi)線段開(kāi)關(guān)
lineSegment = true;
if (lineSegment == false)
{
}
}
}
//鼠標(biāo)移動(dòng)
private void previewLayer_MouseMove(object sender, MouseEventArgs e)
{
if (lineState)
{
bLine.X2 = e.GetPosition(null).X;
bLine.Y2 = e.GetPosition(null).Y;
}
}
//按鍵
private void previewLayer_KeyDown(object sender, KeyEventArgs e)
{
//如果按ESC 取消畫(huà)線
if (e.Key == Key.Escape)
{
bPoint.Visibility = Visibility.Collapsed;
bLine.Visibility = Visibility.Collapsed;
}
}
#endregion
#region 圖形圖層操作
#endregion
}
}
2、繪線輔助提示功能(如圖)
1.1 預(yù)顯線開(kāi)始點(diǎn)控件(代碼如下)
<UserControl x:Class="fxhomeSoftMaps.baseMap_point"
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="//schemas.microsoft.com/expression/blend/2008"
xmlns:mc="//schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Width="4" Height="4">
<Grid x:Name="LayoutRoot" Margin="-2,-2,0,0">
<Ellipse x:Name="baseMapPoint">
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Color="#FF00A7FF" Offset="0"/>
<GradientStop Color="#FF44FBFF" Offset="1"/>
<GradientStop Color="#FF14C0FF" Offset="0.575"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace fxhomeSoftMaps
{
public partial class baseMap_point : UserControl
{
public baseMap_point()
{
InitializeComponent();
}
public Double X
{
get { return (Double)GetValue(XProperty); }
set { SetValue(XProperty, value); }
}
// Using a DependencyProperty as the backing store for X. This enables animation, styling, binding, etc...
public static readonly DependencyProperty XProperty =
DependencyProperty.Register("X", typeof(Double), typeof(baseMap_point), new PropertyMetadata(null));
public Double Y
{
get { return (Double)GetValue(YProperty); }
set { SetValue(YProperty, value); }
}
// Using a DependencyProperty as the backing store for Y. This enables animation, styling, binding, etc...
public static readonly DependencyProperty YProperty =
DependencyProperty.Register("Y", typeof(Double), typeof(baseMap_point), new PropertyMetadata(null));
}
}
1.2 預(yù)顯線結(jié)束控件(代碼如下)
<UserControl x:Class="fxhomeSoftMaps.baseMap_previewArrow"
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="//schemas.microsoft.com/expression/blend/2008"
xmlns:mc="//schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="30">
<Grid x:Name="LayoutRoot" Margin="-15,-15,0,0">
<Line StrokeThickness="2" Y2="30" X2="15" X1="15" Stroke="#FFC00000" StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
<Line Y1="15" X2="30" Y2="15" Stroke="#FFC00000" StrokeThickness="2" StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
<Ellipse HorizontalAlignment="Left" Height="10" Margin="10,10,0,0" StrokeThickness="2" VerticalAlignment="Top" Width="10">
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Color="#FF5AFF00" Offset="0"/>
<GradientStop Color="Transparent" Offset="1"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace fxhomeSoftMaps
{
public partial class baseMap_previewArrow : UserControl
{
public baseMap_previewArrow()
{
InitializeComponent();
}
}
}
1.3 預(yù)顯線控件[包含以上兩個(gè)控件](代碼如下)
<UserControl x:Name="userControl" x:Class="fxhomeSoftMaps.baseMap_previewLine"
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="//schemas.microsoft.com/expression/blend/2008"
xmlns:mc="//schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:my="clr-namespace:fxhomeSoftMaps">
<Grid x:Name="LayoutRoot">
<Line x:Name="baseMapLine" Stroke="#FFC00000" StrokeThickness="2" StrokeDashArray="2 2" X1="{Binding X1, ElementName=userControl, Mode=TwoWay}" Y1="{Binding Y1, ElementName=userControl, Mode=TwoWay}" X2="{Binding X2, ElementName=userControl, Mode=TwoWay}" Y2="{Binding Y2, ElementName=userControl, Mode=TwoWay}"/>
<my:baseMap_previewArrow x:Name="baseMap_previewArrow"/>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace fxhomeSoftMaps
{
public partial class baseMap_previewLine : UserControl
{
public baseMap_previewLine()
{
InitializeComponent();
}
#region 附加屬性
public Double X1
{
get { return (Double)GetValue(X1Property); }
set { SetValue(X1Property, value); }
}
// Using a DependencyProperty as the backing store for X1. This enables animation, styling, binding, etc...
public static readonly DependencyProperty X1Property =
DependencyProperty.Register("X1", typeof(Double), typeof(baseMap_previewLine), new PropertyMetadata(null));
public Double X2
{
get { return (Double)GetValue(X2Property); }
set
{
SetValue(X2Property, value);
baseMap_previewArrow.Margin = new Thickness(baseMapLine.X2, baseMapLine.Y2, 0, 0);
}
}
// Using a DependencyProperty as the backing store for X2. This enables animation, styling, binding, etc...
public static readonly DependencyProperty X2Property =
DependencyProperty.Register("X2", typeof(Double), typeof(baseMap_previewLine), new PropertyMetadata(null));
public Double Y1
{
get { return (Double)GetValue(Y1Property); }
set { SetValue(Y1Property, value); }
}
// Using a DependencyProperty as the backing store for Y1. This enables animation, styling, binding, etc...
public static readonly DependencyProperty Y1Property =
DependencyProperty.Register("Y1", typeof(Double), typeof(baseMap_previewLine), new PropertyMetadata(null));
public Double Y2
{
get { return (Double)GetValue(Y2Property); }
set
{
SetValue(Y2Property, value);
baseMap_previewArrow.Margin = new Thickness(baseMapLine.X2, baseMapLine.Y2, 0, 0);
}
}
// Using a DependencyProperty as the backing store for Y2. This enables animation, styling, binding, etc...
public static readonly DependencyProperty Y2Property =
DependencyProperty.Register("Y2", typeof(Double), typeof(baseMap_previewLine), new PropertyMetadata(null));
#endregion
}
}
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:博客轉(zhuǎn)載