Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / Windows Forms

BizDraw framework for .NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (21 votes)
30 May 20075 min read 128.1K   3.2K   102  
A small framework to design and print documents containing shapes, text, images, bar codes...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace BizDraw.Printing
{
    [System.Runtime.InteropServices.ComVisible(true)]
    public partial class CellsSelector : UserControl
    {
        private DocumentPrintEngine _printEngine;
        private double  zoomFactorX = 1;
        private double  zoomFactorY = 1;
        private int currentPage = 0;
        private int cellHeight;
        private int cellWidth;
        private int rowsSep;
        private int colsSep;

        public CellsSelector()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
            this.pCells.Paint += new PaintEventHandler(pCells_Paint);
            this.pCells.MouseClick += new MouseEventHandler(pCells_MouseClick);

        }

        void pCells_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
                return;
            
            Cell selectedCell = GetCellFromPoint(e.X, e.Y);
            if (selectedCell == null)
                return;

            selectedCell.Selected = !selectedCell.Selected;
            Redraw();
        }
        private Cell GetCellFromPoint(int x, int y)
        {
            if (this.PrintEngine.Count == 0)
                return null ;

            for (int i = currentPage * this.PrintEngine.DocumentsPerPage; i < (currentPage + 1) * this.PrintEngine.DocumentsPerPage; i++)
            {
                if (i >= this.PrintEngine.Count)
                    return null ;
                
                Cell c = this.PrintEngine.Cells[i];
                Rectangle rect = RectFromCell(c);
                if (rect.Contains(new Point(x, y)))
                    return c;
            }
            return null;

        }
        void pCells_Paint(object sender, PaintEventArgs e)
        {
            if (this.PrintEngine == null || this.PrintEngine.Format == null  && this.PrintEngine.Columns == 0 || this.PrintEngine.Rows == 0)
                return;

            double mmToPixel = 96 / 25.4;
            double hInchToPixel = 0.96;

            //Zoom factors calculations
            double Xnum = (double)this.pCells.Width;
            double Xdenum = 96.0d * (double)this.PrintEngine.Format.PageSettings.PaperSize.Width / 100.0d;
            zoomFactorX = Xnum / Xdenum;

            double Ynum = (double)this.pCells.Height;
            double Ydenum = 96.0d * (double)this.PrintEngine.Format.PageSettings.PaperSize.Height / 100.0d;
            zoomFactorY = Ynum / Ydenum;
            
            //Painting cells
            cellHeight = Convert.ToInt32((this.PrintEngine.Format.PageSettings.PaperSize.Height - this.PrintEngine.Format.PageSettings.Margins.Top - this.PrintEngine.Format.PageSettings.Margins.Bottom) * zoomFactorY * hInchToPixel / this.PrintEngine.Rows);
            cellWidth = Convert.ToInt32((this.PrintEngine.Format.PageSettings.PaperSize.Width - this.PrintEngine.Format.PageSettings.Margins.Left - this.PrintEngine.Format.PageSettings.Margins.Right ) * zoomFactorX * hInchToPixel / this.PrintEngine.Columns);
            rowsSep = Convert.ToInt32(this.PrintEngine.Format.RowsSepHeight  * zoomFactorY * mmToPixel  );
            colsSep = Convert.ToInt32(this.PrintEngine.Format.ColumnsSepWidth  * zoomFactorX * mmToPixel );

            //painting margins
            Rectangle marginRect = this.PrintEngine.Format.PrintableArea;
            marginRect.X = Convert.ToInt32(marginRect.X * mmToPixel * zoomFactorX);
            marginRect.Y = Convert.ToInt32(marginRect.Y * zoomFactorY * mmToPixel);
            marginRect.Height = cellHeight * this.PrintEngine.Rows ;
            marginRect.Width = cellWidth * this.PrintEngine.Columns ;

            e.Graphics.DrawRectangle(new Pen(Brushes.Gray), marginRect);

            if (this.PrintEngine.Count == 0)
                return;
            for (int i = currentPage * this.PrintEngine.DocumentsPerPage; i < (currentPage + 1) * this.PrintEngine.DocumentsPerPage; i++)
            {
                if (i >= this.PrintEngine.Count)
                    return;
                DrawCell(e, this.PrintEngine.Cells[i]);
            }
        }
        internal void Redraw()
        {
            pCells_Paint(this.pCells, new PaintEventArgs(this.pCells.CreateGraphics(), this.pCells.ClientRectangle));
            pCells.Refresh();
        }
        private void DrawCell(PaintEventArgs e, Cell cell)
        {
            Rectangle cellRectangle = RectFromCell(cell);
            e.Graphics.DrawRectangle(new Pen(Brushes.Gray), cellRectangle);
            if (cell.Selected)
            {
               e.Graphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle( cellRectangle.X +1, cellRectangle.Y +1, cellRectangle.Width -1 ,cellRectangle.Height -1));
            }
        }
        private Rectangle RectFromCell(Cell cell)
        {
            Rectangle result = new Rectangle();
            result.X = cell.Column * (cellWidth + Convert.ToInt32( (double)colsSep  * 96.0 / 25.4 )) + Convert.ToInt32(this.PrintEngine.Format.PrintableArea.X * 96/25.4 * zoomFactorX);
            result.Y = cell.Row   * (cellHeight + Convert.ToInt32((double) rowsSep * 96.0/25.4) ) + Convert.ToInt32(this.PrintEngine.Format.PrintableArea.Y * 96/ 25.4 * zoomFactorY);
            result.Width = cellWidth;
            result.Height = cellHeight;
            return result;
        }
        public DocumentPrintEngine PrintEngine
        {
            get
            {
                return _printEngine;
            }
            set
            {
                if (_printEngine != value)
                {
                    _printEngine = value;
                    if (_printEngine !=null)
                        _printEngine.ParametersChanged += new EventHandler(_printEngine_ParametersChanged);
                }
            }
        }

        void _printEngine_ParametersChanged(object sender, EventArgs e)
        {
            currentPage = 0;
            SetPagesStatus();

            this.Redraw();
        }

        private void btNext_Click(object sender, EventArgs e)
        {
            this.currentPage++;
            SetPagesStatus();

        }
        private void SetPagesStatus()
        {
            int current = currentPage +1;
            if (this.PrintEngine.PagesCount == 0)
                current = 0;
            this.lCurrentPage.Text = string.Format("Page {0}/{1}  ", current.ToString() , this.PrintEngine.PagesCount.ToString());
            if (current >= this.PrintEngine.PagesCount)
                this.btNext.Enabled = false;
            else
                this.btNext.Enabled = true;

            if (this.currentPage > 0)
                this.btPrevious.Enabled = true;
            else
                this.btPrevious.Enabled = false;

            Redraw();
        }
        private void btPrevious_Click(object sender, EventArgs e)
        {
            this.currentPage--;
            SetPagesStatus();
        }
        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
MCSD Asp.Net certified developer

Comments and Discussions