首页 > 其他分享 >【WPF】绘制水平尺

【WPF】绘制水平尺

时间:2022-10-29 23:12:57浏览次数:51  
标签:DependencyProperty int 水平 System using WPF 绘制 public Maximum

储备知识

Pen、Brushes、绘图类Drawing、DrawingVisual类的使用

效果

 

 

源代码

using System;
using System.Collections.Generic;
using Drawing = System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace VisualPlay
{

    public class CustomTickBar : TickBar
    {


        ///隔N个Frequency 写上刻度文字
        public int IntervalText
        {
            get { return (int)GetValue(IntervalTextProperty); }
            set { SetValue(IntervalTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IntervalTextProperty =
            DependencyProperty.Register("IntervalText", typeof(int), typeof(CustomTickBar), new PropertyMetadata(5));



        public int StartAxisY
        {
            get { return (int)GetValue(StartAxisYProperty); }
            set { SetValue(StartAxisYProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StartAxisY.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StartAxisYProperty =
            DependencyProperty.Register("StartAxisY", typeof(int), typeof(CustomTickBar), new PropertyMetadata(0));



       
        void OnRenderHorizontalRuler(DrawingContext dc)
        {
            Double tickFrequencySize;
            Brush foreBrush = this.Fill;
            Pen line_Pen = new Pen(foreBrush, 1);

            FormattedText font = null;
            Size size = new Size(base.ActualWidth, base.ActualHeight);
            int tickCount = (int)((this.Maximum - this.Minimum) / this.TickFrequency) + 1;
            if ((this.Maximum - this.Minimum) % this.TickFrequency == 0)
                tickCount -= 1;
            tickFrequencySize = (size.Width * this.TickFrequency / (this.Maximum - this.Minimum));
            string text = "";
            double num = this.Maximum - this.Minimum;
            int i = 0;
            for (i = 0; i <= tickCount; i++)
            {
                ///隔N个Frequency 写上刻度
                if (i % IntervalText == 0)
                {
                    text = Convert.ToString(Convert.ToInt32(this.Minimum + this.TickFrequency * i), 10);
                    font = new FormattedText(text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 14, foreBrush);
                    dc.DrawText(font, new Point(tickFrequencySize * i, 20));
                    //
                    Point startpoint = new Point(tickFrequencySize * i, StartAxisY + 4);
                    Point endpoint = new Point(tickFrequencySize * i, StartAxisY + 24);
                    dc.DrawLine(line_Pen, startpoint, endpoint);
                }
                else
                {
                    dc.DrawLine(line_Pen, new Point(tickFrequencySize * i, StartAxisY + 14), new Point(tickFrequencySize * i, StartAxisY + 24));
                }
            }

        }
        protected override void OnRender(DrawingContext dc)
        {
            OnRenderHorizontalRuler(dc);
        }
    }
}

 

标签:DependencyProperty,int,水平,System,using,WPF,绘制,public,Maximum
From: https://www.cnblogs.com/cdaniu/p/16840163.html

相关文章

  • WPF placeHolder效果的样式设定
    <UserControlx:Class="wpfTestStudio.textPlaceHolderDemo"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="......
  • SignalR即时通讯 :WPF+Xamarin.Android
    1、背景介绍PDA操作物料出入库信息投送到大屏显示2、WPF服务端添加OWIN、Microsoft.AspNet.SignalR.Core引用2.1、创建启动类publicclassStartup{publicvoid......
  • 管理水平的四个级别
    不敢说我懂管理,以前也看过很多管理方面的书籍,但纸上得来终觉浅。只是在实践中,加上自己的体悟,逐步提高了自己对管理的认知。我相信天道中所说的一句话:只要不是我觉到,悟到......
  • 【WPF】命中测试(Hitest) 开篇
       命中测试支持VisualTreeHelper类中HitTest方法的用途是确定几何或点坐标值是否在给定对象的呈现内容内,如控件或图形元素。例如,可以使用命中测试确定对象边框......
  • 【WPF】Hitest 命中测试
    概述: WPF中的Canvas是常用的一个绘图控件,可以方便地在Canvas中添加我们需要处理的各种元素如:图片、文字等。但Canvas中元素增加到一定数量,并且有重合的时候,我们如何通过......
  • 【WPF】绘制图形的三种方法及区别
     WPF中用于绘图的类主要有三个,分别是Shape类、Drawing类和DrawingVisual类,Shape类存在于System.Windows.Shapes命,而Drawing类和DrawingVisual类则都存在于System.Window......
  • 第5题:试设计一巴特沃斯低通滤波器,其处理的数据采样率为2Hz,允许0.6Hz以下的信号通过。
    首先对题目进行分析,采样率为2Hz,允许0.6Hz以下信号通过说明该滤波器的截止频率为0.6,也就是幅频增益下降3dB或者是最大幅频增益的0.707倍(对应的是根号2分之一)题目的意思应该......
  • wpf .net core win7 独立运行补丁安装记录
     Windows7系统上,根据dotnet官方文档,需要安装上KB2533623补丁方案如下:首先使用 fx2.0 写一个启动器 如果检测可以运行则拉起.net core 主程序 否则弹出命令......
  • Winform GDI 系列(2) 窗体边框重绘制
    ///<summary>///窗体边框重绘制///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidForm1_Paint(......
  • Winform 无边框 绘制自定义边框
    privatevoidFrmLogin_Paint(objectsender,PaintEventArgse){///自定义绘制边框颜色//e.Graphics.DrawRectangle(Pens.DarkOliveGr......