Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

BizDraw framework for .NET

, 30 May 2007
A small framework to design and print documents containing shapes, text, images, bar codes...
bizdraw.zip
Interop
IO
obj
Debug
Refactor
TempPE
Properties.Resources.Designer.cs.dll
Objects
Editors
Printing
Properties
Resources
about.png
add.png
app.ico
arrow_left_green.png
arrow_right_green.png
barcode.png
closewindow16x16.png
copy16.png
cursor.png
cut.png
delete.png
document_zoom_out.png
edit.png
Ellipse.cur
ellipse.png
exit.png
folder.png
folder1.png
image.gif
layer_to_back.png
layer_to_front.png
Line.cur
line.png
new16.png
open16.png
paste.png
Pencil.cur
pencil.png
pointer.bmp
PolyHandle.cur
print16.png
print_setup16.png
prop16.png
Rectangle.cur
rectangle.png
redo.png
save16.png
save_as.png
text.png
text_rich_colored.png
Thumbs.db
undo.png
zoom_in.png
zoom_out.png
Tools
BizDraw.csproj.user
Attributes
BarCodes
bin
Debug
Controls
Core
Data
bizdrawdemo.zip
BizDraw
Attributes
BarCodes
bin
Debug
BizDraw.csproj.user
Controls
Core
Data
Interop
IO
obj
Debug
Refactor
TempPE
Properties.Resources.Designer.cs.dll
Objects
Editors
Printing
Properties
Resources
about.png
add.png
app.ico
arrow_left_green.png
arrow_right_green.png
barcode.png
closewindow16x16.png
copy16.png
cursor.png
cut.png
delete.png
document_zoom_out.png
edit.png
Ellipse.cur
ellipse.png
exit.png
folder.png
folder1.png
image.gif
layer_to_back.png
layer_to_front.png
Line.cur
line.png
new16.png
open16.png
paste.png
Pencil.cur
pencil.png
pointer.bmp
PolyHandle.cur
print16.png
print_setup16.png
prop16.png
Rectangle.cur
rectangle.png
redo.png
save16.png
save_as.png
text.png
text_rich_colored.png
Thumbs.db
undo.png
zoom_in.png
zoom_out.png
Tools
Client
app.ico
bin
Debug
BizDrawClient.vshost.exe
Formats
Card2x2.bzf
BizDrawClient.csproj.user
obj
Debug
TempPE
Properties
Settings.settings
Resources
about.png
closewindow16x16.png
copy16.png
cut.png
delete.png
document_zoom_out.png
edit.png
exit.png
layer_to_back.png
layer_to_front.png
new16.png
open16.png
paste.png
print16.png
print_setup16.png
prop16.png
redo.png
save16.png
save_as.png
text_rich_colored.png
Thumbs.db
undo.png
zoom_in.png
zoom_out.png
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BizDraw.Core;
using BizDraw.Controls;
using BizDraw.IO;
using BizDraw.Printing;
using System.Reflection;
namespace BizDraw.Controls
{
    [System.Runtime.InteropServices.ComVisible(true)]
    public partial class DocumentSpace : UserControl
    {
        public DocumentSpace()
        {
            InitializeComponent(); 
            printEngine = new DocumentPrintEngine();
            RegisterCommands();
            this.tTools.ToolChanged += new EventHandler(tTools_ToolChanged);
        }

        private  Document currentDocument;
        private DocumentPrintEngine printEngine ;
        private DocumentArea editor;
        private Dictionary<string, Command > Commands;
        public   string documentFileName ;
       
        void tTools_ToolChanged(object sender, EventArgs e)
        {
            this.editor.ActiveTool = this.tTools.SelectedTool;
        }
        void RegisterCommands()
        {
            if (DesignMode)
                return;
            Commands = new Dictionary<string, Command >();
            Type thisType = this.GetType();
            //File commands
            Commands.Add("NewDocument", new Command("NewDocument", this,GetMethod("NewDocument"),true,  this.mNewDocument, this.tNew));
            Commands.Add("OpenDocument", new Command("OpenDocument", this, GetMethod("OpenDocument"), true, this.mOpen, this.tOpen));
            Commands.Add("CloseDocument", new Command("CloseDocument", this, GetMethod("CloseDocument"), false,this.mClose , this.tClose ) );
            Commands.Add("SaveDocument", new Command("SaveDocument", this, GetMethod("SaveDocument"), false, this.mSave, this.tSave));
            Commands.Add("SaveDocumentAs", new Command("SaveDocumentAs", this, GetMethod("SaveDocumentAs"), false, this.mSaveAs, this.tSaveAs));
            Commands.Add("Print", new Command("Print", this, GetMethod("Print"), false, this.mPrint, this.tPrint));
            Commands.Add("Properties", new Command("Properties", this, GetMethod("ShowDocumentProperties"), false, this.mProperties , this.tProperties ));

            //Edit Commands
            Commands.Add("Undo", new Command("Undo", this, GetMethod("UndoAction"), false, this.mUndo, this.tUndo));
            Commands.Add("Redo", new Command("Redo", this, GetMethod("RedoAction"), false, this.mRedo, this.tRedo));
            Commands.Add("Copy", new Command("Copy", this, GetMethod("CopySelection"), false, this.mCopy, this.tCopy));
            Commands.Add("Cut", new Command("Cut", this, GetMethod("CutSelection"), false, this.mCut, this.tCut));
            Commands.Add("Paste", new Command("Paste", this, GetMethod("PasteSelection"), false, this.mPaste, this.tPaste));
            Commands.Add("Delete", new Command("Delete", this, GetMethod("DeleteSelection"), false, this.mDelete, this.tDelete));
            Commands.Add("BringToFront", new Command("BringToFront", this, GetMethod("BringSelectionToFront"), false, this.mBringToFront, this.tBringToFront));
            Commands.Add("SendToBack", new Command("SendToBack", this, GetMethod("SendSelectionToBack"), false, this.mSendToBack, this.tSendToBack));

        }
        private MethodInfo GetMethod(string name)
        {
            return this.GetType().GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance); 

        }
        #region Properties

        #endregion
        #region File Commands
        /// <summary>
        /// Creates a new document
        /// </summary>
        private void NewDocument()
        {
            if (!CloseDocument())
                return;
            currentDocument   = new Document();
            FrmDocumentPropertiesEditor frm = new FrmDocumentPropertiesEditor();
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Document = currentDocument;
            if (frm.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            DoOpenDocument();
        }

        void currentDocument_SelectedObjectChanged(object sender, EventArgs e)
        {
            if (this.currentDocument.Items.SelectionCount == 0)
            {
                Commands["Delete"].Disable();
                Commands["Copy"].Disable();
                Commands["Cut"].Disable();
                Commands["BringToFront"].Disable();
                Commands["SendToBack"].Disable();
            }
            else
            {
                Commands["Delete"].Enable ();
                Commands["Copy"].Enable();
                Commands["Cut"].Enable();
                Commands["BringToFront"].Enable();
                Commands["SendToBack"].Enable();
            }
        }
        void DoOpenDocument()
        {
            currentDocument.SelectedObjectChanged += new EventHandler(currentDocument_SelectedObjectChanged);
            editor = new DocumentArea();
            editor.Document = this.currentDocument;
            editor.Size = new Size(100, 100);
            editor.Location = new Point(0, 0);
            editor.Dock = DockStyle.Fill;
            this.toolStripContainer1.ContentPanel.Controls.Add(editor);

            //Enabling items
            this.tTools.Enabled = true;
            Commands["SaveDocument"].Enable();
            Commands["SaveDocumentAs"].Enable();
            Commands["CloseDocument"].Enable();
            Commands["Print"].Enable();
            Commands["Properties"].Enable();
            this.tZoom.Enabled = true;
            this.tZoomIn.Enabled = true;
            this.tZoomOut.Enabled = true;
        }
        /// <summary>
        /// Opens an existent document
        /// </summary>
        private void OpenDocument()
        {
            if (!CloseDocument())
                return;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Fichiers BizDraw (*.bzw)|*.bzw";
            ofd.InitialDirectory = Application.StartupPath + "\\Documents";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.currentDocument =  DocumentManager.Load(ofd.FileName);
                documentFileName = ofd.FileName;
                DoOpenDocument();
            }
        }
        /// <summary>
        /// Close an open document
        /// </summary>
        private bool  CloseDocument()
        {
            bool result = false;
            if (this.currentDocument != null)
            {
                if (this.currentDocument.IsDirty)
                {
                    DialogResult dr = MessageBox.Show("Voulez vous enregistrer les modifications effectu�es avant d'ouvrir un nouveau document", "Nouveau", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes )
                    {
                        if (!SaveDocument())
                            return false;
                        else
                            result = true;
                    }
                    else if (dr == DialogResult.No)
                    {
                        DoCloseDocument();
                        result = true;
                    }
                    else
                        return false;
                }
                else
                {
                    //close the document
                    DoCloseDocument();
                    result = true;
                }
            }
            else
            {
                DoCloseDocument();
                result = true;
            }
            return result;
        }
        private void DoCloseDocument()
        {
            if (this.editor == null)
                return;
            this.editor.Dispose();
            this.toolStripContainer1.ContentPanel.Controls.Remove(editor);
            this.editor = null;

            //disabling items
            this.tTools.Enabled = false ;
            Commands["SaveDocument"].Disable();
            Commands["SaveDocumentAs"].Disable();
            Commands["CloseDocument"].Disable();
            Commands["Print"].Disable();
            Commands["Properties"].Disable();
            this.tZoom.Enabled = false;
            this.tZoomIn.Enabled = false;
            this.tZoomOut.Enabled = false;
        }
        /// <summary>
        /// Saves the document 
        /// </summary>
        /// <returns></returns>
        private bool SaveDocument()
        {
            if (documentFileName == null)
            {
                return SaveDocumentAs();
            }
            else
            {
                DocumentManager.Save(currentDocument, this.documentFileName);
                return true;
            }
        }

        /// <summary>
        /// Saves the document to a new location
        /// </summary>
        /// <returns></returns>
        private bool SaveDocumentAs()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Fichiers BizDraw (*.bzw)|*.bzw";
            sfd.InitialDirectory = Application.StartupPath + "\\Documents";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                DocumentManager.Save(currentDocument, sfd.FileName);
                this.documentFileName = sfd.FileName;
                sfd.Dispose();
                return true;
            }
            else
                return false;

        }


        /// <summary>
        /// Prints the current document
        /// </summary>
        /// <returns></returns>
        private void Print()
        {
            this.currentDocument.Items.UnselectAll();
            printEngine.Document = this.currentDocument;
            printEngine.ShowDialog();
        }
        /// <summary>
        /// Show the document settings page
        /// </summary>
        private void ShowDocumentProperties()
        {
            FrmDocumentPropertiesEditor frm = new FrmDocumentPropertiesEditor();
            frm.Document = this.currentDocument;
            frm.ShowDialog();
        }
        #endregion
        
        #region Edit Commands

        /// <summary>
        /// Undo the last action
        /// </summary>
        private void UndoAction()
        {
        }

        /// <summary>
        /// Redo the last undo action
        /// </summary>
        private void RedoAction()
        {
        }

        /// <summary>
        /// Copies the selected object(s) to the clipboard
        /// </summary>
        private void CopySelection()
        {
            this.currentDocument.CopySelection();
            this.Commands["Paste"].Enable();
        }

        /// <summary>
        /// Cuts the selected object(s)
        /// </summary>
        private void CutSelection()
        {
            this.currentDocument.CutSelection();
            this.Commands["Paste"].Enable();
        }
       
        /// <summary>
        /// Pastes the selected object(s)
        /// </summary>
        private void PasteSelection()
        {
            this.currentDocument.Paste();
            this.Commands["Paste"].Disable();
            this.editor.Refresh();
        }

        /// <summary>
        /// Deletes the selected object(s)
        /// </summary>
        private void DeleteSelection()
        {
            this.currentDocument.DeleteSelection();
            this.editor.Refresh();
        }

        /// <summary>
        /// Brings to front the selected object(s)
        /// </summary>
        private void BringSelectionToFront()
        {
            this.currentDocument.MoveSelectionToFront();
            this.Refresh();
        }

        /// <summary>
        /// Sends to back the selected object(s)
        /// </summary>
        private void SendSelectionToBack()
        {
            this.currentDocument.MoveSelectionToBack();
            this.Refresh();
        }
        #endregion

        private void apProposToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                FrmAbout frm = new FrmAbout();
                frm.ShowDialog();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.documentFileName))
                {
                    this.currentDocument =  DocumentManager.Load(this.documentFileName);
                    DoOpenDocument();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void toolStripContainer1_TopToolStripPanel_Click(object sender, EventArgs e)
        {

        }

        private void tTools_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void tZoomIn_Click(object sender, EventArgs e)
        {
            try
            {
                editor.Zoom *= 2;
                this.tZoom.Text =(editor.Zoom).ToString("0%");
                
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tZoomOut_Click(object sender, EventArgs e)
        {
            try
            {
                editor.Zoom /= 2;
                this.tZoom.Text = (editor.Zoom).ToString("0%");

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tZoom_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {

                if (e.KeyData == Keys.Enter)
                {
                    editor.Zoom = float.Parse((Convert.ToDouble(this.tZoom.Text.Replace("%", "")) /100).ToString());
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tZoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                editor.Zoom = float.Parse((Convert.ToDouble(this.tZoom.Text.Replace("%", "")) / 100).ToString());
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #region Tools Commands
        #endregion
        [System.Runtime.InteropServices.ComRegisterFunction()]
        public static void Register(Type t)
        {
            BizDraw.Interop.ComRegistration.RegisterControl(t);
        }
        [System.Runtime.InteropServices.ComUnregisterFunction()]
        public static void UnRegister(Type t)
        {
            BizDraw.Interop.ComRegistration.UnRegisterControl(t);
        }
    }
}

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

Share

About the Author

Hayder Marzouk
Web Developer
France France
MCSD Asp.Net certified developer

| Advertise | Privacy | Mobile
Web01 | 2.8.141029.1 | Last Updated 30 May 2007
Article Copyright 2007 by Hayder Marzouk
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid