Home All Groups Group Topic Archive Search About

PrintPreviewControl

Author
31 Oct 2006 11:12 PM
Richard MSL
I am using a PrintPreviewControl to show report output. I would like there to
be a way for the user to select a portion of the output, then right click and
choose an option which would trigger an event in my program. Is there a way
to do this?

I gather that PrintPreviewControl is closed and not able to be extended, is
this so? Is there a better way of showing a report prior to printing? Thanks.

Author
1 Nov 2006 7:51 AM
Jeffrey Tan[MSFT]
Hi Richard,

Based on my understanding, your winform application is using
PrintPreviewControl. Now, you want to allow the user to select portion of
the preview image and pop up a context menu for other customized functions.
If I have misunderstood you, please feel free to tell me, thanks.

Yes, there is no build-in support for this feature. PrintPreviewControl
internally uses a MetaFile to capture the real printing output, then it
will paint the MetaFile on the control interface based on the Zoom
property.

However, PrintPreviewControl is not sealed, which means we can inherit from
this class and provide some customized function ourselves. For example, to
implement the selection function, the normal algorithm is recording the
selection start point in OnMouseDown method of PrintPreviewControl, then in
OnMouseUp method, we can record the end point. In the OnPaint method, we
can use these 2 points to draw the selection rectangle. This logic is not
hard to implement.

I have written a little sample project to demonstrate the algorithm:

internal MyPrintPreviewControl MyPrintPreviewControl1;
private System.Drawing.Printing.PrintDocument docToPrint =
    new System.Drawing.Printing.PrintDocument();

private void button1_Click(object sender, System.EventArgs e)
{
    // Construct the MyPrintPreviewControl.
    this.MyPrintPreviewControl1 = new MyPrintPreviewControl();

    // Set location, name, and dock style for MyPrintPreviewControl1.
    this.MyPrintPreviewControl1.Location = new Point(88, 80);
    this.MyPrintPreviewControl1.Name = "MyPrintPreviewControl1";
    this.MyPrintPreviewControl1.Dock = DockStyle.Fill;

    // Set the Document property to the PrintDocument
    // for which the PrintPage event has been handled.
    this.MyPrintPreviewControl1.Document = docToPrint;

    // Set the zoom to 25 percent.
    this.MyPrintPreviewControl1.Zoom = 1;

    // Set the UseAntiAlias property to true so fonts are smoothed
    // by the operating system.
    this.MyPrintPreviewControl1.UseAntiAlias = true;

    // Add the control to the form.
    this.Controls.Add(this.MyPrintPreviewControl1);

    // Associate the event-handling method with the
    // document's PrintPage event.
    this.docToPrint.PrintPage +=
        new System.Drawing.Printing.PrintPageEventHandler(
        docToPrint_PrintPage);
}

// The MyPrintPreviewControl will display the document
// by handling the documents PrintPage event
private void docToPrint_PrintPage(
    object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

    // Insert code to render the page here.
    // This code will be called when the control is drawn.

    // The following code will render a simple
    // message on the document in the control.
    string text = "In docToPrint_PrintPage method.";
    System.Drawing.Font printFont =
        new Font("Arial", 35, FontStyle.Regular);

    e.Graphics.DrawString(text, printFont,
        Brushes.Black, 10, 10);
}

public class MyPrintPreviewControl: PrintPreviewControl
{
    Point pt_start;
    Point pt_end;
    bool drawSelection=false;
    protected override void OnMouseDown(MouseEventArgs e)
    {
        if(Control.MouseButtons==MouseButtons.Left)
        {
            drawSelection=true;
            pt_start=new Point(e.X, e.Y);
        }
        else
        {
            drawSelection=false;
        }

        base.OnMouseDown (e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        pt_end=new Point(e.X, e.Y);
        base.OnMouseUp (e);
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {       
        base.OnPaint (e);
        if(drawSelection)
        {
            e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)),
                new Rectangle(pt_start, new Size(pt_end.X-pt_start.X,
pt_end.Y-pt_start.Y)));
        }
    }
}

You may customize my sample code snippet and fit in your application.

To implement the right click context menu function, you may add the
ContextMenu component in the winform project and associate it with the
PrintPreviewControl(set PrintPreviewControl.ContextMenu property).

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
1 Nov 2006 2:49 PM
Richard MSL
That's great, thanks, I will try it.

""Jeffrey Tan[MSFT]"" wrote:

Show quote
> Hi Richard,
>
> Based on my understanding, your winform application is using
> PrintPreviewControl. Now, you want to allow the user to select portion of
> the preview image and pop up a context menu for other customized functions.
> If I have misunderstood you, please feel free to tell me, thanks.
>
> Yes, there is no build-in support for this feature. PrintPreviewControl
> internally uses a MetaFile to capture the real printing output, then it
> will paint the MetaFile on the control interface based on the Zoom
> property.
>
> However, PrintPreviewControl is not sealed, which means we can inherit from
> this class and provide some customized function ourselves. For example, to
> implement the selection function, the normal algorithm is recording the
> selection start point in OnMouseDown method of PrintPreviewControl, then in
> OnMouseUp method, we can record the end point. In the OnPaint method, we
> can use these 2 points to draw the selection rectangle. This logic is not
> hard to implement.
>
> I have written a little sample project to demonstrate the algorithm:
>
> internal MyPrintPreviewControl MyPrintPreviewControl1;
> private System.Drawing.Printing.PrintDocument docToPrint =
>     new System.Drawing.Printing.PrintDocument();
>
> private void button1_Click(object sender, System.EventArgs e)
> {
>     // Construct the MyPrintPreviewControl.
>     this.MyPrintPreviewControl1 = new MyPrintPreviewControl();
>
>     // Set location, name, and dock style for MyPrintPreviewControl1.
>     this.MyPrintPreviewControl1.Location = new Point(88, 80);
>     this.MyPrintPreviewControl1.Name = "MyPrintPreviewControl1";
>     this.MyPrintPreviewControl1.Dock = DockStyle.Fill;
>
>     // Set the Document property to the PrintDocument
>     // for which the PrintPage event has been handled.
>     this.MyPrintPreviewControl1.Document = docToPrint;
>
>     // Set the zoom to 25 percent.
>     this.MyPrintPreviewControl1.Zoom = 1;
>
>     // Set the UseAntiAlias property to true so fonts are smoothed
>     // by the operating system.
>     this.MyPrintPreviewControl1.UseAntiAlias = true;
>
>     // Add the control to the form.
>     this.Controls.Add(this.MyPrintPreviewControl1);
>
>     // Associate the event-handling method with the
>     // document's PrintPage event.
>     this.docToPrint.PrintPage +=
>         new System.Drawing.Printing.PrintPageEventHandler(
>         docToPrint_PrintPage);
> }
>
> // The MyPrintPreviewControl will display the document
> // by handling the documents PrintPage event
> private void docToPrint_PrintPage(
>     object sender, System.Drawing.Printing.PrintPageEventArgs e)
> {
>
>     // Insert code to render the page here.
>     // This code will be called when the control is drawn.
>
>     // The following code will render a simple
>     // message on the document in the control.
>     string text = "In docToPrint_PrintPage method.";
>     System.Drawing.Font printFont =
>         new Font("Arial", 35, FontStyle.Regular);
>
>     e.Graphics.DrawString(text, printFont,
>         Brushes.Black, 10, 10);
> }
>
> public class MyPrintPreviewControl: PrintPreviewControl
> {
>     Point pt_start;
>     Point pt_end;
>     bool drawSelection=false;
>     protected override void OnMouseDown(MouseEventArgs e)
>     {
>         if(Control.MouseButtons==MouseButtons.Left)
>         {
>             drawSelection=true;
>             pt_start=new Point(e.X, e.Y);
>         }
>         else
>         {
>             drawSelection=false;
>         }
>        
>         base.OnMouseDown (e);
>     }
>
>     protected override void OnMouseUp(MouseEventArgs e)
>     {
>         pt_end=new Point(e.X, e.Y);
>         base.OnMouseUp (e);
>         this.Invalidate();
>     }
>
>     protected override void OnPaint(PaintEventArgs e)
>     {       
>         base.OnPaint (e);
>         if(drawSelection)
>         {
>             e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)),
>                 new Rectangle(pt_start, new Size(pt_end.X-pt_start.X,
> pt_end.Y-pt_start.Y)));
>         }
>     }
> }
>
> You may customize my sample code snippet and fit in your application.
>
> To implement the right click context menu function, you may add the
> ContextMenu component in the winform project and associate it with the
> PrintPreviewControl(set PrintPreviewControl.ContextMenu property).
>
> Hope this helps.
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Community Support
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/default.aspx.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
Author
2 Nov 2006 2:11 AM
Jeffrey Tan[MSFT]
Hi Richard,

Glad to see my reply can help you. I will still monitor this issue for 2
days for your further test result. Please feel free to feedback any concern
or further question, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

AddThis Social Bookmark Button