如何翻转面板中绘制内容的对齐方式 [英] How to flip alignment of drawn content in a panel
问题描述
我正在使用两个面板沿着画框的侧面(顶部和左侧)绘制标尺。它可以工作,但是现在我的要求是翻转标尺的方向,以使该行从图片框开始,并且文本(数字)在顶部。我该怎么做?
I am using two panels for drawing ruler along the sides (top and left) of a picture box. It works, but now my requirement is to flip the direction of the ruler so that the line starts from the picture box and the text (numbers) are the top. How can I do this?
我的代码如下:
private void panel2_Paint(object sender, PaintEventArgs e)//left Panel
{
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Millimeter;
int step = 1;
int length = panelleft.Height / 3;
int small = 5;
int big = 10;
int number = 10;
int scale = 10;
float stroke = 2.5f;
for (int i = 0; i < length; i += step)
{
float d = 1;
if (i % small == 0)
{
if (i % big == 0)
{
d = 3;
}
else
{
d = 2;
}
}
g.DrawLine(this.pen, 0f, i,d * stroke, i);
if ((i % number) == 0)
{
string text = (i / scale).ToString();
SizeF size = g.MeasureString(text, this.Font, length, this.format);
g.DrawString(text, this.Font, Brushes.Black,d * stroke, i - size.Width-1 / 2 , this.format);
}
}
}
private void panel3_Paint(object sender, PaintEventArgs e)// top panel
{
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Millimeter;
int step = 1;//incremnent
int length = paneltop.Width / 3; //panelinte widthinte pakuthi mathi bcs namml oru point gap vittanu line varakunnath
int small = 5;//cheriya vark ulla length
int big = 10;//valiya vark ulla length
int number = 10;//units 1cm=10 units
float stroke = 2.5f;
for (int i = 0; i < length; i += step)
{
float d = 1;
if (i % small == 0)//cheriya line
{
if (i % big == 0)//valiya line
{
d = 3; //varyude length
}
else
{
d = 2;//varyude length
}
}
g.DrawLine(this.pen, i, 0f, i, d * stroke);//lines varakunnu
if ((i % number) == 0)//0,1,,2
{
string text = (i / number).ToString();//1,2,3 ennu ezhuthan
SizeF size = g.MeasureString(text, this.Font, length, this.format);//ezhuthuna stringnte length ariyan// one digit length 1.618635,2 digit length3.23727
g.DrawString(text, this.Font, Brushes.Black, i - size.Width / 2, d * stroke, this.format);//Y constant ayirikum (d* stroke) ennu koduthath line kazhinju string varan anu alenkil overlapp cheyum
// ( X ) ( Y )
}
}
}
我也想显示一个水平和垂直线,并且当用户将鼠标移到图像上时必须将它们指向标尺。
I also want to show a horizontal and vertical line, and they must be pointed to the ruler when the user moves the mouse over the image.
必需的输出示例:
Required output sample:
推荐答案
由于已将 Graphics
设置为 mm
,因此需要计算控件像素大小的转换系数:
Since you have set the Graphics
to mm
you need to calculate the conversion factor for the controls' pixel sizes:
float dpi = e.Graphics.DpiX;
float factor = 25.4f / dpi;
使用此功能,您可以简单地修改 DrawString
和 DrawLine
这样的调用:
with this you can simply adapt your DrawString
and DrawLine
calls like this:
在 panel2_Paint
中:
float w = panelleft.Width * factor;
g.DrawLine(this.pen, w - d * stroke, i, w, i);
g.DrawString(text, this.Font, Brushes.Black,
w - d * stroke - size.Width, i - size.Width - 1 / 2, this.format);
和 panel3_Paint
中:
float h = paneltop.Height * factor;
g.DrawLine(this.pen, i, h - d * stroke, i, h);
g.DrawString(text, this.Font, Brushes.Black,
i - size.Width / 2, h - d * stroke - size.Height, this.format);
要显示光标行,可以使用以下方法:
To show the Cursor lines you can use this:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Point mp = pictureBox1.PointToClient(Cursor.Position);
if (e.ClipRectangle.Contains(mp))
{
e.Graphics.DrawLine(Pens.Red, 0, mp.Y, e.ClipRectangle.Width, mp.Y);
e.Graphics.DrawLine(Pens.Red, mp.X, 0, mp.X, e.ClipRectangle.Height);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Invalidate();
}
这里是使用以下假设的结果:
Here is the result, using a few assumptions:
panelleft = panel2;
paneltop = panel3;
format = StringFormat.GenericTypographic;
pen = new Pen(Color.Black, 0.25f);
理论上,您应该使用 dpiX
和 dpiY
分别得到两个因素。
In theory you should use dpiX
and dpiY
respectively to get two factors.
这篇关于如何翻转面板中绘制内容的对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!