Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Problem on custom draw of shapenode when using the print preview dialog (Read 4633 times)
Dav
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 4
Joined: Aug 30th, 2013
Problem on custom draw of shapenode when using the print preview dialog
Sep 3rd, 2013 at 11:31am
Print Post  
Hello,

I have used the event DrawNode on a MindFusion.Diagramming.Diagram.

the code is similar to this:

Code
Select All
protected void flowChart_DrawNode(object sender, DrawNodeEventArgs e)
{

			var node = e.Node.Tag as DiagramNode;
			if (node != null)
				node.DrawNode(e);
} 



and the Node (which has a MindFusion.Diagramming.ShapeNode as a private field) has this DrawNode method

Code
Select All
public override void DrawNode(DrawNodeEventArgs e)
{
    System.IntPtr gHdc = e.Graphics.GetHdc();

    // Get the node bounds
    var bounds = diagramPanel.DiagramView.DocToClient(new RectangleF(ShapeNode.Bounds.X, ShapeNode.Bounds.Y, ShapeNode.Bounds.Width, ShapeNode.Bounds.Height));
    bounds = new Rectangle(bounds.X - 3, bounds.Y - 3, bounds.Width + 7, bounds.Height + 7);

    System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(((MindFusion.Drawing.SolidBrush)ShapeNode.Brush).Color);
    System.Drawing.Pen pen = new System.Drawing.Pen(((MindFusion.Drawing.Pen)ShapeNode.Pen).Color, ((MindFusion.Drawing.Pen)ShapeNode.Pen).Width);
    pen.DashStyle = ((MindFusion.Drawing.Pen)ShapeNode.Pen).DashStyle;


    try
    {
        using (var g = Graphics.FromHdc(gHdc))
        {
            // Draw the background color
            g.FillRectangle(brush, bounds);
            g.DrawRectangle(pen, rectangleBounds);

            // Render the RTF contents...
            rtfControl.Render(g, bounds);
        }

    }
    finally
    {
        e.Graphics.ReleaseHdc(gHdc);
    }
} 



And the rtfControl Render method

Code
Select All
public void Render(Graphics g, Rectangle rc)
{
    // convert rect from pixels to twips
    rc.X = (int)(rc.X * 1440 / g.DpiX);
    rc.Y = (int)(rc.Y * 1440 / g.DpiY);
    rc.Width = rc.X + (int)((rc.Width) * 1440 / g.DpiX);
    rc.Height = rc.Y + (int)((rc.Height) * 1440 / g.DpiY);

    // get dc
    IntPtr hdc = g.GetHdc();

    // set up FORMATRANGE struct
    FORMATRANGE fmt = new FORMATRANGE();
    fmt.hdc = fmt.hdcTarget = hdc;
    fmt.rc = fmt.rcPage = rc;
    fmt.cpMin = 0;
    fmt.cpMax = -1;

    // render RTF
    int render = 1;
    SendMessageFormatRange(Handle, EM_FORMATRANGE, render, ref fmt);

    // clean up
    SendMessage(Handle, EM_FORMATRANGE, render, 0);

    // done with dc
    g.ReleaseHdc(hdc);
}

// FORMATRANGE is used by RichEd20.dll to render RTF
/// <summary>
///
/// </summary>
internal struct FORMATRANGE
{
    internal IntPtr hdc, hdcTarget;
    internal Rectangle rc, rcPage;
    internal int cpMin, cpMax;
} 



The problem is that this code works fine when drawing the mindfusion diagram within a WinForms application control, but when using the diagramView.PrintPreviewEx(printDocument); method, the node is not well drawn on the final page of the print preview dialog, seems to be more little and in the left sqare position, and I know that one reason is the print resolution for the graphics object which is 600 dpi when using the e.Graphics object for printing while when drawing the node on the windows application uses 96 dpi, but I don't know why is not working properly when getting the bounds of the shape.

Attached is the ShapeNode in the right place using the diagram within the windows forms cotrol, and whe previewing
Any help would be appreciated
Thanks in advance
  

within_the_control.png (Attachment deleted)
preview_dialog.png (Attachment deleted)
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Problem on custom draw of shapenode when using the print preview dialog
Reply #1 - Sep 3rd, 2013 at 5:15pm
Print Post  
Hi,

Instead of diagramPanel.DiagramView.DocToClient, try calling the Utilities.DocToDevice method. The former gives you the number of DiagramView pixels that correspond to the specified logical coordinates, and you don't want to use number of DiagramView pixels as target size when rendering on the printer DC. The latter gives you the number of device pixels that correspond to the currently applied transforms for the specified Graphics instance, and it should convert to the printer's resolution correctly.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Dav
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 4
Joined: Aug 30th, 2013
Re: Problem on custom draw of shapenode when using the print preview dialog
Reply #2 - Sep 5th, 2013 at 10:52am
Print Post  
Hello,

thanks for the quick response.

It works, but I have now another problem.

If I change the diagramView Zoom, the rendered node is also affected by this factor.

If I change the diagramView.ZoomFactor to 100 before I call the diagramView.PrintPreviewEx(printDocument); it works as expected.

But I have to show this dialog without viewing the zoom change in the WinForms application, I can later reset it to the old value, it works as expected, but the diagramview is refreshed during the showdialog of the print preview and the you can see the zoom factor effect. I don't want this behavior. Can I do anything to show the print dialog with zoomfactor 100 without seeing it on the main Diagram?

Thanks in advance!
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Problem on custom draw of shapenode when using the print preview dialog
Reply #3 - Sep 5th, 2013 at 7:13pm
Print Post  
Hi,

The standard printing code does not scale its output by view's ZoomFactor. If the output looks scaled for your custom nodes, it probably happens because the bounds value calculated using DocToClient or DocToDevice gets scaled by ZoomFactor / 100 by these methods. To inverse that scale, multiply the rectangle's coordinates back by 100 / ZoomFactor.

Alternatively, you could set PrintOptions.ScaleFactor *= 100 / ZoomFactor, however this will affect all items on the page. If the page contains standard nodes and links, they would look scaled down, and only the custom nodes will look the correct size.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Dav
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 4
Joined: Aug 30th, 2013
Re: Problem on custom draw of shapenode when using the print preview dialog
Reply #4 - Sep 9th, 2013 at 8:30am
Print Post  
Hello,

Thank you for your response!

Finally, I stored these values before calling the diagramView.PrintPreviewEx(printDocument) dialog:

oldZoomFactorForPrint = diagramView.ZoomFactor;
oldScrollX = diagramView.ScrollX;
oldScrollY = diagramView.ScrollY;


Previously I declared the printDocument BeginPrint event, and use the following ocde for setting the right scale factor for my custom draw code:

diagramView.ZoomFactor = diagramView.PrintOptions.Scale;

I also declared in the printDocument EndPrint event, and used this code to restore the old values:

diagramView.ZoomFactor = oldZoomFactorForPrint;
diagramView.ScrollX = oldScrollX;
diagramView.ScrollY = oldScrollY;

With this code, you cannot appreciate any zoom changes in the diagram view, so it looks nice in the Application.

Thank you very much.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint