2

我正在尝试在自定义控件中绘制右对齐文本,但是,由于某种原因,它似乎与我的目标水平位置不对齐,并且字符串之间存在差异。

我可以接受它与我的目标水平位置不完全匹配的事实,但字符串之间的差异在视觉上很糟糕!

任何指针?

在此处输入图像描述

隔离代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RightAlignTest {
    class RightControlTest : UserControl {
        public RightControlTest() {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }

        public static void DrawString(Graphics g, string s, Font f, RectangleF r, Color c) {
            float locx = r.Left;
            float locy = r.Top;
            SizeF txts = g.MeasureString(s, f);
            locx = (locx + r.Width - txts.Width);
            g.DrawString(s, f, new SolidBrush(c), locx, locy);
        }

        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
            int rightTarget = Width - 20;
            Font f = new Font("Arial Unicode MS", 13f, FontStyle.Regular);
            int i = 0;
            string[] strings = { "Current Limit 1:", "Current Limit 2:", "Temperature Center 1:", "Temperature Center 2:" };
            foreach (var s in strings) {
                Rectangle r1 = new Rectangle(0, 30 * i++, rightTarget, Height);
                DrawString(e.Graphics, s, f, r1, Color.Black);
            }
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Blue)), rightTarget, 0, rightTarget, Height);
        }
    }
    public partial class Form1 : Form {
        public Form1() {
            RightControlTest t = new RightControlTest();
            t.Dock = DockStyle.Fill;
            Controls.Add(t);
        }
    }
}
4

2 回答 2

3

试试这是否有效:

public static void DrawString(
    Graphics g, string s, Font f, 
    RectangleF r, Color c) 
{
    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Far;
    stringFormat.LineAlignment = StringAlignment.Center; // Not necessary here
    g.DrawString(s, f, new SolidBrush(c), r, stringFormat);
}

使用StringFormat取自http://msdn.microsoft.com/en-us/library/332kzs7c.aspx的示例

于 2012-07-12T11:48:20.677 回答
0

使用StringFormat.SetTabStop(int,float[]),像这样:

private void PrintPage_Print(object sender, PrintPageEventArgs e)
    {
        using (Font f = new Font("Segoe UI", 10f, FontStyle.Regular, GraphicsUnit.Point))
        {
            using (Brush b = new SolidBrush(Color.Black))
            {
                using (Graphics g = e.Graphics)
                {
                    float y = 5;
                    string headLine = "Article\tUnit\tNet\tGross";
                    Pen pen = new Pen(b);
                    SizeF measure = g.MeasureString(headLine, f);                        
                    StringFormat format = new StringFormat();

                    float[] tabs = new float[] { 200, 100, 55, 55};
                    Rectangle rect = new Rectangle(5, (int)y, (int)(tabs.Sum()+5), (int)measure.Height);
                    format.SetTabStops(0, tabs);

                    g.DrawString(headLine, f, b, rect, format);
                    g.DrawRectangle(pen, rect);
                    y += rect.Height + 3f;
                    format.LineAlignment = StringAlignment.Far;
                    foreach (var product in Bill.ListPositions())
                    {
                        measure = g.MeasureString(product.PositionString, f);
                        rect = new Rectangle(5, (int)y, (int)(tabs.Sum()+5), (int)measure.Height);
                        g.DrawString(product.PositionString, f, b, rect, format);
                        y += measure.Height + 2f;
                    }
                    g.DrawLine(pen, new Point(0, (int)y), new Point((int)(tabs.Sum()), (int)y));

                    tabs = new float[] { 300, 110 };
                    format.LineAlignment = StringAlignment.Near;
                    format.SetTabStops(0, tabs);
                    foreach (var line in DrawTotalSummaryLines())
                    {
                        measure = g.MeasureString(line, f);
                        rect = new Rectangle(5, (int)y, (int)(tabs.Sum()+5), (int)measure.Height);                            
                        g.DrawString(line, f, b, rect, format);
                        y += measure.Height + 2f;
                        if (line.Contains("Gross:") ||line.Contains("CHANGE:"))
                        {
                            g.DrawLine(pen, new Point(0, (int)y), new Point((int)(tabs.Sum()), (int)y));
                            y += measure.Height + 2f;
                        }
                    }
                    g.Dispose();
                    pen.Dispose();
                }
                b.Dispose();
            }
            f.Dispose();
        }

结果应如下所示:

结果图片

于 2018-05-27T19:52:34.433 回答