A
AliR \(VC++ MVP\)
Hi everyone,
I need to draw some rft to the screen. And I found some code out there that
uses a Richedit control and sends it an EM_FORMATRANGE to do exactly that.
Now I need to add scaling to this code. What I'm trying to do is to have
the richedit control draw to a metafile and then I can set the ScaleTrasform
of the Graphics object and draw the metafile.
But everytime I call Metafile.GetHenhmetafile() it throws a "Parameter is
not valid" execption.
I haven't been able to find a solution to this problem anywhere.
public void Draw(Graphics graphics, RectangleF layoutArea, float
xFactor)
{
//Calculate the area to render.
SafeNativeMethods.RECT rectLayoutArea;
rectLayoutArea.Top = (int)(layoutArea.Top * anInch);
rectLayoutArea.Bottom = (int)(layoutArea.Bottom * anInch);
rectLayoutArea.Left = (int)(layoutArea.Left * anInch);
rectLayoutArea.Right = (int)(layoutArea.Right * anInch);
IntPtr hdc = graphics.GetHdc();
Metafile metafile = new Metafile(hdc, layoutArea);
//Release the device context handle obtained by a previous call
graphics.ReleaseHdc(hdc);
IntPtr hDCEMF = metafile.GetHenhmetafile(); <-------- I can't
get passed this line!
SafeNativeMethods.FORMATRANGE fmtRange;
fmtRange.chrg.cpMax = -1; //Indicate character from
to character to
fmtRange.chrg.cpMin = 0;
fmtRange.hdc = hDCEMF; //Use the same DC for
measuring and rendering
fmtRange.hdcTarget = hDCEMF; //Point at printer hDC
fmtRange.rc = rectLayoutArea; //Indicate the area on page
to print
fmtRange.rcPage = rectLayoutArea; //Indicate size of page
IntPtr wParam = IntPtr.Zero;
wParam = new IntPtr(1);
//Get the pointer to the FORMATRANGE structure in memory
IntPtr lParam = IntPtr.Zero;
lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lParam, false);
SafeNativeMethods.SendMessage(this.Handle,
SafeNativeMethods.EM_FORMATRANGE, wParam, lParam);
//Free the block of memory allocated
Marshal.FreeCoTaskMem(lParam);
graphics.ScaleTransform(xFactor, xFactor);
graphics.DrawImage(metafile, layoutArea.Location);
}
AliR.
I need to draw some rft to the screen. And I found some code out there that
uses a Richedit control and sends it an EM_FORMATRANGE to do exactly that.
Now I need to add scaling to this code. What I'm trying to do is to have
the richedit control draw to a metafile and then I can set the ScaleTrasform
of the Graphics object and draw the metafile.
But everytime I call Metafile.GetHenhmetafile() it throws a "Parameter is
not valid" execption.
I haven't been able to find a solution to this problem anywhere.
public void Draw(Graphics graphics, RectangleF layoutArea, float
xFactor)
{
//Calculate the area to render.
SafeNativeMethods.RECT rectLayoutArea;
rectLayoutArea.Top = (int)(layoutArea.Top * anInch);
rectLayoutArea.Bottom = (int)(layoutArea.Bottom * anInch);
rectLayoutArea.Left = (int)(layoutArea.Left * anInch);
rectLayoutArea.Right = (int)(layoutArea.Right * anInch);
IntPtr hdc = graphics.GetHdc();
Metafile metafile = new Metafile(hdc, layoutArea);
//Release the device context handle obtained by a previous call
graphics.ReleaseHdc(hdc);
IntPtr hDCEMF = metafile.GetHenhmetafile(); <-------- I can't
get passed this line!
SafeNativeMethods.FORMATRANGE fmtRange;
fmtRange.chrg.cpMax = -1; //Indicate character from
to character to
fmtRange.chrg.cpMin = 0;
fmtRange.hdc = hDCEMF; //Use the same DC for
measuring and rendering
fmtRange.hdcTarget = hDCEMF; //Point at printer hDC
fmtRange.rc = rectLayoutArea; //Indicate the area on page
to print
fmtRange.rcPage = rectLayoutArea; //Indicate size of page
IntPtr wParam = IntPtr.Zero;
wParam = new IntPtr(1);
//Get the pointer to the FORMATRANGE structure in memory
IntPtr lParam = IntPtr.Zero;
lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lParam, false);
SafeNativeMethods.SendMessage(this.Handle,
SafeNativeMethods.EM_FORMATRANGE, wParam, lParam);
//Free the block of memory allocated
Marshal.FreeCoTaskMem(lParam);
graphics.ScaleTransform(xFactor, xFactor);
graphics.DrawImage(metafile, layoutArea.Location);
}
AliR.