`using System;

using System.Drawing;

using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace IntegrationAPP

{

public partial class Form2 : Form

{

public static float MonitorDPI = 96f ;

private int originLocation = 0; //坐标原地起始位置

private int maxScaleX = 1000; //X轴最大刻度

private int maxScaleY = 1000; //Y轴最大刻度

private float scaling = 1.0F; //缩放比例

private int offSetX = 0; //X轴偏移位置

private int offSetY = 0; //Y轴偏移位置

private Font font = new Font("Arial", 9); //刻度值显示字体

private TextureBrush textureBrush;

private Bitmap bit;

int x = 0;

int y = 0;

    public Form2()
{ InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
// bit = new Bitmap((int)(MonitorDPI / 25.4 * 1 * scaling) - offSetX, (int)(MonitorDPI / 25.4 * 1 * scaling) - offSetX);
Graphics g1 = CreateGraphics();
PaintEventArgs pe = new PaintEventArgs(g1, this.ClientRectangle);
RulerControl_Paint(g1,pe );
// g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// g1.Dispose();
// textureBrush = new TextureBrush(bit);//使用TextureBrush可以有效减少窗体拉伸时的闪烁
}
private void RulerControl_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int widthInmm = maxScaleX;
int heightInmm = maxScaleY; //绘制X轴
for (int i = 0; i <= widthInmm; i++)
{
SizeF size = g.MeasureString(Convert.ToString(i), font);
float x = originLocation + (float)(MonitorDPI / 25.4 * i * scaling) - offSetX;
if (x >= originLocation)
{
PointF start = new PointF(x, originLocation);
PointF end = new PointF(x, 3);
if (i % 5 == 0)
{
end = new PointF(x, 6);
}
if (i % 10 == 0 && i != 0)
{
end = new PointF(x, 12);
g.DrawString(Convert.ToString(i), font, Brushes.Black, new PointF(x - (float)(MonitorDPI / 25.4 * Convert.ToString(i).Length * scaling), 12));
}
g.DrawLine(Pens.Black, start, end);
}
}
g.DrawLine(Pens.Black, new PointF(originLocation, originLocation), new PointF(this.Width, originLocation)); //绘制Y轴
for (int i = 0; i <= heightInmm; i++)
{
SizeF size = g.MeasureString(Convert.ToString(i), font);
float y = originLocation + (float)(MonitorDPI / 25.4 * i * scaling) - offSetY;
if (y >= originLocation)
{
PointF start = new PointF(originLocation, y);
PointF end = new PointF(3, y);
if (i % 5 == 0)
{
end = new PointF(6, y);
}
if (i % 10 == 0 && i != 0)
{
end = new PointF(12, y);
g.DrawString(Convert.ToString(i), font, Brushes.Black, new PointF(12, y - (float)(MonitorDPI / 25.4 * Convert.ToString(i).Length * scaling)));
}
g.DrawLine(Pens.Black, start, end);
}
}
g.DrawLine(Pens.Black, new PointF(originLocation, originLocation), new PointF(originLocation, this.Height)); Pen p = new Pen(Color.Gray, 1);
p.DashStyle = DashStyle.Solid;
p.DashPattern = new float[] { 1, (float)(MonitorDPI / 25.4 * 5 * scaling) - 1 }; for (int i = 0; i <= heightInmm; i += 5)
{
g.DrawLine(p, 0, (float)(MonitorDPI / 25.4 * i * scaling), 1000, (float)(MonitorDPI / 25.4 * i * scaling));
}
}
}

}

`

https://blog.csdn.net/zhangsongshan/article/details/4797698?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0.no_search_link&spm=1001.2101.3001.4242

【转】WinForm窗体刻度尺的更多相关文章

  1. winform 窗体圆角设计

    网上看到的很多winform窗体圆角设计代码都比较累赘,这里分享一个少量代码就可以实现的圆角.主要运用了System.Drawing.Drawing2D. 效果图 代码如下. private void ...

  2. WinForm 窗体属性 窗体美化

    WinForm是·Net开发平台中对Windows Form的一种称谓. Windows窗体的一些重要特点如下: 功能强大:Windows窗体可用于设计窗体和可视控件,以创建丰富的基于Windows的 ...

  3. winform窗体置顶

    winform窗体置顶 金刚 winform 置顶 今天做了一个winform小工具.需要设置置顶功能. 网上找了下,发现百度真的很垃圾... 还是必应靠谱些. 找到一个可以链接. https://s ...

  4. winform窗体控件(全)

    回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...

  5. C#将exe运行程序嵌入到自己的winform窗体中

    以下例子是将Word打开,然后将它嵌入到winform窗体中,效果如下图:C将exe运行程序嵌入到自己的winform窗体中 - kingmax_res - iSport注意:该方法只适用于com的e ...

  6. Winform 窗体单例

    有窗体Form1和窗体Form2,单击Form1上按钮,只弹出一个Form2. Form2里自定义一个方法,里面判断是否弹出Form2,没有时弹出Form2. public static Form2 ...

  7. WinForm窗体嵌入

    一.在winform窗体上添加两个控件 1.容器>Panel 2.添加 SideBar.dll (下载链接:http://pan.baidu.com/s/1o6qhf9w) (1)将SideBa ...

  8. C#实现WinForm窗体逐渐显示效果

    C#实现WinForm窗体逐渐显示效果,这个博客园里面已经有其它人已经实现了,原理很简单,就是通过定时改变窗体的透明度(从0到1,即透明度从完全透明到不透明),我这里也是按照这个思路来实现的,但是我做 ...

  9. .NET vs2010中使用IrisSkin2.dll轻松实现winForm窗体换肤功能

    IrisSkin2.dll是一款很不错的免费皮肤控件,利用它可以轻松的实现winForm窗体换肤! 网上很多朋友说在VS2010中不能使用IrisSkin2.dll,我这里提供一个取巧的办法. Iri ...

随机推荐

  1. SpringDataJdbc多数据源

    代码基于 SpringBoot + SpringDataJDBC + Mybatis 架构 介绍使用多数据源的关键的一些类:AbstractRoutingDataSource继承实现determine ...

  2. 有没有可能两个不相等的对象有有相同的 hashcode?

    有可能,两个不相等的对象可能会有相同的 hashcode 值,这就是为什么在 hashmap 中会有冲突.相等 hashcode 值的规定只是说如果两个对象相等,必 须有相同的 hashcode 值, ...

  3. Java 中堆和栈有什么区别?

    JVM 中堆和栈属于不同的内存区域,使用目的也不同.栈常用于保存方法帧和局 部变量,而对象总是在堆上分配.栈通常都比堆小,也不会在多个线程之间共享, 而堆被整个 JVM 的所有线程共享.

  4. SpringCloud个人笔记-02-Feign初体验

    项目结构 sb_cloud_product <?xml version="1.0" encoding="UTF-8"?> <project x ...

  5. OpenCV+QT5在Window下的环境配置记录

    在安装OpenCV时最需要注意的是,OpenCV库,也就是我们需要的dll和动态库需要我们使用CMake来生成. 虽然在官网上下载得到的文件中已经包含了库文件和.h等头文件,但是在具体开发中编译器编译 ...

  6. FPGA入门到精通系列1:数字电路基础知识

      本文主要介绍数字电路基础知识,用最简洁的内容介绍最核心的知识. 1.数字电路是什么? 数字电路是利用电源电压的高电平和低电平分别表示1和0,进而实现信息的表达.模拟信号:随时间连续变化的信号.处理 ...

  7. 【控制】模型预测控制 MPC 【合集】Model Predictive Control

    1.模型预测控制--运动学模型 2.模型预测控制--模型线性化 3.模型预测控制--模型离散化 4.模型预测控制--预测 5.模型预测控制--控制律优化二次型优化 6.模型预测控制--反馈控制 7.模 ...

  8. 《css揭秘》读书笔记

    第一章 引言 css编码技巧 在引言中,作者提到使用em与inherit来实现css代码的简洁与可维护性.但是根据本司机两年的开发经验来看,在实际开发中很少来使用em这个单位.如何用以及何时去使用,还 ...

  9. PAT B1051 复数乘法

    输入样例: 2.3 3.5 5.2 0.4 输出样例: -8.68-8.23i 解题思路: 1.读入R1.P1.R2.P2. 2.A=(R1*R2)cos(P1+P2),B=(R1*R2)sin(P1 ...

  10. Python入门-系统模块time

    1.time模块 时间戳:1970年,1月1日开始时间元祖:包含日期,时间,保存日期结构的元祖对象格式化时间日期:按照指定的标记进行格式化处理 时间戳 import time time_num = t ...