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:
- Add scroll bars to the draw area
- Handling of
AutoScrollPosition
in the different tools provided by the original project - 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.
Add a menu item to the menu bar
Simply open the MainForm designer and add the option
Link the MenuItem
to a MainForm
method to trigger the CommandExportToJpg
private void CommandExportToJpg()
{
docManager.ExportToJpg();
}
- Implement the user interface logic in the
DocManager ExportToJpg
I let you look at the DocManager.ExportToJpg
- Subscribe
MainForm ExportEvent
method implementation to DocManager
event.
docManager.ExportEvent += docManager_ExportEvent;
- 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
- Added scroll bars to the draw area
- Added a status bar
- Fixed a bug on the context menu when the scroll bar position is not 0
- Export of the drawing with jpeg format
- Fixed an
OutOfMemoryException
with JPEG format export
Special thank you to Alex, who originally posted the DrawTools project on CodeProject.