문자열을 비트 맵에 풀로 정렬해야합니다. 나는 StringFormat.Alignment가 완전 정렬 정렬을 지원하지 않는다는 것을 알고있다. 그래서, 나는 풀 - 정당화 비트 맵에 문자열을 그리는 솔루션을 찾고 있어요. RictTextBox 전체 정당하지만 텍스트를 정당화 WinAPI를 사용하는 것 같아요. RichTextBox로 텍스트를 그릴 수는 있지만 폼에 표시하지 않고 컨트롤 비트 맵 (스크린 샷)을 얻는 방법을 모르겠습니다. System.Drawing.Graphics에 대한 트릭이나 타사 라이브러리가 있습니까?.NET에서 전체 정렬 된 문자열 그리기
0
A
답변
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 **입니까? –
관련 문제
- 1. .NET에서 역 정렬 된 사전
- 2. Postgresql - 정렬 된 집합의 전체 텍스트 정렬
- 3. OpenGL에서 정렬 순서 그리기
- 4. .NET에서 이미지 그리기 - 지우는 방법?
- 5. 캔버스에 문자열 그리기
- 6. 문자열^C++ .Net에서
- 7. 정렬 된 집합에 대한 "전체"계산
- 8. C# 문자열 정렬 VS Oracle 문자열 정렬
- 9. .NET에서 엄청난 양의 2D 데이터 그리기
- 10. 그라디언트 색상으로 문자열 그리기
- 11. 타원에 문자열 그리기
- 12. OpenGL에서 문자열 그리기
- 13. JPanel에 문자열 그리기
- 14. 캔버스에 문자열 그리기
- 15. 정렬 문자열
- 16. 문자열 정렬
- 17. Access 및 .NET에서 숫자 정렬
- 18. MySql 테이블 값을 기준으로 정렬 된 문자열
- 19. 네이티브 모양 및 느낌으로 .NET에서 텍스트 그리기
- 20. .NET에서 동적 문자열 수식 계산?
- 21. Rails에서 숫자와 텍스트로 문자열 정렬
- 22. 임의의 순서로 문자열 정렬
- 23. matlab에 정렬 된 데이터 세트를 사용하여 파라 메트릭 곡선 그리기
- 24. NEWBIE : 정렬 된 정점 배열을 사용하여 OpenGL ES 모양 그리기
- 25. 전체 SQLite 테이블 정렬
- 26. 정렬 전체 페이지
- 27. 파일에서 문자열 정렬
- 28. 정렬 문자열 배열
- 29. 같은 클래스에서 여러 문자열 그리기
- 30. LWJGL에서 Slick의 Unicodefont로 문자열 그리기
Winforms 또는 WPF? – kenny
Winforms ....... – oruchreis