精华内容
下载资源
问答
  • 使用WPF制作旗的简单示例,用到了ViewBox、Canvas、Path,代码简单明了,一看就懂,一看就会。
  • WPF经典教程之Canvas、InkCanvas布局

    千次下载 热门讨论 2014-10-05 00:45:06
    WPF中子元素的绝对定位的布局控件 其子元素使用Width、Height定义元素的宽度和高度 使用Convas.Left(Convas.Right)、Convas.Top(Convas.Bottom)定义与Convas容器的相对位置..... (更多资源:...
  • public MainWindow() { InitializeComponent();//初始化 Image image = new Image();//新建图片对象 image.Source = new BitmapImage(new Uri("G:\\个人成长\\认知\\互联网\...//将图片对象赋值给Canvas的Background }
      public MainWindow()
            {
               
                InitializeComponent();//初始化
    
                
                Image image = new Image();//新建图片对象
                image.Source = new BitmapImage(new Uri("G:\\个人成长\\认知\\互联网\\.NET\\常用UI资源\\背景图片\\1.jpg"));//读取图片
                ImageBrush ib = new ImageBrush();//新建图片对象
                ib.ImageSource = image.Source;//赋值为背景图片
                canvasLogin.Background = ib;//将图片对象赋值给Canvas的Background
            }

     

    展开全文
  • WPFCanvas使用

    2019-10-08 13:26:07
    首先知道Canvas有Left、Right、Top和Bottom这四个属性,放入Canvas的元素通过这四个属性来决定它们在Canvas里面的位置。 比如: Xaml: <Canvas HorizontalAlignment="Left" Height="299" Margin="133,52,0,...

    首先知道Canvas有Left、Right、Top和Bottom这四个属性,放入Canvas的元素通过这四个属性来决定它们在Canvas里面的位置。

    比如:

    Xaml:

    <Canvas HorizontalAlignment="Left" Height="299" Margin="133,52,0,0" VerticalAlignment="Top" Width="526">
                <Button Content="Button" Canvas.Left="72" Canvas.Top="45" Width="75" Height="38"/>
                <Button Content="Button" Canvas.Left="124" Canvas.Top="173" Width="75" Height="42"/>
                <Button Content="Button" Canvas.Left="387" Canvas.Top="64" Width="75" Height="46"/>
                <Button Content="Button" Canvas.Left="336" Canvas.Top="196" Width="75" Height="42"/>
    </Canvas>

     如图:

     

    Canvas中Z-Index属性:

    不加Z-Index属性时,Canvas里面的元素会按照后定义的优先级更高,优先级高的可以覆盖优先级低的。

    例:Xaml:

    <Grid>
            <Canvas HorizontalAlignment="Left" Height="299" Margin="133,52,0,0" VerticalAlignment="Top" Width="526">
                <Rectangle Fill="#FF5D5DC8" Height="45" Canvas.Left="22" Stroke="Black" Canvas.Top="25" Width="100"/>
                <Rectangle Fill="#FFEAEAF6" Height="50" Canvas.Left="94" Stroke="Black" Canvas.Top="46" Width="100"/>
                <Rectangle Fill="#FFCD6650" Height="100" Canvas.Left="166" Stroke="Black" Canvas.Top="84" Width="59"/>
                <Ellipse Fill="#FFB1CD50" Height="100" Canvas.Left="195" Stroke="Black" Canvas.Top="143" Width="100"/>
            </Canvas>
    </Grid>

    这里后定义的会覆盖先定义的子元素:

    下面看看使用了Z-Index属性:

    Xaml:

    <Grid>
            <Canvas HorizontalAlignment="Left" Height="299" Margin="133,52,0,0" VerticalAlignment="Top" Width="526">
                <Rectangle Fill="#FF5D5DC8" Panel.ZIndex="3" Height="45" Canvas.Left="22" Stroke="Black" Canvas.Top="25" Width="100"/>
                <Rectangle Fill="#FFEAEAF6" Panel.ZIndex="2" Height="50" Canvas.Left="94" Stroke="Black" Canvas.Top="46" Width="100"/>
                <Rectangle Fill="#FFCD6650" Panel.ZIndex="1" Height="100" Canvas.Left="166" Stroke="Black" Canvas.Top="84" Width="59"/>
                <Ellipse Fill="#FFB1CD50" Panel.ZIndex="0" Height="100" Canvas.Left="195" Stroke="Black" Canvas.Top="143" Width="100"/>
            </Canvas>
    </Grid>

    如图:

    可以看出,Z-Index越小优先级越高,那么如果定义的Z-Index一样会是谁优先级更高呢?接着看。

     

    Xaml:

    <Grid>
            <Canvas HorizontalAlignment="Left" Height="299" Margin="133,52,0,0" VerticalAlignment="Top" Width="526">
                <Rectangle Fill="#FF5D5DC8" Panel.ZIndex="3" Height="45" Canvas.Left="22" Stroke="Black" Canvas.Top="25" Width="100"/>
                <Rectangle Fill="#FFEAEAF6" Panel.ZIndex="3" Height="50" Canvas.Left="94" Stroke="Black" Canvas.Top="46" Width="100"/>
                <Rectangle Fill="#FFCD6650" Panel.ZIndex="3" Height="100" Canvas.Left="166" Stroke="Black" Canvas.Top="84" Width="59"/>
                <Ellipse Fill="#FFB1CD50" Panel.ZIndex="3" Height="100" Canvas.Left="195" Stroke="Black" Canvas.Top="143" Width="100"/>
            </Canvas>
    </Grid>

    如图:

    可以看出,Z-Index一样的时候,谁后定义谁的优先级就高。

    转载于:https://www.cnblogs.com/Leozi/p/10779991.html

    展开全文
  • wpf Canvas重写可视化流程图配置 类型VISIO功能 VS2013 可编译 有生成exe可参考
  • WPF <ZoomableCanvas> 实现缩放移动
  • WPF中画布Canvas控件中内容缩放 1. Transform Class 简介 Transform类位于命名空间System.Windows.Media下。在WPF中Transform类被广泛应用于绘图元素的变换。作为大部分可视化控件的基类,UIElement类具有这样的静态...

    WPF中画布Canvas控件中内容缩放

    1. Transform Class 简介

    Transform类位于命名空间System.Windows.Media下。在WPF中Transform类被广泛应用于绘图元素的变换。作为大部分可视化控件的基类,UIElement类具有这样的静态依赖属性:

    
    // System.Windows.UIElement
    
    public Transform RenderTransform
    {
    	get
    	{
    		return (Transform)base.GetValue(UIElement.RenderTransformProperty);
    	}
    	set
    	{
    		base.SetValue(UIElement.RenderTransformProperty, value);
    	}
    }
    
    [CommonDependencyProperty]
    public static readonly DependencyProperty RenderTransformProperty = 
    	DependencyProperty.Register("RenderTransform", typeof(Transform), typeof(UIElement), new PropertyMetadata(Transform.Identity, new PropertyChangedCallback(UIElement.RenderTransform_Changed)));
    
    private static void RenderTransform_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    	UIElement uielement = (UIElement)d;
    	if (!uielement.NeverMeasured && !uielement.NeverArranged && !e.IsASubPropertyChange)
    	{
    		uielement.InvalidateArrange();
    		uielement.AreTransformsClean = false;
    	}
    }
    
    

    官方文档的注解

    A render transform does not regenerate layout size or render size information. Render transforms are typically intended for animating or applying a temporary effect to an element. For example, the element might zoom when focused or moused over, or might jitter on load to draw the eye to that part of the user interface (UI).

    故可以通过更改UIElement的RenderTransform属性达到控件变换的目的,且开销较小。
    注意到Transform类为抽象类,派生类型有:

    • System.Windows.Media.MatrixTransform
    • System.Windows.Media.TranslateTransform
    • System.Windows.Media.RotateTransform
    • System.Windows.Media.ScaleTransform
    • System.Windows.Media.SkewTransform
    • System.Windows.Media.TransformGroup

    由于继承自System.Windows.Media.Transform ,上述各种变换中都具有Value的只读属性,其返回 Matrix 结构。

    2. 画布Canvas控件中内容缩放方法简述

    网上存在一些解决方案,是通过给Canvas外层添加Border,缩放变换Canvas本省来达到缩放的目的。该方法无法控制Canvas中某一控件不缩放,或各控件非同时缩放,故这里不使用该方法进行缩放,而是监听Canvas的MouseWheel事件并遍历Canvas.Children,对子控件单独控制。控制的基本思想在于,为子控件附加CanZoom附加属性,并附加子控件各自的缩放信息,以此为根据进行缩放变换。
    为方便起见,下节直接构造一个新的布局控件 CanvasEx ,其继承于Canvas。

    3. CanvasEx具体实现及解读

    废话不多说,先上代码:

    public class CanvasEx : Canvas
    {
    	private bool _onKeyboardCommand = false;
    	private bool _onMouseButtonCommand = false;
    	private Point _previousDraggingPoint;
    
    	public static Cursor DragCursor => new Cursor(AppDomain.CurrentDomain.BaseDirectory + "DragHand.cur");
    	private Cursor _previousCursor;
    
    	[Browsable(false)]
    	private bool IsDragging
    	{
    		get { return (bool)GetValue(IsDraggingProperty); }
    		set { SetValue(IsDraggingProperty, value); }
    	}
    
    	// Using a DependencyProperty as the backing store for IsDragging.  This enables animation, styling, binding, etc...
    	public static readonly DependencyProperty IsDraggingProperty =
    		DependencyProperty.Register("IsDragging", typeof(bool), typeof(CanvasEx), new PropertyMetadata(false));
    
    	[Browsable(false)]
    	private bool IsZooming
    	{
    		get { return (bool)GetValue(IsZoomingProperty); }
    		set { SetValue(IsZoomingProperty, value); }
    	}
    
    	// Using a DependencyProperty as the backing store for IsZooming.  This enables animation, styling, binding, etc...
    	public static readonly DependencyProperty IsZoomingProperty =
    		DependencyProperty.Register("IsZooming", typeof(bool), typeof(CanvasEx), new PropertyMetadata(false));
    
    	public static bool GetCanDrag(DependencyObject obj)
    	{
    		return (bool)obj.GetValue(CanDragProperty);
    	}
    
    	public static void SetCanDrag(DependencyObject obj, bool value)
    	{
    		obj.SetValue(CanDragProperty, value);
    	}
    
    	// Using a DependencyProperty as the backing store for CanDrag.  This enables animation, styling, binding, etc...
    	public static readonly DependencyProperty CanDragProperty =
    		DependencyProperty.RegisterAttached("CanDrag", typeof(bool), typeof(CanvasEx), new PropertyMetadata(false));
    
    	public static Vector GetDraggingVector(DependencyObject obj)
    	{
    		return (Vector)obj.GetValue(DraggingVectorProperty);
    	}
    
    	private static void SetDraggingVector(DependencyObject obj, Vector value)
    	{
    		obj.SetValue(DraggingVectorProperty, value);
    	}
    
    	// Using a DependencyProperty as the backing store for DraggingVector.  This enables animation, styling, binding, etc...
    	public static readonly DependencyProperty DraggingVectorProperty =
    		DependencyProperty.RegisterAttached("DraggingVector", typeof(Vector), typeof(CanvasEx), new PropertyMetadata(default(Vector)));
    
    	public static bool GetCanZoom(DependencyObject obj)
    	{
    		return (bool)obj.GetValue(CanZoomProperty);
    	}
    
    	public static void SetCanZoom(DependencyObject obj, bool value)
    	{
    		obj.SetValue(CanZoomProperty, value);
    	}
    
    	// Using a DependencyProperty as the backing store for CanZoom.  This enables animation, styling, binding, etc...
    	public static readonly DependencyProperty CanZoomProperty =
    		DependencyProperty.RegisterAttached("CanZoom", typeof(bool), typeof(CanvasEx), new PropertyMetadata(false));
    
    	public static double GetZoomingScale(DependencyObject obj)
    	{
    		return (double)obj.GetValue(ZoomingScaleProperty);
    	}
    
    	private static void SetZoomingScale(DependencyObject obj, double value)
    	{
    		obj.SetValue(ZoomingScaleProperty, value);
    	}
    
    	// Using a DependencyProperty as the backing store for ZoomingScale.  This enables animation, styling, binding, etc...
    	public static readonly DependencyProperty ZoomingScaleProperty =
    		DependencyProperty.RegisterAttached("ZoomingScale", typeof(double), typeof(CanvasEx), new PropertyMetadata(1.0));
    
    	public double ScaleSpacing
    	{
    		get { return (double)GetValue(ScaleSpacingProperty); }
    		set { SetValue(ScaleSpacingProperty, value); }
    	}
    
    	// Using a DependencyProperty as the backing store for ScaleSpacing.  This enables animation, styling, binding, etc...
    	public static readonly DependencyProperty ScaleSpacingProperty =
    		DependencyProperty.Register("ScaleSpacing", typeof(double), typeof(CanvasEx), new PropertyMetadata(0.1));
    
    	private static Vector GetScaleMoveVector(DependencyObject obj)
    	{
    		return (Vector)obj.GetValue(ScaleMoveVectorProperty);
    	}
    
    	private static void SetScaleMoveVector(DependencyObject obj, Vector value)
    	{
    		obj.SetValue(ScaleMoveVectorProperty, value);
    	}
    	[Browsable(false)]
    	// Using a DependencyProperty as the backing store for ScaleMoveVector.  This enables animation, styling, binding, etc...
    	public static readonly DependencyProperty ScaleMoveVectorProperty =
    		DependencyProperty.RegisterAttached("ScaleMoveVector", typeof(Vector), typeof(CanvasEx), new PropertyMetadata(default(Vector)));
    
    
    	protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    	{
    		this._onMouseButtonCommand = true;
    		this.BeginDragging(e);
    		base.OnMouseLeftButtonDown(e);
    	}
    
    	protected override void OnMouseMove(MouseEventArgs e)
    	{
    		base.OnMouseMove(e);
    		this.DraggingBehavior(e);
    	}
    
    	protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    	{
    		this.EndDragging();
    		this._onMouseButtonCommand = false;
    		base.OnMouseLeftButtonUp(e);
    	}
    
    	protected override void OnMouseWheel(MouseWheelEventArgs e)
    	{
    		base.OnMouseWheel(e);
    		this.ZoomBehavior(e);
    	}
    
    	private void KeyDownJudgment()
    	{
    		if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
    		{
    			this._onKeyboardCommand = true;
    		}
    		else
    		{
    			this._onKeyboardCommand = false;
    		}
    	}
    
    	private void BeginDragging(MouseButtonEventArgs e)
    	{
    		if (!this.IsDragging)
    		{
    			this.CaptureMouse();
    			KeyDownJudgment();
    			bool onDragCommand = this._onKeyboardCommand && this._onMouseButtonCommand;
    			this.SetValue(CanvasEx.IsDraggingProperty, onDragCommand);
    			if (onDragCommand)
    			{
    				this._previousDraggingPoint = e.GetPosition(this);
    				this._previousCursor = this.Cursor;
    				this.Cursor = CanvasEx.DragCursor;
    			}
    			else
    			{
    				base.ReleaseMouseCapture();
    			}
    		}
    	}
    
    	private void DraggingBehavior(MouseEventArgs e)
    	{
    		if (this.IsDragging)
    		{
    			KeyDownJudgment();
    			bool onDragCommand = this._onKeyboardCommand && this._onMouseButtonCommand;
    			if (!onDragCommand)
    			{
    				EndDragging();
    				return;
    			}
    			if (e.MouseDevice.LeftButton == MouseButtonState.Pressed)
    			{
    				var p = e.GetPosition(this);
    				if (!VisualTreeHelper.GetContentBounds(this).Contains(p))
    				{
    					EndDragging();
    					return;
    				}
    				Vector v = p - this._previousDraggingPoint;
    				if (v.LengthSquared <= 0)
    				{
    					if (e.MouseDevice.Captured == this)
    					{
    						base.ReleaseMouseCapture();
    					}
    					this.SetValue(CanvasEx.IsDraggingProperty, false);
    					return;
    				}
    				foreach (var child in this.Children)
    				{
    					UIElement ch = child as UIElement;
    					if (CanvasEx.GetCanDrag(ch))
    					{
    						try
    						{
    							Matrix originalMatTrans = ch.RenderTransform.Value;
    							originalMatTrans.OffsetX += v.X;
    							originalMatTrans.OffsetY += v.Y;
    							ch.RenderTransform = new MatrixTransform(originalMatTrans);
    							CanvasEx.SetDraggingVector(ch, CanvasEx.GetDraggingVector(ch) + v);
    						}
    						catch (Exception exception)
    						{
    							throw exception;
    						}
    					}
    				}
    				this._previousDraggingPoint = e.GetPosition(this);
    			}
    		}
    	}
    
    	private void EndDragging()
    	{
    		if (base.IsMouseCaptured && this.IsDragging)
    		{
    			this.SetValue(CanvasEx.IsDraggingProperty, false);
    			base.ReleaseMouseCapture();
    			this.Cursor = this._previousCursor;
    		}
    	}
    
    	private void ZoomBehavior(MouseWheelEventArgs e)
    	{
    		KeyDownJudgment();
    		if (!this._onKeyboardCommand)
    		{
    			return;
    		}
    		var p = e.GetPosition(this);
    		if (!VisualTreeHelper.GetContentBounds(this).Contains(p))
    		{
    			return;
    		}
    		int delta = e.Delta / 120;
    		foreach (var child in this.Children)
    		{
    			UIElement ch = child as UIElement;
    			if (CanvasEx.GetCanZoom(ch))
    			{
    				var zs = Math.Round(CanvasEx.GetZoomingScale(ch) + this.ScaleSpacing * delta, 2);
    				if (zs < 0.1 || zs > 10)
    				{
    					continue;
    				}
    				Matrix mat = ch.RenderTransform.Value;
    				var ps1 = mat.M11;
    				var ps2 = mat.M22;
    				var s1 = Math.Round(mat.M11 + this.ScaleSpacing * delta, 2);
    				if (s1 <= 0)
    				{
    					continue;
    				}
    				var s2 = Math.Round(mat.M22 + this.ScaleSpacing * delta, 2);
    				if (s2 <= 0)
    				{
    					continue;
    				}
    				mat.M11 = s1;
    				mat.M22 = s2;
    
    				Point rp = ch.TransformToAncestor(this).Transform(new Point());
    				Rect rect = new Rect(rp, new Point((2 * p.X) - rp.X, (2 * p.Y) - rp.Y));
    
    				bool IsTopLeft = IsApproximate((rect.TopLeft - rp).LengthSquared, 0);
    				bool IsTopRight = IsApproximate((rect.TopRight - rp).LengthSquared, 0);
    				bool IsBottomLeft = IsApproximate((rect.BottomLeft - rp).LengthSquared, 0);
    				bool IsBottomRight = IsApproximate((rect.BottomRight - rp).LengthSquared, 0);
    
    				rect.Width *= s1 / ps1;
    				rect.Height *= s2 / ps2;
    				rect.Offset(p - GetCenterPoint(ref rect));
    
    				Vector sMV;
    
    				if (IsTopLeft)
    				{
    					sMV = rect.TopLeft - rp;
    				}
    				else
    				if (IsTopRight)
    				{
    					sMV = rect.TopRight - rp;
    				}
    				else
    				if (IsBottomLeft)
    				{
    					sMV = rect.BottomLeft - rp;
    				}
    				else
    				if (IsBottomRight)
    				{
    					sMV = rect.BottomRight - rp;
    				}
    				else
    				{
    					System.Diagnostics.Debug.WriteLine("Error");
    					continue;
    				}
    				mat.OffsetX += sMV.X;
    				mat.OffsetY += sMV.Y;
    				ch.RenderTransform = new MatrixTransform(mat);
    				CanvasEx.SetScaleMoveVector(ch, CanvasEx.GetScaleMoveVector(ch) + sMV);
    				CanvasEx.SetZoomingScale(ch, zs);
    			}
    		}
    	}
    
    	private Point GetCenterPoint(ref Rect rect)
    	{
    		return new Point((rect.TopLeft.X + rect.BottomRight.X) * 0.5, (rect.TopLeft.Y + rect.BottomRight.Y) * 0.5);
    	}
    
    	private bool IsApproximate(double value, double referance) 
    		=> value == referance 
    			? true 
    			: value <= referance + 1e-5 && value >= referance - 1e-5;
    
    	public void ResetTransform(UIElement child)
    	{
    		if (this.Children.Contains(child))
    		{
    			var dv = CanvasEx.GetDraggingVector(child);
    			var sv = CanvasEx.GetScaleMoveVector(child);
    			Matrix originalMatTrans = child.RenderTransform.Value;
    			originalMatTrans.OffsetX -= dv.X + sv.X;
    			originalMatTrans.OffsetY -= dv.Y + sv.Y;
    			var zs = CanvasEx.GetZoomingScale(child);
    			originalMatTrans.M11 -= zs - 1;
    			originalMatTrans.M22 -= zs - 1;
    			child.RenderTransform = new MatrixTransform(originalMatTrans);
    			CanvasEx.SetDraggingVector(child, new Vector());
    			CanvasEx.SetScaleMoveVector(child, new Vector());
    			CanvasEx.SetZoomingScale(child, 1.0);
    		}
    	}
    	public void ResetTransform()
    	{
    		foreach (UIElement child in this.Children)
    		{
    			ResetTransform(child);
    		}
    	}
    }
    

    可以看到,CanvasEx 主要简单实现了拖拽和缩放功能。对于缩放,其核心在于ZoomBehavior方法,下面对其详细解说。
    对CanvasEx的内容缩放,我想看到的是,以鼠标当前位置进行缩放,缩放或平移完之后,可以通过一些方法还原到原来位置的效果。
    曾经尝试通过对RenderTransform.Value使用Matrix.ScaleAt方法来实现以鼠标当前位置进行缩放。虽然单缩放操作堪称完美(缩小后再放大能回到原来位置),但是一旦牵扯到平移-缩放混合变换时就失效了。在ResetTransform后,总是会出现回到原来位置的现象,目前还未知具体原因,可能是由于矩阵乘法先后的缘故导致的。
    所以,我直接采用了(暴力)矩形(Rect 结构)缩放的方法。首先构造一个以鼠标鼠标当前位置为中心,控件坐标为角点的矩形;缩放大小(此时缩放是以矩形左上角点为原点缩放的),并手动平移,使得变换前后的矩形中心对齐,计算变换后的角点。此时可以获得因变换而生成的缩放大小和缩放位移,以附加属性的方式附加在控件上。
    此方法对内容缩放变换,不会破坏子控件原来的RenderTransform。
    在xaml中可以这样使用CanvasEx的一些属性,例如其存在一个Rectangle控件:

    <Rectangle 
    	x:Name="R" 
    	Fill="#FFC72121" 
    	Canvas.Left="150" 
    	Canvas.Top="150"
    	Width="50" 
    	Height="50" 
    	local:CanvasEx.CanDrag="True" 
    	local:CanvasEx.CanZoom="True" 
    >
    	<Rectangle.RenderTransform>
    		<TranslateTransform X="10" Y="30"/>
    	</Rectangle.RenderTransform>
    </Rectangle>
    

    甚至可以获取缩放或平移的信息与TextBlock绑定:

    <!--平移信息-->
    <TextBlock 
    	FontSize="14" 
    	FontWeight="Bold" 
    	Canvas.Left="330" 
    	Canvas.Bottom="20"
    	Text="{Binding Path=(local:CanvasEx.DraggingVector), Mode=OneWay, ElementName=R}" 
    />
    <!--缩放信息-->
    <TextBlock 
    	FontSize="14" 
    	FontWeight="Bold" 
    	Canvas.Left="200" 
    	Canvas.Bottom="80"
    	Text="{Binding Path=(local:CanvasEx.ZoomingScale), Mode=OneWay, ElementName=R}" 
    />
    
    展开全文
  • 将AutoCAD软件生成的DXF文件通过VS建立WPF工程,实现将AutoCAD软件中生成的工件图在WPFCanvas中画出来,并实现一定的功能。
  • wpf 可缩放的canvas

    2016-06-02 11:34:32
    这个是转的一个可缩放的canvas例子,可以直接用,很方便
  • WPF Canvas

    2019-07-17 19:54:42
    Canvas是最基本的面板,只是一个存储控件的容器,它不会自动调整内部元素的排列及大小,它仅支持用显式坐标定位控件,它也允许指定相对任何角的坐标,而不仅仅是左上角。可以使用Left、Top、Right、Bottom附加属性在...

    Canvas是最基本的面板,只是一个存储控件的容器,它不会自动调整内部元素的排列及大小,它仅支持用显式坐标定位控件,它也允许指定相对任何角的坐标,而不仅仅是左上角。可以使用LeftTopRight Bottom附加属性在Canvas中定位控件。通过设置LeftRight属性的值表示元素最靠近的那条边,应该与Canvas左边缘或右边缘保持一个固定的距离,设置TopBottom的值也是类似的意思。实质上,你在选择每个控件停靠的角时,附加属性的值是作为外边距使用的。如果一个控件没有使 用任何附加属性,它会被放在Canvas的左上方(等同于设置LeftTop0)。

     

    Canvas的主要用途是用来画图。Canvas默认不会自动裁减超过自身范围的内容,即溢出的内容会显示在Canvas外面,这是因为默认 ClipToBounds=”False”;我们可以通过设置ClipToBounds=”True”来裁剪多出的内容。

    <Window x:Class="WpfCS.MainWindow"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

            xmlns:local="clr-namespace:WpfCS"

            mc:Ignorable="d"

            Title="MainWindow" Height="350" Width="525">

        <Grid>

            <Canvas Margin="0,0,0,0" Background="White">

                <Rectangle Fill="Blue"

                    Stroke="Azure"

                    Width="250"

                    Height="2000"

                    Canvas.Left="210" Canvas.Top="101" ClipToBounds="True">

                  

                </Rectangle>

                <Ellipse Fill="Red"

                    Stroke="Green"

                    Width="250" Height="100"

                    Panel.ZIndex="1"

                    Canvas.Left="65" Canvas.Top="45"/>

                <Button Width="100" Canvas.Left="50">后台代码实现</Button>

            </Canvas>

            <Canvas>

                <Button Name="btnByCode" Click="btnByCode_Click">后台代码实现</Button>

            </Canvas>

        </Grid>

    </Window>

     

     

    后台代码,点击实现Canvas画布

    <Window x:Class="WpfCS.MainWindow"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

            xmlns:local="clr-namespace:WpfCS"

            mc:Ignorable="d"

            Title="MainWindow" Height="350" Width="525">

        <Grid>

            <Canvas >

                <Button Name="btnByCode" Click="btnByCode_Click">点击按钮</Button>

            </Canvas>

        </Grid>

     

     

     

     

    private void btnByCode_Click(object sender, RoutedEventArgs e)

            {

                Canvas canv = new Canvas();

                //把canv添加为窗体的子控件

                this.Content = canv;

                canv.Margin = new Thickness(0, 0, 0, 0);

                canv.Background = new SolidColorBrush(Colors.White);

                //Rectangle

                Rectangle r = new Rectangle();

                r.Fill = new SolidColorBrush(Colors.Yellow);

                r.Stroke = new SolidColorBrush(Colors.Red);

                r.Width = 250;

                r.Height = 200;

                r.SetValue(Canvas.LeftProperty, (double)140);

                r.SetValue(Canvas.TopProperty, (double)50);

                canv.Children.Add(r);

                //Ellipse

                Ellipse el = new Ellipse();

                el.Fill = new SolidColorBrush(Colors.Green);

                el.Stroke = new SolidColorBrush(Colors.Blue);

                el.Width = 250;

                el.Height = 100;

                el.SetValue(Panel.ZIndexProperty, 1);

                el.SetValue(Canvas.LeftProperty, (double)10);

                el.SetValue(Canvas.TopProperty, (double)20);

                canv.Children.Add(el);

            }

     

     

     

    展开全文
  • WpfCanvas介绍

    千次阅读 2019-06-19 18:14:52
    从这篇文章开始是对WPF中的界面如何布局做一个较简单的介绍,大家都知道:UI是做好一个软件很重要的因素,如果没有一个漂亮的UI,功能做的再好也无法吸引很多用户使用,而且没有漂亮的界面,那么普通用户会感觉这个...
  • WPF之路——Canvas布局(画布)

    万次阅读 2013-11-26 19:43:26
    Canvas为容器控件,用于定位 1.基本应用 可以把Canvas比作一个坐标系,所有的元素通过设置坐标来决定其在坐标系中的位置.这个坐标系的原点并不是在中央,而是位于它的左上角.见下图 元素设置坐标的方法共有四个:  ...
  • 1 Canvas和基本形状 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <Canvas Margin="0,0,0,0...
  • *本人是个学习WPF的新手,期间遇到了很多问题,今天在项目上遇到了一个特别麻烦的问题,就是Canvas不能添加滑动条,我原先的想法是用Canvas作为父容器,之所以采用Canvas作为父容器,是因为它可以实现绝对布局,可以...
  • 有一些业务场景中我们需要拖动控件,上一篇文章《C# wpf 实现Grid内控件拖动》在Grid中实现的控件拖动,在Canvas中也可以实现控件拖动,通过设置Canvas的附加属性即可,根据鼠标的移动,设置相应Canvas的Left、Top。...
  • C# WPF Canvas绘图

    万次阅读 2016-12-16 11:34:34
    //清除canvas画布 DrawCanvas.Children.Clear(); //直线对象 Line mydrawline = new Line(); mydrawline.Stroke= Brushes.Black;//外宽颜色,在直线里为线颜色 mydrawline.StrokeThickness = 3;//线宽度 mydrawline....
  • WPFCanvas滚动条

    2021-02-23 23:06:58
    <UserControl x:Class="GraphAlgorithm.UCGraphCanvas" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc=...
  • WPF用鼠标在Canvas画线

    2015-03-19 10:42:35
    WPF用鼠标在Canvas画线,通过左击获得线的起点,通过移动并释放左键获得终点实现画线功能。
  • WPF C# Canvas上绘制文字

    千次阅读 2018-07-30 08:40:39
    private void drawText(double x, double y, string text, Color color,Canvas canvasObj) { TextBlock textBlock = new TextBlock(); textBlock.Text = text; te...
  • wpf canvas折线图和点图

    2021-03-30 15:17:37
    //-4是为了补偿圆点的大小,到精确的位置 Canvas.SetTop(dataEllipse, dataY_can[i] - dataEllipse.Height / 2); this.can.Children.Add(dataEllipse); System.Windows.Point linePoint = new System.Windows.Point...
  • WPF对象都具有RenderTransform的属性,可以通过设置RenderTransform来对WPF的元素进行变换,无论是控件还是形状都可以变换。典型的变换包括缩放和平移: (一)缩放 如果采用Canvas作为画板来绘制一些形状,想要...
  • WPF Canvas绘图

    2016-07-27 22:29:00
    Canvas节点下直接添加对应的节点即可,如直线节点Line,矩形节点Rectangle 2. 设置Canvas.Top等属性 Canvas.SetTop(Sharp,top) Rectangle rct = new Rectangle(); Canvas.SetLeft(rct, left); Canvas....
  • WPF Canvas 异形件颜色填充

    千次阅读 2018-03-01 11:01:29
    通过直线段画异形件,然后填充颜色 &...Canvas&gt; &lt;Path Stroke="DarkCyan" StrokeThickness="3"&gt; &lt;Path.Fill&gt; &lt;LinearGradientBrush&gt; ...
  • WPF Canvas中元素的定位

    万次阅读 2009-10-21 21:27:00
    Canvas中元素的定位 Canvas,DockPanel,Grid,StackPanel,WrapPanel是WPF中的5个布局控件.每个布局控件都有不同的应用场景.如果您要对元素进行精确的定位,那么就需要使用Canvas了. 可以把Canvas比作一个坐标系,...
  • WPF 中保存 window(窗口)或者canvas成图片
  • WPF对象都具有RenderTransform的属性,可以通过设置RenderTransform来对WPF的元素进行变换,无论是控件还是形状都可以变换。典型的变换包括缩小放大与平移。 (一)缩放 (见前一篇文章) (二)平移 为了实现平...
  • WPF 中利用Canvas 做用户自定义报表

    热门讨论 2013-03-25 15:18:53
    功能描述:wpf 环境,用户可以自己拖动 调整报表内容,可以保存模板到服务器,可以加载服务器模板功能,可以动态创建每页4条数据,每页2行。可以自动根据id 生成条形码功能。可以更改报表字体样式,是否显示隐藏报表...
  • 参考文章:https://blog.csdn.net/u012551485/article/details/72354264/ 将界面上的所有其它的控件放在Canvas中即可,模板如下:
  • WPF 使用Canvas做轮播图

    2020-09-07 15:01:01
    前端有的,WPF也要有! 先看效果: 方法:使用Canvas设计UserControl。 一、新建UserControl: <UserControl x:Class="WpfApp1.Slider" xmlns=...
  • WPF教程(二十四)Canvas

    千次阅读 2016-10-22 17:55:38
    Canvas大概是最简单的面板了。在默认情况下,它什么都不做,你把控件放到它里面,然后通过具体的坐标来指定位置。 如果你之前使用过其他UI库如WinForms,Canvas会让你感觉很亲切。然而它趋向于使用绝对坐标来控制子...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 8,544
精华内容 3,417
关键字:

canvaswpf