首页 > 其他分享 >WPF cvs draw rectangle and line

WPF cvs draw rectangle and line

时间:2024-12-15 20:23:17浏览次数:3  
标签:draw idx Windows cvs System startPt using line WPF

  1 //xaml
  2 <Window x:Class="WpfApp67.MainWindow"
  3         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  7         WindowState="Maximized"
  8         xmlns:local="clr-namespace:WpfApp67"
  9         mc:Ignorable="d"
 10         Title="MainWindow" Height="450" Width="800">
 11     <Grid>
 12         <Grid.RowDefinitions>
 13             <RowDefinition/>
 14             <RowDefinition Height="50"/>
 15         </Grid.RowDefinitions>
 16         <Canvas x:Name="cvs"
 17                 Grid.Row="0"
 18                 Background="Transparent"
 19                 PreviewMouseDown="cvs_PreviewMouseDown"
 20                 PreviewMouseUp="cvs_PreviewMouseUp"
 21                 PreviewMouseMove="cvs_PreviewMouseMove">
 22             <!--<Rectangle x:Name="rect"
 23                        Visibility="Collapsed"
 24                        Fill="LightBlue"
 25                        Stroke="DarkBlue"
 26                        StrokeThickness="5">                
 27             </Rectangle>-->
 28         </Canvas>
 29         <StatusBar Grid.Row="1">
 30             <StatusBarItem>
 31                 <TextBlock x:Name="statusTbk"
 32                            FontSize="20"/>
 33             </StatusBarItem>
 34         </StatusBar>
 35     </Grid>
 36 </Window>
 37 
 38 
 39 
 40 
 41 //cs
 42 using System;
 43 using System.Collections.Generic;
 44 using System.Linq;
 45 using System.Text;
 46 using System.Threading.Tasks;
 47 using System.Windows;
 48 using System.Windows.Controls;
 49 using System.Windows.Data;
 50 using System.Windows.Documents;
 51 using System.Windows.Input;
 52 using System.Windows.Media;
 53 using System.Windows.Media.Imaging;
 54 using System.Windows.Navigation;
 55 using System.Windows.Shapes;
 56 
 57 namespace WpfApp67
 58 {
 59     /// <summary>
 60     /// Interaction logic for MainWindow.xaml
 61     /// </summary>
 62     public partial class MainWindow : Window
 63     {
 64         private Point startPt, endPt;
 65         string statusMsg = string.Empty;
 66         int idx = 0;
 67         List<Rectangle> rectsList = new List<Rectangle>();
 68 
 69         List<Line> linesList = new List<Line>();
 70 
 71         public MainWindow()
 72         {
 73             InitializeComponent();
 74         }
 75 
 76         private void cvs_PreviewMouseDown(object sender, MouseButtonEventArgs e)
 77         {
 78             startPt = e.GetPosition(cvs);
 79             rectsList.Add(new Rectangle());
 80             linesList.Add(new Line());
 81         }
 82 
 83         private void cvs_PreviewMouseUp(object sender, MouseButtonEventArgs e)
 84         {
 85             if (!cvs.Children.Contains(rectsList[idx]))
 86             {
 87                 rectsList[idx].ToolTip = $"Rect[{idx+1}],{Guid.NewGuid().ToString("N")}";
 88                 cvs.Children.Add(rectsList[idx]);
 89             }
 90             if (!cvs.Children.Contains(linesList[idx]))
 91             {
 92                 linesList[idx].ToolTip = $"Line[{idx + 1}],{Guid.NewGuid().ToString("N")}";
 93                 cvs.Children.Add(linesList[idx]);
 94             }
 95             ++idx;
 96         }
 97 
 98         private void cvs_PreviewMouseMove(object sender, MouseEventArgs e)
 99         {
100             if (Mouse.LeftButton == MouseButtonState.Pressed)
101             {
102                 endPt = e.GetPosition(cvs);
103                 Rectangle rect = rectsList[idx];
104                 rect.Stroke = new SolidColorBrush(Colors.DarkBlue);
105                 rect.StrokeThickness = 5;
106                 rect.Visibility = Visibility.Visible;
107                 rect.Width = Math.Abs(endPt.X - startPt.X);
108                 rect.Height = Math.Abs(endPt.Y - startPt.Y);
109                 Canvas.SetLeft(rect, startPt.X);
110                 Canvas.SetTop(rect, startPt.Y);
111 
112 
113                 Line ln = linesList[idx];
114                 ln.X1 = startPt.X+2.5;
115                 ln.Y1 = startPt.Y+2.5;
116                 ln.X2 = endPt.X-2.5;
117                 ln.Y2 = endPt.Y-2.5;
118                 ln.Stroke = new SolidColorBrush(Colors.DarkBlue);
119                 ln.StrokeThickness = 1;
120               
121                 statusMsg = $"Start:({startPt.X},{startPt.Y}),end point:({endPt.X},{endPt.Y})";
122                 UpdateStatus(statusMsg);
123                 if (!cvs.Children.Contains(rectsList[idx]))
124                 {
125                     cvs.Children.Add(rectsList[idx]);
126                 }
127                 if (!cvs.Children.Contains(linesList[idx]))
128                 {
129                     cvs.Children.Add(linesList[idx]);
130                 }
131             }
132         }
133 
134         private void UpdateStatus(string msg)
135         {
136             Application.Current.Dispatcher.Invoke(new Action(() =>
137             {
138                 statusTbk.Text = msg;
139             }));
140         }
141     }
142 }

 

 

 

 

 

 

 

标签:draw,idx,Windows,cvs,System,startPt,using,line,WPF
From: https://www.cnblogs.com/Fred1987/p/18608642

相关文章

  • PresentationFontCache.exe 是与 Windows Presentation Foundation (WPF) 相关的一个
    PresentationFontCache.exe是与WindowsPresentationFoundation(WPF)相关的一个系统进程,它用于缓存字体信息,以提高WPF应用程序的启动和运行速度。具体来说,它是WindowsPresentationFoundationFontCache3.0.0.0的一部分,通常会在运行WPF应用程序时启动。下面是对这个......
  • WPF TreeView实现固定表头
    1、在WPF中TreeView默认不支持固定表头的我们可以修改样式实现固定表头 新建一个TreeListView类然后继承TreeView代码如下publicclassTreeListView:TreeView,IDisposable{publicTreeListView(){//this.Loaded+=TreeListView_Loa......
  • Git之git push报错protocol error: bad line length 8192怎么处理
    故障现象gitpull报错$gitpushCountingobjects:40,done.Deltacompressionusingupto32threads.Compressingobjects:100%(38/38),done.fatal:protocolerror:badlinelength819247.18MiB/sfatal:sha1file'<stdout>'writeerror:Bro......
  • P6474 [NOI Online #2 入门组] 荆轲刺秦王 题解
    荆轲将会臭名昭著首先$15$做法很简单,那就是直接`cout<<-1`考虑用BFS来解思路很简单,但是怎么求每个士兵的控制范围呢?直接暴力时间复杂度是$O(nma^2)$当然过不了一定会TLE。所以,只需要差分+前缀和即可。说起来简单,实现起来也简单。然后,单打广搜大家应该都会了,可是出题......
  • GeoGebra中级篇026—GeoGebra的极线或直径线工具(Polar or Diameter Line Tool)
    注意:关注微信公众号“第五智能”,免费查阅全系列文章。在几何学的学习和探索中,极线和直径线是非常重要的概念,它们在圆、椭圆及其他几何图形中扮演着关键角色。在GeoGebra中,极线和直径线工具为我们提供了一个强大的方式来可视化和构建这些几何关系,帮助我们更好地理解几何构......
  • Transformers-pipline
    HFTransformersPipelinesPipelines接口方式任务名称参数名称参数描述sentiment-analysismodel指定使用的模型名称或路径。tokenizer指定使用的分词器名称或路径。framework选择使用的深度学习框架,"pt"表示PyTorch,"tf"表示TensorFlow。device设......
  • 为什么应该尽量减少draw call?
    问题非常经典,为什么应该尽量减少DrawCall是图形渲染优化中的核心问题之一。简单来说,DrawCall的瓶颈并不仅仅是数据量,而是它对CPU/GPU协同工作和渲染流水线效率的影响。以下我们详细分析DrawCall的瓶颈来源、合并DrawCall提高效率的原因。1.什么是DrawCall......
  • adb devices 后显示offline的解决方法
    一、遇到的问题上午在执行monkey,测试的好好的,下午一觉睡醒之后,再次操作的时候,发现进入不了手机。结果如图一连接手机,显示连接如下:adbdevices显示如下: 二、解决方法1、重启服务adbkill-serveradbstart-server 2、用命令adbconnect连接,再次查看就正常了。  ......
  • Transformers 框架 Pipeline 任务详解(三):词元分类(token-classification)和命名实体识别
    微信公众号:老牛同学公众号标题:Transformers框架Pipeline任务详解(三):词元分类(token-classification)和命名实体识别公众号链接:https://mp.weixin.qq.com/s/r2uFCwPZaMeDL_eiQsEmIQ在自然语言处理(NLP)领域,Token-Classification(词元分类)任务是一项关键的技术,这项技术广泛应用于......
  • Data ETL Pipelines Using Terraform
    https://www.dheeraj3choudhary.com/automate-s3-data-etl-pipelines-with-aws-glue-using-terraform/ CreatinganETLpipelinewithAWSGlueandTerraformcansignificantlystreamlineyourdataprocessingtasks.Inthisblog,wewillwalkyouthroughthepro......