Files
VAR.Toolbox/VAR.Toolbox/UI/FrmToolbox.cs
2017-12-24 12:15:50 +01:00

190 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using VAR.Toolbox.Code;
namespace VAR.Toolbox.UI
{
public partial class FrmToolbox : Form
{
#region Declarations
private bool _closing = false;
#endregion Declarations
#region Form life cycle
public FrmToolbox()
{
InitializeComponent();
MouseDown += DragWindow_MouseDown;
lblToolbox.MouseDown += DragWindow_MouseDown;
}
private void FrmToolbox_Load(object sender, EventArgs e)
{
Icon ico = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
Icon = ico;
niTray.Icon = ico;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown || _closing)
{
base.OnFormClosing(e);
return;
}
HideChildWindows();
Hide();
e.Cancel = true;
}
#endregion Form life cycle
#region UI events
private void DragWindow_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
User32.ReleaseCapture();
User32.SendMessage(Handle, User32.WM_NCLBUTTONDOWN, User32.HT_CAPTION, 0);
}
}
private void btnExit_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Are you sure want to exit?", "Exit?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
_closing = true;
CloseChildWindows();
Close();
}
}
private void niTray_MouseClick(object sender, MouseEventArgs e)
{
Show();
ShowChildWindows();
WindowState = FormWindowState.Normal;
}
private void btnBase64_Click(object sender, EventArgs e)
{
CreateWindow(typeof(FrmBase64));
}
private void btnProxyCmd_Click(object sender, EventArgs e)
{
CreateWindow(typeof(FrmProxyCmd));
}
private void btnWebcam_Click(object sender, EventArgs e)
{
CreateWindow(typeof(FrmWebcam));
}
private void btnTunnelTCP_Click(object sender, EventArgs e)
{
CreateWindow(typeof(FrmTunnelTCP));
}
private void btnTestSoapService_Click(object sender, EventArgs e)
{
CreateWindow(typeof(FrmTestSoapService));
}
private void btnTestRestService_Click(object sender, EventArgs e)
{
CreateWindow(typeof(FrmTestRestService));
}
private void btnScreenshooter_Click(object sender, EventArgs e)
{
CreateWindow(typeof(FrmScreenshooter));
}
#endregion UI events
#region Window handling
private Form CreateWindow(Type type)
{
Form frm = Activator.CreateInstance(type) as Form;
if(frm== null)
{
return null;
}
_forms.Add(frm);
frm.FormClosing += frmChild_FormClosing;
if ((frm is IFormWithIcon) == false)
{
frm.Icon = Icon;
}
frm.Show();
return frm;
}
private List<Form> _forms = new List<Form>();
private void frmChild_FormClosing(object sender, FormClosingEventArgs e)
{
_forms.Remove((Form)sender);
}
private void FrmToolbox_FormClosing(object sender, FormClosingEventArgs e)
{
CloseChildWindows();
}
private bool _wasMinimized = false;
private void FrmToolbox_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
{
_wasMinimized = true;
HideChildWindows();
}
if (FormWindowState.Normal == WindowState && _wasMinimized)
{
_wasMinimized = false;
ShowChildWindows();
}
}
private void CloseChildWindows()
{
while (_forms.Count > 0)
{
_forms[0].Close();
}
}
private void ShowChildWindows()
{
foreach (Form frm in _forms)
{
frm.Show();
}
}
private void HideChildWindows()
{
foreach (Form frm in _forms)
{
frm.Hide();
}
}
#endregion Window handling
}
}