2011-10-10 1 views
0

문자열을 비트 맵에 풀로 정렬해야합니다. 나는 StringFormat.Alignment가 완전 정렬 정렬을 지원하지 않는다는 것을 알고있다. 그래서, 나는 풀 - 정당화 비트 맵에 문자열을 그리는 솔루션을 찾고 있어요. RictTextBox 전체 정당하지만 텍스트를 정당화 WinAPI를 사용하는 것 같아요. RichTextBox로 텍스트를 그릴 수는 있지만 폼에 표시하지 않고 컨트롤 비트 맵 (스크린 샷)을 얻는 방법을 모르겠습니다. System.Drawing.Graphics에 대한 트릭이나 타사 라이브러리가 있습니까?.NET에서 전체 정렬 된 문자열 그리기

+0

Winforms 또는 WPF? – kenny

+0

Winforms ....... – oruchreis

답변

2

비트 맵에 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); 
+0

예제에서 ** align ** 및 ** TextAlign **은 무엇입니까? 귀하의 솔루션 주소 ** Justified Alignment **입니까? –