Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

DrawTools 2014

0.00/5 (No votes)
15 Jan 2014 1  
ScrollBars improvement

Introduction  

Current alternative is based on the CodeProject DrawTools 2005, built under Visual Studio C# 2010.

The main goal of this tip is to post and save the DrawTools project back to the community after some enhancements. The tip is short, as the improvements are minor.

A few improvements have been made, as follows:

  1. Add scroll bars to the draw area
  2. Handling of AutoScrollPosition in the different tools provided by the original project
  3. Export of the drawing with jpeg format  

Background

Knowledge of C# is required, but this project shows the basics of UI programming under '.NET'.

Architecture

Dynamic handling for showing scroll bars

A dirty boolean is handled in GraphicsList, the canvas document, where it should be. To this Dirty property is associated a DocumentDirtyObserver interface.

    public interface DocumentDirtyObserver
    {
        void IsDirty(GraphicsList gList);
    }

Every time the document is modified, look with the different draw tools, its Dirty property is set to true and any DocumentDirtyObserver is fired.

    public bool Dirty
    {
        get
        {
            return this.dirty;
        }
        set
        {
            this.dirty = value;
            if (this.dirty)
            {
                NotifyDirty();
            }
        }
    }

The class DrawArea implements the DocumentDirtyObserver, and every time the document is 'dirty', it calls AdjustRendering, which in turn computes the bounding box of the canvas document (GraphicsList).

    void AdjustRendering()
    {
        Size docSize;

        if (this.GraphicsList != null)
        {
            docSize = this.GraphicsList.GetSize();
            docSize.Width += 20;
            docSize.Height += 20;
            AutoScrollMinSize = docSize;
        }
        else
        {
	        AutoScrollMinSize = new Size(0, 0);
        }
        Invalidate();
    }

This way, it implements the Mode/View/Controller design pattern, the GraphicsList being the model, DrawArea being the View and the draw Tools being the controller.

In the future, Dirty property should be handled in only one place (it is also handled in the DrawArea class).

Export the graph to JPEG format

The DrawTools 2014 architecture is good enough to be followed to implement a new menu strip option.

  1. Add a menu item to the menu bar

    Simply open the MainForm designer and add the option

  2. Link the MenuItem to a MainForm method to trigger the CommandExportToJpg

        private void CommandExportToJpg()
        {
            docManager.ExportToJpg();
        }
  3. Implement the user interface logic in the DocManager ExportToJpg

    I let you look at the DocManager.ExportToJpg

  4. Subscribe MainForm ExportEvent method implementation to DocManager event.
  5.     docManager.ExportEvent += docManager_ExportEvent;
  6. Implement MainForm logic to actually transform image bitmap into JPEG file.

    All is in the source code, I don't copy it here as it is essentially technical stuff.

Result

The result is best shown by the following graph, obtained with a customized version of the DrawTools.

Points of Interest

Architecture is already well thought in the original project, please read the original document for insight about it.

There was a bug where the status bar was hiding the horizontal scroll bar, but after some thorough inspection of the code, it has been fixed.

History

  1. Added scroll bars to the draw area
  2. Added a status bar
  3. Fixed a bug on the context menu when the scroll bar position is not 0
  4. Export of the drawing with jpeg format
  5. Fixed an OutOfMemoryException with JPEG format export

Special thank you to Alex, who originally posted the DrawTools project on CodeProject.

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