I came up with some code to print just the objects that are selected by setting the Printable property. I found out that I had to recurse through GoSubGraphs and set the children GoObject’s Printable property as well.
If I had GoSubGraphs nested inside of each other, I also found that I had to set the containing GoSubGraph.Printable to true or else nothing would print.
Is there a way to print just the nested GoSubGraph and not its parent?
Thanks
public override bool CanPrint()
{
return base.CanPrint() || PrintsChildren;
}
[/code]
protected override void PaintDecoration(Graphics g, GoView view)
{
if (Printable)
base.PaintDecoration(g, view);
}
[/code]
Thanks for the tips. At first I didn’t get it, so I set this problem aside and twisted my brain around some WPF. When I came back to this, it made sense.
private void Print()
{
bool changedPrintable = false;
goView1.PrintScale = goView1.DocScale;
changedPrintable = SetPrintSelected();
goView1.Print();
// or for PrintPreview:
// goView1.PrintPreview();
if (changedPrintable)
ClearPrintSelected();
}
private bool SetPrintSelected()
{
bool changedPrintable = false;
if (goView1.Selection.Count > 0)
{
changedPrintable = true;
foreach (GoObject obj in goView1.Document)
{
obj.Printable = goView1.Selection.Contains(obj);
if (obj is MySubGraph)
{
var sgQuestion = (MySubGraph)obj;
if (SetPrintSelectedChildren(sgQuestion))
sgQuestion.PrintsChildren = true;
}
}
}
return changedPrintable;
}<?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
private bool SetPrintSelectedChildren(MySubGraph)
bool printable = sg.Printable;
foreach (GoObject sgObj in sg)
{
if (goView1.Selection.Contains(sgObj))
sgObj.Printable = true;
else
sgObj.Printable = sg.Printable;
printable = printable || sgObj.Printable;
if (sgObj is MySubGraph)
{
var sgQuestion = (MySubGraph)sgObj;
if (SetPrintSelectedChildren(sgQuestion))
{
printable = true;
sgQuestion.PrintsChildren = true;
}
}
}
return printable;
}
private void ClearPrintSelected()
{
foreach (GoObject obj in goView1.Document)
{
obj.Printable = true;
if (obj is GoSubGraph)
{
GoSubGraph sg = (GoSubGraph)obj;
foreach (GoObject sgObj in sg)
{
sgObj.Printable = true;
if (sgObj is GoSubGraph)
ClearPrintSelectedChildren((GoSubGraph)sgObj);
}
}
}
goView1.Document.ComputeBounds();
}
private void ClearPrintSelectedChildren(GoSubGraphsg)
{
foreach (GoObject sgObj in sg)
{
sgObj.Printable = true;
sg.PrintsChildren = false;
if (sgObj is GoSubGraph)
{
ClearPrintSelectedChildren((GoSubGraph)sgObj);
}
}
}
[/code]
public override SizeF PrintDocumentSize
{
get
{
if (Selection.Count > 0)
return GoDocument.ComputeBounds(Selection, this).Size;
else
return base.PrintDocumentSize;
}
}
public override PointF PrintDocumentTopLeft
{
get
{
if (Selection.Count > 0)
return GoDocument.ComputeBounds(Selection, this).Location;
else
return base.PrintDocumentTopLeft;
}
}
[/code]
private bool _PrintsChildren = false;
public bool PrintsChildren
{
get
{
return _PrintsChildren;
}
set
{
_PrintsChildren = value;
}
}
public override bool CanPrint()
{
return base.CanPrint() || PrintsChildren;
}
protected override void PaintDecoration(Graphics g, GoView view)
{
if (Printable)
base.PaintDecoration(g, view);
}
[/code]