using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;
using MoveGraphLibrary;
namespace GraphicalPrimitives
{
public partial class Form_Triangles : Form
{
int m_version = 620;
const string strAddRegKey = "Form_Triangles";
Mover mover;
Point ptMouse_Down, ptMouse_Up;
Triangle trioPressed;
int iTrioPressed;
List<Triangle> triangles = new List<Triangle> ();
TextM info;
string infotext = "Move any vertex to change the configuration.\n" +
"Resizing can be started at any border point (left button).\n" +
"By any inner point a triangle can be moved (left button) or rotated (right button).\n" +
"The order of triangles and their colors are changed via the context menu.";
bool bRestore = false;
// -------------------------------------------------
public Form_Triangles ()
{
InitializeComponent ();
mover = new Mover (this);
}
// ------------------------------------------------- OnLoad
private void OnLoad (object sender, EventArgs e)
{
RestoreFromRegistry ();
if (!bRestore)
{
triangles .Add (new Triangle (this, mover, null, Color .Lime));
triangles .Add (new Triangle (this, mover, new PointF [] { new PointF (250, 130), new PointF (190, 250), new PointF (400, 400) },
Color .Blue));
triangles .Add (new Triangle (this, mover, new PointF [] { new PointF (120, 60), new PointF (600, 30), new PointF (380, 290) },
Color .Yellow));
triangles .Add (new Triangle (this, mover, new PointF [] { new PointF (50, 300), new PointF (570, 330), new PointF (200, 450) },
Color .Red));
info = new TextM (this, new Point (240, 460), infotext);
info .BackColor = Color .LightYellow;
}
RenewMover ();
}
// ------------------------------------------------- OnFormClosing
private void OnFormClosing (object sender, FormClosingEventArgs e)
{
SaveIntoRegistry ();
}
// ------------------------------------------------- RenewMover
private void RenewMover ()
{
mover .Clear ();
mover .Add (info);
for (int i = 0; i < triangles .Count; i++)
{
mover .Add (triangles [i]);
}
}
// ------------------------------------------------- OnPaint
private void OnPaint (object sender, PaintEventArgs e)
{
Graphics grfx = e .Graphics;
for (int i = triangles .Count - 1; i >= 0; i--)
{
triangles [i] .Draw (grfx);
}
info .Draw (grfx);
}
// ------------------------------------------------- OnMouseDown
private void OnMouseDown (object sender, MouseEventArgs e)
{
ptMouse_Down = e .Location;
if (mover .Catch (e .Location, e .Button))
{
GraphicalObject grobj = mover .CaughtSource;
if (grobj is Triangle)
{
Triangle trio = grobj as Triangle;
if (e .Button == MouseButtons .Left)
{
NodeShape shape = mover .CaughtNodeShape;
if (shape == NodeShape .Circle)
{
trio .StartReconfiguration (mover .CaughtNode);
}
else if (shape == NodeShape .Strip)
{
trio .StartResizing (e .Location, mover .CaughtNode);
}
}
else if (e .Button == MouseButtons .Right)
{
trio .StartRotation (e .Location);
}
}
Invalidate ();
}
ContextMenuStrip = null;
}
// ------------------------------------------------- OnMouseUp
private void OnMouseUp (object sender, MouseEventArgs e)
{
ptMouse_Up = e .Location;
if (mover .Release ())
{
double dist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);
GraphicalObject grobj = mover .WasCaughtSource;
if (grobj is Triangle)
{
if (e .Button == MouseButtons .Left)
{
(grobj as Triangle) .StopResizing ();
}
else if (e .Button == MouseButtons .Right && dist <= 3)
{
trioPressed = grobj as Triangle;
for (iTrioPressed = triangles .Count - 1; iTrioPressed >= 0; iTrioPressed--)
{
if (triangles [iTrioPressed] .ID == grobj .ID)
{
break;
}
}
ContextMenuStrip = menuOnFigures;
}
}
else if (grobj is TextM)
{
if (e .Button == MouseButtons .Right && dist <= 3)
{
ContextMenuStrip = menuOnInfo;
}
}
Invalidate ();
}
}
// ------------------------------------------------- OnMouseMove
private void OnMouseMove (object sender, MouseEventArgs e)
{
if (mover .Move (e .Location))
{
Invalidate ();
}
}
// ------------------------------------------------- OnContextMenuChanged
private void OnContextMenuChanged (object sender, EventArgs e)
{
if (ContextMenuStrip != null)
{
ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
}
}
// ***** menuOnFigures *****
// ------------------------------------------------- OnOpening_menuOnFigures
private void OnOpening_menuOnFigures (object sender, CancelEventArgs e)
{
ToolStripItemCollection items = menuOnFigures .Items;
items ["miPutOnTop"] .Enabled = iTrioPressed > 0;
items ["miOneLevelUp"] .Enabled = iTrioPressed > 0;
items ["miOneLevelDown"] .Enabled = iTrioPressed < triangles .Count - 1;
items ["miPutUnderneath"] .Enabled = iTrioPressed < triangles .Count - 1;
}
// ------------------------------------------------- Click_miPutOnTop
private void Click_miPutOnTop (object sender, EventArgs e)
{
triangles .RemoveAt (iTrioPressed);
triangles .Insert (0, trioPressed);
RenewMover ();
Invalidate ();
}
// ------------------------------------------------- Click_miOneLevelUp
private void Click_miOneLevelUp (object sender, EventArgs e)
{
triangles .Reverse (iTrioPressed - 1, 2);
RenewMover ();
Invalidate ();
}
// ------------------------------------------------- Click_miOneLevelDown
private void Click_miOneLevelDown (object sender, EventArgs e)
{
triangles .Reverse (iTrioPressed, 2);
RenewMover ();
Invalidate ();
}
// ------------------------------------------------- Click_miPutUnderneath
private void Click_miPutUnderneath (object sender, EventArgs e)
{
triangles .RemoveAt (iTrioPressed);
triangles .Add (trioPressed);
RenewMover ();
Invalidate ();
}
// ------------------------------------------------- Click_miColor
private void Click_miColor (object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog ();
dlg .Color = trioPressed .Color;
if (dlg .ShowDialog () == DialogResult .OK)
{
trioPressed .Color = dlg .Color;
Invalidate ();
}
}
// ***** menuOnInfo *****
// ------------------------------------------------- Click_miInfoFont
private void Click_miInfoFont (object sender, EventArgs e)
{
FontDialogWithTitle dlg = new FontDialogWithTitle ();
dlg .Font = info .Font;
dlg .Title = "Information font";
if (dlg .ShowDialog () == DialogResult .OK)
{
info .Font = dlg .Font;
Invalidate ();
}
}
// ------------------------------------------------- Click_miInfoTextColor
private void Click_miInfoTextColor (object sender, EventArgs e)
{
ColorDialogWithTitle dlg = new ColorDialogWithTitle ();
dlg .Color = info .Color;
dlg .Title = "Information color";
if (dlg .ShowDialog () == DialogResult .OK)
{
info .Color = dlg .Color;
Invalidate ();
}
}
// ------------------------------------------------- Click_miInfoBackColor
private void Click_miInfoBackColor (object sender, EventArgs e)
{
ColorDialogWithTitle dlg = new ColorDialogWithTitle ();
dlg .Color = info .BackColor;
dlg .Title = "Information background";
if (dlg .ShowDialog () == DialogResult .OK)
{
info .BackColor = dlg .Color;
Invalidate ();
}
}
const string name = "Main";
// ------------------------------------------------- SaveIntoRegistry
private void SaveIntoRegistry ()
{
string strRegKey = Form_Main .strRegKey + strAddRegKey;
RegistryKey regkey = null;
try
{
regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
if (regkey != null)
{
regkey .SetValue (name, new string [] { m_version .ToString (), // 0
ClientSize .Width .ToString (), // 1
ClientSize .Height .ToString (), // 2
triangles .Count .ToString () }, // 3
RegistryValueKind .MultiString);
info .IntoRegistry (regkey, "Info");
for (int i = 0; i < triangles .Count; i++)
{
triangles [i] .IntoRegistry (regkey, "triangle_" + i .ToString ());
}
}
}
catch
{
}
finally
{
if (regkey != null) regkey .Close ();
}
}
// ------------------------------------------------- RestoreFromRegistry
private void RestoreFromRegistry ()
{
string namekey = Auxi_DLL_Internal .PathDefinedByExecutable ();
namekey += strAddRegKey;
RegistryKey regkey = null;
try
{
regkey = Registry .CurrentUser .OpenSubKey (namekey);
if (regkey != null)
{
string [] strs = (string []) regkey .GetValue (name);
if (strs != null && strs .Length == 4 && Convert .ToInt32 (strs [0]) >= 620)
{
ClientSize = Auxi_Convert .ToSize (strs, 1);
int nFigures = Convert .ToInt32 (strs [3]);
info = TextM .FromRegistry (this, regkey, "Info");
triangles .Clear ();
for (int i = 0; i < nFigures; i++)
{
Triangle trio = Triangle .FromRegistry (this, mover, regkey, "triangle_" + i .ToString ());
if (trio != null)
{
triangles .Add (trio);
}
}
if (info == null || triangles .Count < nFigures)
{
return;
}
bRestore = true;
}
regkey .Close ();
}
}
catch
{
}
finally
{
if (regkey != null) regkey .Close ();
}
}
}
}