Printing Graphics
-
Hello all, I am working on a program that displays descision trees in a panel control. The links between the nodes are drawn straight on to the panel whereas the nodes are custom controls. Trying to print the contents of the panel I have used GetImage, unfortunately this only gives me the links and not the nodes. I suspect having studied further I should have drawn the whole lot to a graphics path... any way I can bodge it without rewriting all the graphics code? I wish to keep the custom controls as they allow me to process mouse events easily. Rob
-
Hello all, I am working on a program that displays descision trees in a panel control. The links between the nodes are drawn straight on to the panel whereas the nodes are custom controls. Trying to print the contents of the panel I have used GetImage, unfortunately this only gives me the links and not the nodes. I suspect having studied further I should have drawn the whole lot to a graphics path... any way I can bodge it without rewriting all the graphics code? I wish to keep the custom controls as they allow me to process mouse events easily. Rob
What you could do is create an Image object (as a member of your form class), give that to the PictureBox's Image property, then do all your drawing operations onto the Image itself instead of the picturebox (you can get a Graphics handle just as easily from the Image, so your code should need only minor modifications) -- Help me! I'm turning into a grapefruit! Phoenix Paint - back from DPaint's ashes!
-
Hello all, I am working on a program that displays descision trees in a panel control. The links between the nodes are drawn straight on to the panel whereas the nodes are custom controls. Trying to print the contents of the panel I have used GetImage, unfortunately this only gives me the links and not the nodes. I suspect having studied further I should have drawn the whole lot to a graphics path... any way I can bodge it without rewriting all the graphics code? I wish to keep the custom controls as they allow me to process mouse events easily. Rob
You can write any window to a DC using Windows API as it's being shown on the screen. You first need to call CreateDC("DISPLAY", null, null, null) and then use SendMessage to send a WM_PRINT to the window. Alternatively, you'll need to rewrite your code and draw everything on the Panel's OnPaint. Yes, even I am blogging now!
-
What you could do is create an Image object (as a member of your form class), give that to the PictureBox's Image property, then do all your drawing operations onto the Image itself instead of the picturebox (you can get a Graphics handle just as easily from the Image, so your code should need only minor modifications) -- Help me! I'm turning into a grapefruit! Phoenix Paint - back from DPaint's ashes!
That would be a complete waste of memory and CPU cycles. The
OnPaint
virtual function and the event if fires -Paint
- exist so that you can draw onto any control. Drawing into an images then assigning that images to aPictureBox
is inefficient and requires much more memory than drawing on the control's surface itself. That can be done with a simplePanel
as the poster is currently doing. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
Hello all, I am working on a program that displays descision trees in a panel control. The links between the nodes are drawn straight on to the panel whereas the nodes are custom controls. Trying to print the contents of the panel I have used GetImage, unfortunately this only gives me the links and not the nodes. I suspect having studied further I should have drawn the whole lot to a graphics path... any way I can bodge it without rewriting all the graphics code? I wish to keep the custom controls as they allow me to process mouse events easily. Rob
Extending on what Daniel said, you should rewrite your
OnPaint
to paint the entire surface (nodes and lines) but in a modular manner that you can pass aGraphics
object (and perhaps the clip bounds) to paint on, but don't paint directly inOnPaint
. This allows you to pass theGraphics
for aPrintDocument
(or even anHDC
for some other device usingGraphics.FromHdc
) and use the same drawing routines. The common solution follows:private void Paint(Graphics g, Rectangle bounds)
{
// Your drawing routines go here...
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Paint(e.Graphics, Bounds);
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Paint(e.Graphics, e.PageBounds);
}Handling your mouse events isn't too hard if you design a nice abstract system of nodes where the
Panel
may translate mouse events to the nodes, but the nodes actually contain the code to move themselves. Polymorphism is a very powerful tool. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]