【解決方法】C# を使用してフローチャートを作成するには (プログラム的に)

プログラミングQA


こんにちは皆さん
C# Windows アプリケーションを持っています。C# Windows アプリケーションを使用して (プログラム的に) フローチャートを描画する必要がありますか? 単語の図形を試してみましたが、図形ごとに位置を指定する必要があるため、難しいと思います。
C# Windows アプリケーションでフローチャートを描画するための良いアイデアはありますか?

前もって感謝します

解決策 1

単語の形はおそらく考えられる中で最悪のアイデアでしょう。 さまざまなアプローチが多すぎる可能性があるため、質問は曖昧すぎます。 そして私たちはあなたの背景を知りません。 ほとんど空白の場所から質問を始めるのはあまり良い考えではありません。

私の過去の回答の一部を共有させていただきます。 役に立つアイデアが得られるかもしれません:
さまざまなコントロールを接続する[^]、
C# または VB.NET の CorelDraw や Inkscape などのベクター グラフィック ソフトウェア[^]。

-SA

解決策 2

ここでコードを共有します。これは Form1.cs ファイルに追加できます。
詳細は次のとおりです。

It provides a basic interactive flowchart making utility. The program's main user interface contains a picture box and buttons that allow the user to interactively add, connect, and remove nodes on a flowchart. When the "Add Start" button is checked, and the picture box is clicked, a FlowchartElement is added to the elements list at the clicked location. Before adding, it may open a dialog box to input the text of the new element if any existing element doesn't have text. If Control is held while left-clicking, the program tries to make a connection between the most recently added element and the one under the cursor.

Form1.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace FlowchartMaker
{
    public partial class Form1 : Form
    {
        private List<FlowchartElement> elements = new List<FlowchartElement>();
        public Form1()
        {
            InitializeComponent();
            pictureBox1.BackColor = Color.White;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
            btnRemoveLast.Click += btnRemoveLast_Click;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            foreach (var element in elements)
            {
                DrawFlowchartElement(e.Graphics, element);

                foreach (var connectedElement in element.Connections)
                {
                    Point start = new Point(element.Position.X + 50, element.Position.Y + 50);
                    Point end = new Point(connectedElement.Position.X + 50, connectedElement.Position.Y);
                    e.Graphics.DrawLine(Pens.Black, start, end);
                }
            }    
        }

        private void DrawFlowchartElement(Graphics graphics, FlowchartElement element)
        {
            Rectangle rect = new Rectangle(element.Position, new Size(70, 35));
            graphics.DrawRectangle(Pens.Black, rect);
            graphics.FillRectangle(Brushes.LightGray, rect);

            StringFormat sf = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };
            graphics.DrawString(element.NodeText, Font, Brushes.Black, rect, sf); 
        }

        private bool ShowInputDialogIfNeeded(out string inputText)
        {
            inputText = string.Empty;
            bool shouldShowInputDialog = elements.Any(element => string.IsNullOrWhiteSpace(element.NodeText));

            if (shouldShowInputDialog)
            {
                using (var inputDialog = new InputDialog())
                {
                    inputDialogShown = true;
                    if (inputDialog.ShowDialog() == DialogResult.OK)
                    {
                        inputText = inputDialog.InputText.Trim();
                    }
                    inputDialogShown = false; 
                }
            }

            return shouldShowInputDialog;
        }

        private FlowchartElement lastClickedElement = null;
        private bool inputDialogShown = false;



        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (btnAddStart.Checked)
            {

                if (!inputDialogShown)
                {
                    if (ShowInputDialogIfNeeded(out string inputText))
                    {
                        elements.Add(new FlowchartElement
                        {
                            NodeText = inputText.Trim(),
                            Position = e.Location
                        });

                        pictureBox1.Invalidate();
                    }
                }
            }

            if (e.Button == MouseButtons.Left && ModifierKeys == Keys.Control && lastClickedElement != null)
            {
                FlowchartElement clickedElement = elements.FirstOrDefault(element =>
                    
                e.X >= element.Position.X && e.X <= element.Position.X + 70 &&
                    e.Y >= element.Position.Y && e.Y <= element.Position.Y + 35);

                if (clickedElement != null && clickedElement != lastClickedElement)
                {
                    lastClickedElement.Connections.Add(clickedElement);
                    pictureBox1.Invalidate();
                }
            }
            else
            {
                lastClickedElement = elements.LastOrDefault();
            }
        }
        private void btnRemoveLast_Click(object sender, EventArgs e)
        {
            if (elements.Count > 0)
            {
                elements.RemoveAt(elements.Count - 1);
                pictureBox1.Invalidate();
            }
            else {
                MessageBox.Show("No Node is added!");
            }
        }

        private void btnAddStart_CheckedChanged(object sender, EventArgs e)
        {
            if (btnAddStart.Checked)
            {
                btnAddStart.BackColor = Color.LightGreen; 
            }
            else
            {
                btnAddStart.BackColor = SystemColors.Control;
            }
        }
    }
}

コメント

タイトルとURLをコピーしました