Click here to Skip to main content
Click here to Skip to main content

DrawTools 2014

, 15 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Arnault Bonafos
Software Developer (Senior)
France France
Java/C++ Engineer, I worked for 7.5 years in the Silicon Valley at Tumbleweed Comm. Corp.
Back in France since, I miss those moments that were delights in the bay area.

Comments and Discussions

 
SuggestionExport to CSharp Code Itself / XML PinprofessionalVanlalruata_Hnamte17-Nov-14 3:38 
QuestionExpandable Line or connected line between Draw Objects PinmemberKRISHNARAYALU16-Aug-14 3:17 
Questionhello PinmemberMember 1086125313-Jun-14 2:47 
Questionsome function such as tooltips,draw image Pinmemberaolongxue20-May-14 21:17 
AnswerRe: some function such as tooltips,draw image PinmemberArnault Bonafos21-May-14 0:30 
GeneralRe: some function such as tooltips,draw image Pinmemberaolongxue23-May-14 23:03 
Questionmy 5 PinmemberSouthmountain15-Jan-14 12:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 15 Jan 2014
Article Copyright 2014 by Arnault Bonafos
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid