当前位置: 首页 > news >正文

WPF draw triangle and add contextmenu, menuitem programmatically

 private void DrawTriangleInCanvas(){try{triangle = new Polygon(){Stroke = Brushes.Black,StrokeThickness = 3,Fill = Brushes.Red};double height = (Math.Sqrt(3) / 2) * sideLength;Point centerPt = new Point(cvs.ActualWidth / 2, cvs.ActualHeight / 2);Point topPt = new Point(centerPt.X, cvs.ActualHeight / 2 - height / 2);Point leftPt = new Point(centerPt.X - sideLength / 2, centerPt.Y + height / 2);Point rightPt = new Point(centerPt.X + sideLength / 2, centerPt.Y + height / 2);triangle.Points.Add(leftPt);triangle.Points.Add(rightPt);triangle.Points.Add(topPt);AddContextMenuWithTriangle(triangle);cvs.Children.Add(triangle);}catch (Exception ex){MessageBox.Show(ex.Message);}}private void AddContextMenuWithTriangle(Polygon triangle){ContextMenu cxt = new ContextMenu();MenuItem saveMenuItem = new MenuItem();saveMenuItem.Header = "Save";saveMenuItem.Click += (s, e) =>{SaveMenuItemMethod();};MenuItem removeMenuItem = new MenuItem();removeMenuItem.Header = "Delete";removeMenuItem.Click += (s, e) =>{DeleteMenuItemMethod();};cxt.Items.Add(saveMenuItem);cxt.Items.Add(removeMenuItem);triangle.ContextMenu = cxt;}private void DeleteMenuItemMethod(){MessageBox.Show("In Delete MenuItem!");}private void SaveMenuItemMethod(){MessageBox.Show($"In Save MenuItem!");}

 

 

 

 

image

 

 

 

image

 

 

 

 

 

image

 

 

 

 

 

image

 

//Window.xaml
<Window x:Class="WpfApp16.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:WpfApp16"mc:Ignorable="d"WindowState="Maximized"Title="MainWindow" Height="450" Width="800"><Grid><Canvas x:Name="cvs"Background="White"HorizontalAlignment="Stretch"VerticalAlignment="Stretch"/></Grid>
</Window>//Window.xaml.cs
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApp16
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{Polygon triangle = null;double sideLength = 100.0d;public MainWindow(){InitializeComponent();this.Loaded += MainWindow_Loaded;}private void MainWindow_Loaded(object sender, RoutedEventArgs e){DrawTriangleInCanvas();}private void DrawTriangleInCanvas(){try{triangle = new Polygon(){Stroke = Brushes.Black,StrokeThickness = 3,Fill = Brushes.Red};double height = (Math.Sqrt(3) / 2) * sideLength;Point centerPt = new Point(cvs.ActualWidth / 2, cvs.ActualHeight / 2);Point topPt = new Point(centerPt.X, cvs.ActualHeight / 2 - height / 2);Point leftPt = new Point(centerPt.X - sideLength / 2, centerPt.Y + height / 2);Point rightPt = new Point(centerPt.X + sideLength / 2, centerPt.Y + height / 2);triangle.Points.Add(leftPt);triangle.Points.Add(rightPt);triangle.Points.Add(topPt);AddContextMenuWithTriangle(triangle);cvs.Children.Add(triangle);}catch (Exception ex){MessageBox.Show(ex.Message);}}private void AddContextMenuWithTriangle(Polygon triangle){ContextMenu cxt = new ContextMenu();MenuItem saveMenuItem = new MenuItem();saveMenuItem.Header = "Save";saveMenuItem.Click += (s, e) =>{SaveMenuItemMethod();};MenuItem removeMenuItem = new MenuItem();removeMenuItem.Header = "Delete";removeMenuItem.Click += (s, e) =>{DeleteMenuItemMethod();};cxt.Items.Add(saveMenuItem);cxt.Items.Add(removeMenuItem);triangle.ContextMenu = cxt;}private void DeleteMenuItemMethod(){MessageBox.Show("In Delete MenuItem!");}private void SaveMenuItemMethod(){MessageBox.Show($"In Save MenuItem!");}}
}

 

http://www.jsqmd.com/news/5083/

相关文章:

  • 使用 SignalR 向前端推送图像
  • C# WPF实现ComboBox实时搜索与数据绑定 - 教程
  • 隐私保护与联邦学习文献阅读
  • Java实习模拟面试|离散数学|概率论|金融英语|数据库实战|职业规划|期末冲刺|今日本科计科要闻速递:技术分享与学习指南 - 实践
  • 学术写作
  • 2025.9.27
  • 深入解析:用 Spring Boot 打造 Docker 和 K8s 部署的硬核指南
  • 9.27(课后作业
  • 详细介绍:【序列晋升】45 Spring Data Elasticsearch 实战:3 个核心方案破解索引管理与复杂查询痛点,告别低效开发
  • 四则运算和验证码
  • 博客写作者该何去何从
  • 第一次课动手动脑合集
  • JSON dump in Ruby
  • 详细介绍:python+django/flask+uniapp基于微信小程序的瑜伽体验课预约系统
  • smartctl on FreeBSD: Please specify device type with the -d option.
  • GraphRAG对自然语言处理中深层语义分析的革命性影响与未来启示 - 教程
  • prefect
  • 生成算数问题*30
  • 6379:统计学生信息(使用动态链表完成)
  • 详细介绍:云原生 vs 传统部署
  • 使用IOT-Tree Server通过FINS协议连接欧姆龙CP2E-N系列PLC - 详解
  • 实用指南:疑难bug之正确处理InterruptedException
  • 课后作业1-3
  • GNSS精度判断和协方差矩阵 - MKT
  • 实用指南:clsx:高效处理 React 条件类名的实用工具
  • Insightly模板页面存储型XSS漏洞分析与复现
  • 记录 | 关于陪伴型交互AI的一些探讨
  • luogu P1719 最大加权矩形
  • CF2065D Skibidus and Sigma
  • 微信二次开发个人号api