using DirectShowLib;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ImpulseVision
{
class ApplicationParameters
{
#region <Переменные>
private bool autoRun;
private Point points;//положение окна
private Size sizes;//размеры окна
private int countDay;
public bool AutoRun
{
get { return GetStatusAutoRun(); }
set
{
autoRun = value;
if (autoRun)
{
SetAutoRun(Application.ExecutablePath);
}
else
{
RemoveAutoRun();
}
}
}
///
/// положение формы
///
public Point Points
{
get { return GetFormLocation(); }
set
{
points = value;
if (points.X != -1 && points.Y != -1)
{
SetFormLocation(points);
}
else
{
RemoveFormLocation();
}
}
}
public Size Sizes
{
get { return GetSizeForm(); }
set
{
sizes = value;
if (sizes.Width != -1 && sizes.Height != -1)
{
SetSizeWindow(sizes);
}
else
{
RemoveSizeWindow();
}
}
}
public int CountDay
{
get { return GetCountDay(); }
set
{
countDay = value;
if (countDay != -1)
{
SetSaveLogFiles(countDay);
}
else
{
RemoveSaveLogFiles();
}
}
}
#endregion
public ApplicationParameters()
{
}
public struct FormParameters
{
public bool startingWithWindows;
public Point points;
public Size sizes;
public int countDay;
}
///
/// получение параметров формы
///
private FormParameters GetParameters()
{
//получить параметры из реестра и возвратить в качестве структуры
return new FormParameters();
}
public void DeleteAllParameter()
{
RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software", true);
try
{
Reg.DeleteSubKeyTree("AppDemo");
MessageBox.Show("Ключ реестра удалён!");
}
catch
{
MessageBox.Show("Указанного ключа реестра не существует!");
}
Reg.Close();
}
#region --Загрузка вместе с ОС--
///
/// задание параметров автозагрузки
///
private void SetAutoRun(string executablePath)
{
RegistryKey Reg = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\");
try
{
executablePath = executablePath.Replace("/", "\\");
Reg.SetValue("ImpulseVisionApp", executablePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Reg.Close();
StreamWriter Sw = new StreamWriter(Application.StartupPath + "\\autorun.txt");
Sw.Close();
}
///
/// получение статуса автозагрузки
///
/// включена или выключена (true/false)
private bool GetStatusAutoRun()
{
bool Status = false;
RegistryKey Reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\",true);
if (Reg != null)
{
Status = Reg.GetValue("ImpulseVisionApp") != null;
}
Reg.Close();
return Status;
}
///
/// удаление автозагрузки
///
private void RemoveAutoRun()
{
RegistryKey Reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\", true);
if (Reg != null)
{
if (Reg.GetValue("ImpulseVisionApp") != null)
{
Reg.DeleteValue("ImpulseVisionApp");
}
}
Reg.Close();
if (File.Exists(Application.StartupPath + "\\autorun.txt"))
{
File.Delete(Application.StartupPath + "\\autorun.txt");
}
}
#endregion
#region --Положение формы--
///
/// установка параметров положения формы
///
private void SetFormLocation(Point points)
{
int PosX = points.X;
int PosY = points.Y;
RegistryKey Reg = Registry.CurrentUser.CreateSubKey("Software\\ImpulseVisionApp", true);
Reg.SetValue("LocX", points.X);
Reg.SetValue("LocY", points.Y);
Reg.Close();
}
private Point GetFormLocation()
{
int PosX = -1;
int PosY = -1;
RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
if(Reg != null)
{
if (Reg.GetValue("LocX") != null)
{
PosX = (int)Reg.GetValue("LocX");
}
if (Reg.GetValue("LocY") != null)
{
PosY = (int)Reg.GetValue("LocY");
}
}
Reg.Close();
return new Point(PosX, PosY);
}
///
/// удаление сохранения положения формы
///
private void RemoveFormLocation()
{
RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
if(Reg != null)
{
if (Reg.GetValue("LocX") != null)
{
Reg.DeleteValue("LocX");
}
if (Reg.GetValue("LocY") != null)
{
Reg.DeleteValue("LocY");
}
}
Reg.Close();
}
#endregion
#region --Размеры окна--
///
/// установка настроек для сохранения размеров окна
///
private void SetSizeWindow(Size sizes)
{
RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
if (Reg == null)
Reg = Registry.CurrentUser.CreateSubKey("Software\\ImpulseVisionApp");
Reg.SetValue("width", sizes.Width);
Reg.SetValue("height", sizes.Height);
Reg.Close();
}
///
/// получение ранее сохранённых размеров формы
///
///
private Size GetSizeForm()
{
int Wigth = -1, Height = -1;
RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp");
if(Reg != null)
{
if (Reg.GetValue("width") != null)
{
Wigth = (int)Reg.GetValue("width");
}
if (Reg.GetValue("height") != null)
{
Height = (int)Reg.GetValue("height");
}
}
Reg.Close();
return new Size(Wigth, Height);
}
///
/// удалить настройки для сохранения размеров окна
///
private void RemoveSizeWindow()
{
RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
if (Reg != null)
{
if(Reg.GetValue("width") != null)
Reg.DeleteValue("width");
if(Reg.GetValue("height") != null)
Reg.DeleteValue("height");
}
Reg.Close();
}
#endregion
#region --Хранение файлов журнала--
///
/// установка количества дней хранения файлов журнала
///
private void SetSaveLogFiles(int countDay)
{
RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
if (Reg == null)
Reg = Registry.CurrentUser.CreateSubKey("Software\\ImpulseVisionApp");
Reg.SetValue("countDay", countDay);
Reg.Close();
}
private int GetCountDay()
{
int CountDay = 2;
RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp");
if (Reg != null)
{
if (Reg.GetValue("countDay") != null)
{
CountDay = (int)Reg.GetValue("countDay");
}
Reg.Close();
}
return CountDay;
}
///
/// удаление параметра для сохранения файлов журнала
///
private void RemoveSaveLogFiles()
{
RegistryKey Reg = Registry.CurrentUser.OpenSubKey("Software\\ImpulseVisionApp", true);
if(Reg != null)
{
if (Reg.GetValue("countDay") != null)
{
Reg.DeleteValue("countDay");
}
}
}
#endregion
}
}