`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. Redis 集群,集群的原理是什么?

    1).Redis Sentinal 着眼于高可用,在 master 宕机时会自动将 slave 提升为master,继续提供服务. 2).Redis Cluster 着眼于扩展性,在单个 redis ...

  2. Spring 支持的事务管理类型?

    Spring 支持两种类型的事务管理:编程式事务管理:这意味你通过编程的方式管理事务,给你带来极大的灵 活性,但是难维护.声明式事务管理:这意味着你可以将业务代码和事务管理分离,你只需用 注解和 XM ...

  3. zookeeper 的应用

    不建议使用(单独)zookeeper 做分布式队列,有几点原因,以下原因摘抄于curator的官网: 1.zookeeper有1MB的传输限制.而在队列中,拥有很多的数据节点,通常包括数千个,如果有较 ...

  4. vmware 磁盘清理---还原虚拟机硬盘大小

    linux host: 1.使用dd命令将客户机未使用的磁盘空间用0填满 cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill ...

  5. SpringCloud个人笔记-04-Stream初体验

    sb_cloud_stream Spring Cloud Stream 是一个构建消息驱动微服务的框架 应用程序通过 inputs 或者 outputs 来与 Spring Cloud Stream ...

  6. springboot项目如何添加热部署

    环境jdk1.8.maven3.6.使用工具为idea 1.在pom.xml文件中添加依赖 <dependency> <groupId>org.springframework. ...

  7. Python - 函数的五大参数

    Python的函数参数挺重要的,总结一下: (1)位置参数:没啥好说的,就是普通的参数. (2)默认参数: 参数形式:def power(x, n = 3): (在函数定义时通过对一个形参赋值的形式, ...

  8. 获取Java数据库中结果集的每个字段名和个数

    /** * 查询到多条数据, 封装到List<Map> */public List<Map<String, Object>> queryForMapList(Str ...

  9. Markdown语法1

    Markdown是一种轻量级标记语言. Markdown 编写的文档可以导出 HTML .Word.图像.PDF.Epub 等多种格式的文档. Markdown 编写的文档后缀为 .md, .mark ...

  10. HTML5相关文章和资源

    Polyfills HTML5 Cross Browser Polyfills canvas HTML5 JS实现毛玻璃效果(高斯模糊) 高斯模糊的算法Canvas 内部元素添加事件处理 应用场景 P ...