I'm using the code in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_RichTextBox.asp to print richtextboxes.
However, I have a form with multiple richtextboxes on it and I wrote a sub to convert the whole form to GDI. This just inspects each object, draws its tline, fills its color, add any text etc. Simple stuff and it works great... apart from richtextboxes, I can get the outline, backcolor no problem but the text formatting is a mare.
Is there any way I can get the formatted graphics returned from the code shown in the link above? I dont want to direct the result to a printer until later, I want to include richtextboxes in my sub so that I have a routine the draws the full graphical representation of the form.
My attempt so far is shown below, but this is junk since the FormatRange function is sending a message to a printer, not drawing/returning the graphics.
The part of my code which handles RTB's is;
My RTBEx code is as per the link except for the function below where I changed the incoming parameters from printargs to graphics and the target rectangle;
However, I have a form with multiple richtextboxes on it and I wrote a sub to convert the whole form to GDI. This just inspects each object, draws its tline, fills its color, add any text etc. Simple stuff and it works great... apart from richtextboxes, I can get the outline, backcolor no problem but the text formatting is a mare.
Is there any way I can get the formatted graphics returned from the code shown in the link above? I dont want to direct the result to a printer until later, I want to include richtextboxes in my sub so that I have a routine the draws the full graphical representation of the form.
My attempt so far is shown below, but this is junk since the FormatRange function is sending a message to a printer, not drawing/returning the graphics.
The part of my code which handles RTB's is;
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IsRichTextBoxEx [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] RTB [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] RichTextBoxEx[/SIZE]
[SIZE=2]RTB = ctl[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] RTB.Visible [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Text[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] RTBRect [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Rectangle[/SIZE]
[SIZE=2]RTBRect = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Rectangle(YearPanel.PointToClient(ctl.Parent.PointToScreen(ctl.Location)).X, _[/SIZE]
[SIZE=2]YearPanel.PointToClient(ctl.Parent.PointToScreen(ctl.Location)).Y, ctl.Width, ctl.Height)[/SIZE]
[SIZE=2]RTB.FormatRange(g, RTBRect, 0, RTB.TextLength)[/SIZE]
[SIZE=2]RTB.FormatRangeDone()[/SIZE]
[SIZE=2][COLOR=#008000]'BackColor[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ctl.BackColor <> PanelBackColor [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ctlBackBrush [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SolidBrush(SetColor(ctl.BackColor))[/SIZE]
[SIZE=2]g.FillRectangle(ctlBackBrush, CtlRect)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Outline[/COLOR][/SIZE]
[SIZE=2]g.DrawRectangle(Pens.Black, CtlRect)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
My RTBEx code is as per the link except for the function below where I changed the incoming parameters from printargs to graphics and the target rectangle;
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] FormatRange([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics, _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] r [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Rectangle, _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] charFrom [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] charTo [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]' Specify which characters to print[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] STRUCT_CHARRANGE[/SIZE]
[SIZE=2]cr.cpMin = charFrom[/SIZE]
[SIZE=2]cr.cpMax = charTo[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rect [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Rectangle[/SIZE]
[SIZE=2]rect = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Rectangle(r.Location.X, r.Location.Y, r.Width, r.Height)[/SIZE]
[SIZE=2][COLOR=#008000]' Specify the area inside page margins[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] STRUCT_RECT[/SIZE]
[SIZE=2]rc.top = HundredthInchToTwips(rect.Top)[/SIZE]
[SIZE=2]rc.bottom = HundredthInchToTwips(rect.Bottom)[/SIZE]
[SIZE=2]rc.left = HundredthInchToTwips(rect.Left)[/SIZE]
[SIZE=2]rc.right = HundredthInchToTwips(rect.Right)[/SIZE]
[SIZE=2][COLOR=#008000]' Specify the page area[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rcPage [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] STRUCT_RECT[/SIZE]
[SIZE=2]rcPage.top = HundredthInchToTwips(rect.Top)[/SIZE]
[SIZE=2]rcPage.bottom = HundredthInchToTwips(rect.Bottom)[/SIZE]
[SIZE=2]rcPage.left = HundredthInchToTwips(rect.Left)[/SIZE]
[SIZE=2]rcPage.right = HundredthInchToTwips(rect.Right)[/SIZE]
[SIZE=2][COLOR=#008000]' Get device context of output device[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] hdc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IntPtr[/SIZE]
[SIZE=2]hdc = e.GetHdc()[/SIZE]
[SIZE=2][COLOR=#008000]' Fill in the FORMATRANGE structure[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] STRUCT_FORMATRANGE[/SIZE]
[SIZE=2]fr.chrg = cr[/SIZE]
[SIZE=2]fr.hdc = hdc[/SIZE]
[SIZE=2]fr.hdcTarget = hdc[/SIZE]
[SIZE=2]fr.rc = rc[/SIZE]
[SIZE=2]fr.rcPage = rcPage[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] lParam [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IntPtr[/SIZE]
[SIZE=2]lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fr))[/SIZE]
[SIZE=2]Marshal.StructureToPtr(fr, lParam, [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] res [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2]res = SendMessage(Handle, EM_FORMATRANGE, 1, lParam)[/SIZE]
[SIZE=2]Marshal.FreeCoTaskMem(lParam)[/SIZE]
[SIZE=2]e.ReleaseHdc(hdc)[/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] res[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]