From 52ad12538de60b6db7aac6ab368a71a1f8f2fc5c Mon Sep 17 00:00:00 2001 From: sardq Date: Fri, 18 Oct 2024 20:27:08 +0400 Subject: [PATCH] 2.3 component + refactoring --- WinForms.sln | 4 +- WinForms/FormComponents.Designer.cs | 3 + WinForms/FormComponents.cs | 50 +++++++ WinForms/FormComponents.resx | 3 + WinForms/WinForms.csproj | 2 +- .../Enums/LegendLayoutEnum.cs | 16 +++ .../Helpers/GraphicWordInfo.cs | 16 +++ .../Helpers/SeriesParameters.cs | 12 ++ .../WordDiagram.Designer.cs | 36 +++++ .../NonVisualComponents/WordDiagram.cs | 123 ++++++++++++++++++ .../DropDownList.Designer.cs | 0 .../{ => VisualComponent}/DropDownList.cs | 0 .../{ => VisualComponent}/DropDownList.resx | 0 .../EmailTextBox.Designer.cs | 0 .../{ => VisualComponent}/EmailTextBox.cs | 0 .../{ => VisualComponent}/EmailTextBox.resx | 0 .../ListBoxValues.Designer.cs | 0 .../{ => VisualComponent}/ListBoxValues.cs | 0 .../{ => VisualComponent}/ListBoxValues.resx | 0 ....csproj => WinFormsLibraryRazubaev.csproj} | 4 - 20 files changed, 262 insertions(+), 7 deletions(-) create mode 100644 WinFormsLibrary/NonVisualComponents/Enums/LegendLayoutEnum.cs create mode 100644 WinFormsLibrary/NonVisualComponents/Helpers/GraphicWordInfo.cs create mode 100644 WinFormsLibrary/NonVisualComponents/Helpers/SeriesParameters.cs create mode 100644 WinFormsLibrary/NonVisualComponents/WordDiagram.Designer.cs create mode 100644 WinFormsLibrary/NonVisualComponents/WordDiagram.cs rename WinFormsLibrary/{ => VisualComponent}/DropDownList.Designer.cs (100%) rename WinFormsLibrary/{ => VisualComponent}/DropDownList.cs (100%) rename WinFormsLibrary/{ => VisualComponent}/DropDownList.resx (100%) rename WinFormsLibrary/{ => VisualComponent}/EmailTextBox.Designer.cs (100%) rename WinFormsLibrary/{ => VisualComponent}/EmailTextBox.cs (100%) rename WinFormsLibrary/{ => VisualComponent}/EmailTextBox.resx (100%) rename WinFormsLibrary/{ => VisualComponent}/ListBoxValues.Designer.cs (100%) rename WinFormsLibrary/{ => VisualComponent}/ListBoxValues.cs (100%) rename WinFormsLibrary/{ => VisualComponent}/ListBoxValues.resx (100%) rename WinFormsLibrary/{WinFormsLibrary.csproj => WinFormsLibraryRazubaev.csproj} (94%) diff --git a/WinForms.sln b/WinForms.sln index 4bd2c79..5021067 100644 --- a/WinForms.sln +++ b/WinForms.sln @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33110.190 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsLibrary", "WinFormsLibrary\WinFormsLibrary.csproj", "{FA2C000C-0815-44C4-BBA7-FB37C33DE121}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsLibraryRazubaev", "WinFormsLibrary\WinFormsLibraryRazubaev.csproj", "{FA2C000C-0815-44C4-BBA7-FB37C33DE121}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinForms", "WinForms\WinForms.csproj", "{AA566691-FB51-4E4C-BB7B-9BA77AD20FD1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinForms", "WinForms\WinForms.csproj", "{AA566691-FB51-4E4C-BB7B-9BA77AD20FD1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/WinForms/FormComponents.Designer.cs b/WinForms/FormComponents.Designer.cs index d4a9afc..6ee9b48 100644 --- a/WinForms/FormComponents.Designer.cs +++ b/WinForms/FormComponents.Designer.cs @@ -49,6 +49,7 @@ this.buttonWordText = new System.Windows.Forms.Button(); this.wordTextComponent = new WinFormsLibrary.WordText(this.components); this.wordTableComponent = new WinFormsLibrary.NonVisualComponents.WordTable(this.components); + this.wordDiagramComponent = new WinFormsLibrary.NonVisualComponents.WordDiagram(this.components); this.panel1.SuspendLayout(); this.SuspendLayout(); // @@ -202,6 +203,7 @@ this.buttonWordDiagram.TabIndex = 15; this.buttonWordDiagram.Text = "Word (диаграмма)"; this.buttonWordDiagram.UseVisualStyleBackColor = true; + this.buttonWordDiagram.Click += new System.EventHandler(this.buttonWordDiagram_Click); // // buttonWordTable // @@ -275,5 +277,6 @@ private Button buttonWordText; private WinFormsLibrary.WordText wordTextComponent; private WinFormsLibrary.NonVisualComponents.WordTable wordTableComponent; + private WinFormsLibrary.NonVisualComponents.WordDiagram wordDiagramComponent; } } \ No newline at end of file diff --git a/WinForms/FormComponents.cs b/WinForms/FormComponents.cs index 982959e..fb75513 100644 --- a/WinForms/FormComponents.cs +++ b/WinForms/FormComponents.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using WinFormsLibrary; +using WinFormsLibrary.NonVisualComponents.Enums; using WinFormsLibrary.NonVisualComponents.Helpers; using WinFormsLibrary.Object; @@ -165,5 +166,54 @@ namespace WinForms } } } + + private void buttonWordDiagram_Click(object sender, EventArgs e) + { + using var dialog = new SaveFileDialog + { + Filter = "docx|*.docx" + }; + if (dialog.ShowDialog() == DialogResult.OK) + { + try + { + var series = new List + { + new() { + SeriesName = "Номер 1", + ValuesY = new List { 14, 22, 13 }, + Color = Color.FromArgb(255, 165, 0) + }, + new() { + SeriesName = "Номер 2", + ValuesY = new List { 87, 44, 51 }, + Color = Color.FromArgb(145, 145, 145) + }, + new() { + SeriesName = "Номер 3", + ValuesY = new List { 29, 15, 7 }, + Color = Color.FromArgb(255, 255, 0) + } + }; + + wordDiagramComponent.CreateDiagramDocument(new GraphicWordInfo() + { + Path = dialog.FileName, + DocumentTitle = "Диаграмма в ворде", + DiagramTitle = "Ниже показана диаграмма об исследовании...", + LegendLayout = LegendLayoutEnum.Bottom, + SeriesX = new List { "Пример", "Пример", "Пример" }, + SeriesParameters = series + }); + + MessageBox.Show("Готово!"); + } + catch (Exception ex) + { + MessageBox.Show("Произошла ошибка: " + ex.Message, "А", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + } } } \ No newline at end of file diff --git a/WinForms/FormComponents.resx b/WinForms/FormComponents.resx index 674ec51..9f8bfaa 100644 --- a/WinForms/FormComponents.resx +++ b/WinForms/FormComponents.resx @@ -63,4 +63,7 @@ 156, 17 + + 392, 17 + \ No newline at end of file diff --git a/WinForms/WinForms.csproj b/WinForms/WinForms.csproj index 0654708..5862943 100644 --- a/WinForms/WinForms.csproj +++ b/WinForms/WinForms.csproj @@ -9,7 +9,7 @@ - + \ No newline at end of file diff --git a/WinFormsLibrary/NonVisualComponents/Enums/LegendLayoutEnum.cs b/WinFormsLibrary/NonVisualComponents/Enums/LegendLayoutEnum.cs new file mode 100644 index 0000000..c932c48 --- /dev/null +++ b/WinFormsLibrary/NonVisualComponents/Enums/LegendLayoutEnum.cs @@ -0,0 +1,16 @@ +namespace WinFormsLibrary.NonVisualComponents.Enums +{ + public enum LegendLayoutEnum + { + None, + + Left, + + Top, + + Right, + + Bottom + + } +} diff --git a/WinFormsLibrary/NonVisualComponents/Helpers/GraphicWordInfo.cs b/WinFormsLibrary/NonVisualComponents/Helpers/GraphicWordInfo.cs new file mode 100644 index 0000000..06df505 --- /dev/null +++ b/WinFormsLibrary/NonVisualComponents/Helpers/GraphicWordInfo.cs @@ -0,0 +1,16 @@ +using WinFormsLibrary.NonVisualComponents.Enums; + +namespace WinFormsLibrary.NonVisualComponents.Helpers +{ + public class GraphicWordInfo + { + public string Path { get; set; } = string.Empty; + + public string DocumentTitle { get; set; } = string.Empty; + public string DiagramTitle { get; set; } = string.Empty; + public LegendLayoutEnum LegendLayout { get; set; } + public List SeriesX { get; set; } + public List SeriesParameters { get; set; } = new(); + + } +} \ No newline at end of file diff --git a/WinFormsLibrary/NonVisualComponents/Helpers/SeriesParameters.cs b/WinFormsLibrary/NonVisualComponents/Helpers/SeriesParameters.cs new file mode 100644 index 0000000..39a7804 --- /dev/null +++ b/WinFormsLibrary/NonVisualComponents/Helpers/SeriesParameters.cs @@ -0,0 +1,12 @@ +namespace WinFormsLibrary.NonVisualComponents.Helpers +{ + public class SeriesParameters + { + public string SeriesName { get; set; } = string.Empty; + + public List ValuesY { get; set; } = new(); + + public Color Color { get; set; } + + } +} diff --git a/WinFormsLibrary/NonVisualComponents/WordDiagram.Designer.cs b/WinFormsLibrary/NonVisualComponents/WordDiagram.Designer.cs new file mode 100644 index 0000000..6135662 --- /dev/null +++ b/WinFormsLibrary/NonVisualComponents/WordDiagram.Designer.cs @@ -0,0 +1,36 @@ +namespace WinFormsLibrary.NonVisualComponents +{ + partial class WordDiagram + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + #endregion + } +} diff --git a/WinFormsLibrary/NonVisualComponents/WordDiagram.cs b/WinFormsLibrary/NonVisualComponents/WordDiagram.cs new file mode 100644 index 0000000..d263e46 --- /dev/null +++ b/WinFormsLibrary/NonVisualComponents/WordDiagram.cs @@ -0,0 +1,123 @@ +using System.ComponentModel; +using Word = Microsoft.Office.Interop.Word; +using Excel = Microsoft.Office.Interop.Excel; +using WinFormsLibrary.NonVisualComponents.Helpers; +using WinFormsLibrary.NonVisualComponents.Enums; + +namespace WinFormsLibrary.NonVisualComponents +{ + public partial class WordDiagram : Component + { + public WordDiagram() + { + InitializeComponent(); + } + + public WordDiagram(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + + public void CreateDiagramDocument(GraphicWordInfo diagramInfo) + { + ValidateDiagramInfo(diagramInfo); + + var wordApp = new Word.Application(); + var document = wordApp.Documents.Add(); + + var paragraph = document.Paragraphs.Add(); + paragraph.Range.Text = diagramInfo.DocumentTitle; + paragraph.Range.Font.Size = 24; + paragraph.Range.Font.Bold = 1; + paragraph.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; + paragraph.Range.InsertParagraphAfter(); + + AddChart(document, diagramInfo); + + document.SaveAs2(diagramInfo.Path); + wordApp.Quit(); + } + private void AddChart(Word.Document document, GraphicWordInfo diagramInfo) + { + var paragraph = document.Paragraphs.Add(); + var chartShape = document.InlineShapes.AddChart2(-1, (Microsoft.Office.Core.XlChartType)Excel.XlChartType.xlLine, paragraph.Range); + var chart = chartShape.Chart; + + chart.HasTitle = true; + chart.ChartTitle.Text = diagramInfo.DiagramTitle; + + var chartSeriesCollection = chart.SeriesCollection(); + for (int i = chartSeriesCollection.Count; i >= 1; i--) + { + chartSeriesCollection.Item(i).Delete(); + } + + var seriesX = diagramInfo.SeriesX.ToArray(); + + for (int i = 0; i < diagramInfo.SeriesParameters.Count; i++) + { + var seriesInfo = diagramInfo.SeriesParameters[i]; + var series = chartSeriesCollection.NewSeries(); + + series.Name = seriesInfo.SeriesName; + series.Values = seriesInfo.ValuesY.ToArray(); + series.XValues = seriesX; + + SetSeriesColor(series, seriesInfo.Color); + + series.MarkerStyle = Excel.XlMarkerStyle.xlMarkerStyleCircle; + series.MarkerSize = 5; + } + + chart.HasLegend = true; + chart.Legend.Position = diagramInfo.LegendLayout switch + { + LegendLayoutEnum.Left => Word.XlLegendPosition.xlLegendPositionLeft, + LegendLayoutEnum.Top => Word.XlLegendPosition.xlLegendPositionTop, + LegendLayoutEnum.Right => Word.XlLegendPosition.xlLegendPositionRight, + LegendLayoutEnum.Bottom => Word.XlLegendPosition.xlLegendPositionBottom, + _ => Word.XlLegendPosition.xlLegendPositionBottom, + }; + + chart.ChartData.Workbook.Application.Quit(); + } + + private void SetSeriesColor(dynamic series, Color color) + { + series.Format.Line.ForeColor.RGB = ColorTranslator.ToOle(color); + series.Format.Fill.ForeColor.RGB = ColorTranslator.ToOle(color); + } + + private void ValidateDiagramInfo(GraphicWordInfo diagramInfo) + { + if (string.IsNullOrEmpty(diagramInfo.Path) || string.IsNullOrEmpty(diagramInfo.DocumentTitle) || + string.IsNullOrEmpty(diagramInfo.DiagramTitle) || diagramInfo.SeriesX == null || + diagramInfo.SeriesX.Count == 0 || diagramInfo.SeriesParameters == null || + diagramInfo.SeriesParameters.Count == 0) + { + throw new ArgumentException("Не все данные для диаграммы заполнены"); + } + + foreach (var series in diagramInfo.SeriesParameters) + { + if (string.IsNullOrEmpty(series.SeriesName)) + { + throw new ArgumentException("Название серии не может быть пустым"); + } + + if (series.ValuesY == null || series.ValuesY.Count == 0) + { + throw new ArgumentException($"Список значений оси Y для серии '{series.SeriesName}' не заполнен"); + } + + if (diagramInfo.SeriesX.Count != series.ValuesY.Count) + { + throw new ArgumentException($"Количество данных оси X и значений оси Y для серии '{series.SeriesName}' не совпадает"); + } + } + } + + } +} diff --git a/WinFormsLibrary/DropDownList.Designer.cs b/WinFormsLibrary/VisualComponent/DropDownList.Designer.cs similarity index 100% rename from WinFormsLibrary/DropDownList.Designer.cs rename to WinFormsLibrary/VisualComponent/DropDownList.Designer.cs diff --git a/WinFormsLibrary/DropDownList.cs b/WinFormsLibrary/VisualComponent/DropDownList.cs similarity index 100% rename from WinFormsLibrary/DropDownList.cs rename to WinFormsLibrary/VisualComponent/DropDownList.cs diff --git a/WinFormsLibrary/DropDownList.resx b/WinFormsLibrary/VisualComponent/DropDownList.resx similarity index 100% rename from WinFormsLibrary/DropDownList.resx rename to WinFormsLibrary/VisualComponent/DropDownList.resx diff --git a/WinFormsLibrary/EmailTextBox.Designer.cs b/WinFormsLibrary/VisualComponent/EmailTextBox.Designer.cs similarity index 100% rename from WinFormsLibrary/EmailTextBox.Designer.cs rename to WinFormsLibrary/VisualComponent/EmailTextBox.Designer.cs diff --git a/WinFormsLibrary/EmailTextBox.cs b/WinFormsLibrary/VisualComponent/EmailTextBox.cs similarity index 100% rename from WinFormsLibrary/EmailTextBox.cs rename to WinFormsLibrary/VisualComponent/EmailTextBox.cs diff --git a/WinFormsLibrary/EmailTextBox.resx b/WinFormsLibrary/VisualComponent/EmailTextBox.resx similarity index 100% rename from WinFormsLibrary/EmailTextBox.resx rename to WinFormsLibrary/VisualComponent/EmailTextBox.resx diff --git a/WinFormsLibrary/ListBoxValues.Designer.cs b/WinFormsLibrary/VisualComponent/ListBoxValues.Designer.cs similarity index 100% rename from WinFormsLibrary/ListBoxValues.Designer.cs rename to WinFormsLibrary/VisualComponent/ListBoxValues.Designer.cs diff --git a/WinFormsLibrary/ListBoxValues.cs b/WinFormsLibrary/VisualComponent/ListBoxValues.cs similarity index 100% rename from WinFormsLibrary/ListBoxValues.cs rename to WinFormsLibrary/VisualComponent/ListBoxValues.cs diff --git a/WinFormsLibrary/ListBoxValues.resx b/WinFormsLibrary/VisualComponent/ListBoxValues.resx similarity index 100% rename from WinFormsLibrary/ListBoxValues.resx rename to WinFormsLibrary/VisualComponent/ListBoxValues.resx diff --git a/WinFormsLibrary/WinFormsLibrary.csproj b/WinFormsLibrary/WinFormsLibraryRazubaev.csproj similarity index 94% rename from WinFormsLibrary/WinFormsLibrary.csproj rename to WinFormsLibrary/WinFormsLibraryRazubaev.csproj index 9de78af..9abf70a 100644 --- a/WinFormsLibrary/WinFormsLibrary.csproj +++ b/WinFormsLibrary/WinFormsLibraryRazubaev.csproj @@ -37,8 +37,4 @@ - - - -