【问题标题】:Drawing a string with full justified in .NET在 .NET 中绘制完全对齐的字符串
【发布时间】:2011-10-28 22:04:41
【问题描述】:

我需要将一个字符串拉到一个完全对齐的位图上。我知道 StringFormat.Alignment 不支持完全对齐的对齐。所以,我正在寻找一种解决方案来在完全对齐的位图上绘制字符串。 RictTextBox 有充分的理由,但我认为它使用 WinAPI 来证明文本。也许我可以用 RichTextBox 绘制文本,但我不知道如何在不显示在表单中的情况下获取控件位图(屏幕截图)。 System.Drawing.Graphics 是否有任何技巧或替代第三方库?

【问题讨论】:

    标签: .net system.drawing drawstring text-justify


    【解决方案1】:

    我使用了一种方法在位图上绘制RichTextBox

    public class ExtendedRichTextBox : RichTextBox
    {
        private const double inch = 1440 / 96;//Not 14.4!!, believe me you can see someone use 1.44 but it doesn't work on big bitmaps. They round the 1440/96 as 14.4 but it works on only small sized works. use /96
    
        public void DrawToBitmap(Graphics graphics, Rectangle bound)
        {
            Update();  // Ensure RTB fully painted
            IntPtr hDC = graphics.GetHdc();
            FORMATRANGE fmtRange;
    
            RECT rect;
            rect.Left = (int)Math.Ceiling(bound.X * inch);
            rect.Top = (int)Math.Ceiling(bound.Y * inch);
            rect.Right = (int)Math.Ceiling(bound.Right * inch);
            rect.Bottom = (int)Math.Ceiling(bound.Bottom * inch);
            int fromAPI;
    
            fmtRange.hdc = hDC;
            fmtRange.hdcTarget = hDC;
    
            fmtRange.chrg.cpMin = 0;
            fmtRange.chrg.cpMax = -1;
            fmtRange.rc = rect;
            fmtRange.rcPage = rect;
    
            IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
            Marshal.StructureToPtr(fmtRange, lParam, false);
            fromAPI = SendMessage(Handle, EM_FORMATRANGE, 0, lParam);
            fromAPI = SendMessage(Handle, EM_FORMATRANGE, 1, lParam);
            Marshal.FreeCoTaskMem(lParam);
            fromAPI = SendMessage(Handle, EM_FORMATRANGE, 0, new IntPtr(0));
            graphics.ReleaseHdc(hDC);
        }
    }
    

    您可以在 pinvoke 网站上找到 WinApi 实现。但你也可以在这里:

    [DllImport("USER32.dll")]
    private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
    private const int WM_USER = 0x400;
    private const int EM_FORMATRANGE = WM_USER + 57;
    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    private struct CHARRANGE
    {
        public int cpMin;
        public int cpMax;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    private struct FORMATRANGE
    {
        public IntPtr hdc;
        public IntPtr hdcTarget;
        public RECT rc;
        public RECT rcPage;
        public CHARRANGE chrg;
    }
    

    这里是使用的例子。

    var richtext = new ExtendedRichTextBox(); 
    /*I've implemented a RichTextBox but it isn't realted with this question. 
    You can use simply RichTextBox. ExtendedRichTextBox has support rtl.*/
    
    richtext.Font = font;
    richtext.ForeColor = textColor;
    richtext.Text = sometext;
    richtext.SelectAll();
    richtext.RightToLeft = rtl;
    richtext.SelectionAlignment = align;
    
    //Fix the rtl bug in RichTextBox
    if (rtl == RightToLeft.Yes)
    {
        if (align == TextAlign.Center)
            richtext.Rtf = richtext.Rtf.Replace(@"\qr", @"\qc");
        else if (align == TextAlign.Left)
            richtext.Rtf = richtext.Rtf.Replace(@"\qr", @"\ql");
        else if (align == TextAlign.Justify)
            richtext.Rtf = richtext.Rtf.Replace(@"\qr", @"\qj");
    }
    
    //textRect is where we want to put text in.
    var tempBitmap = new Bitmap(textRect.Width, textRect.Height);
    richtext.DrawToBitmap(Graphics.FromImage(tempBitmap), tempRect);
    tempBitmap.MakeTransparent(richtext.BackColor);
    graph.DrawImage(tempBitmap, panelRect.X, panelRect.Y);
    

    【讨论】:

    • 什么是 alignTextAlign 示例。您的解决方案是否解决 Justified Alignment
    猜你喜欢
    • 2013-03-25
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多