using KssSmaPlaLib.Commons; using KssSmaPlaLib.Forms; using KssSmaPlaLib.Ini; using KssSmaPlaLib.Input.Gamen; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace KssSmaPlaLib.Forms.ContextMenuFunction.Gamen { public partial class DgvColumnEditorForm : Form { private const string COL_NAME_PROPERTY_NAME = "colPropertyName"; private const string COL_NAME_DEFAULT_DISPLAY_NAME = "colDefaultDisplayName"; private const string COL_NAME_DISPLAY_NAME = "colDisplayName"; /// /// 対象のDGV /// private DataGridView _targetDgv = null; /// /// 対象DGVの列名から列インデックスを取得する辞書 /// (後ほど列インデックスが全てゼロになる事象が起きるため) /// private readonly Dictionary _colNameToRowIndexDic = null; /// /// コンストラクタ /// /// public DgvColumnEditorForm(DataGridView dgv) { InitializeComponent(); _targetDgv = dgv; // 列名から列インデックスを取得する辞書の作成(後ほど列インデックスが全てゼロになる事象が起きるため) _colNameToRowIndexDic = new Dictionary(); foreach (DataGridViewColumn col in _targetDgv.Columns) _colNameToRowIndexDic.Add(col.Name, col.Index); this.DialogResult = DialogResult.Cancel; DgvHelper.Initialize(ColumnDgv); ColumnDgv.Rows.Clear(); // 編集した表示名 var csv = IniHelper.ReadValue(null, _targetDgv.Parent.Name + "." + _targetDgv.Name, KEY_NAME_DISPLAY_NAME_CSV); // 表示順のリストを作る var cols = new DataGridViewColumn[_targetDgv.Columns.Count]; _targetDgv.Columns.CopyTo(cols, 0); Array.Sort(cols, (a, b) => a.DisplayIndex.CompareTo(b.DisplayIndex)); foreach (var col in cols) { var index = ColumnDgv.Rows.Add(); string propertyName = string.Empty; string defaultDisplayName = string.Empty; bool visible = true; var colIndex = col.Index; var displayIndex = col.DisplayIndex; if (col.Tag == null || !(col.Tag is PropertyDescriptor)) { propertyName = col.Name; defaultDisplayName = propertyName; } else { PropertyDescriptor pd = col.Tag as PropertyDescriptor; propertyName = pd.Name; defaultDisplayName = pd.DisplayName; var browsableAttr = pd.Attributes.OfType().FirstOrDefault(); if (browsableAttr != null && !browsableAttr.Browsable) { visible = false; } } //string displayName = col.HeaderText; string displayName = StringUxer.GetStringByIndexFormCsv(csv, colIndex, string.Empty) .Replace("@@@COMMA@@@", ","); ColumnDgv.Rows[index].Tag = col; ColumnDgv.Rows[index].Cells[COL_NAME_PROPERTY_NAME].Value = (!visible ? "【非】" : "") + propertyName; ColumnDgv.Rows[index].Cells[COL_NAME_DEFAULT_DISPLAY_NAME].Value = defaultDisplayName; ColumnDgv.Rows[index].Cells[COL_NAME_DISPLAY_NAME].Value = displayName == defaultDisplayName ? "" : displayName; SetVisible(index, col.Visible); } //// タグに入れておく(PropertyDescriptor型) //col.Tag = pd; //// 列ヘッダーを [DisplayName] に //col.HeaderText = pd.DisplayName; //// [Browsable(false)] の列は非表示(AutoGenerateでも列が立つ場合の保険) //var browsableAttr = pd.Attributes.OfType().FirstOrDefault(); //if (browsableAttr != null && !browsableAttr.Browsable) //{ // col.Visible = false; // continue; //} // DGV列幅復元 DgvHelper.DgvLoadColWidth(ColumnDgv); } /// /// フォームロード /// /// /// private void DgvColumnEditorForm_Load(object sender, EventArgs e) { // バウンズ復元 FormHelper.LoadFormBounds(this); } /// /// 👆ボタンクリック時 /// /// /// private void UpButton_Click(object sender, EventArgs e) { // 対象外状況判断 if (ColumnDgv.SelectedRows.Count < 1) return; if (ColumnDgv.SelectedRows[0].Index == 0) return; // 事前取得 var row = ColumnDgv.SelectedRows[0]; var index = row.Index; // Removeしたら-1になるからその前に取得しておく // 行移動の実行 ColumnDgv.Rows.Remove(row); ColumnDgv.Rows.Insert(index - 1, row); // 行選択 ColumnDgv.ClearSelection(); ColumnDgv.Rows[index - 1].Selected = true; } /// /// 👇ボタンクリック時 /// /// /// private void DownButton_Click(object sender, EventArgs e) { // 対象外状況判断 if (ColumnDgv.SelectedRows.Count < 1) return; if (ColumnDgv.SelectedRows[0].Index == ColumnDgv.RowCount - 1) return; // 事前取得 var row = ColumnDgv.SelectedRows[0]; var index = row.Index; // Removeしたら-1になるからその前に取得しておく // 行移動の実行 ColumnDgv.Rows.Remove(row); ColumnDgv.Rows.Insert(index + 1, row); // 行選択 ColumnDgv.ClearSelection(); ColumnDgv.Rows[index + 1].Selected = true; } /// /// DGVキー押下イベント /// /// /// private void ColumnDgv_KeyDown(object sender, KeyEventArgs e) { if (e.Alt && (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)) { if (e.KeyCode == Keys.Up) { UpButton_Click(sender, e); } else { DownButton_Click(sender, e); } // 既定動作抑止(メニュー呼び/ビープを止める) e.Handled = true; e.SuppressKeyPress = true; } } private const string KEY_NAME_DISPLAY_INDEX_CSV = "DisplayIndexCsv"; private const string KEY_NAME_COLUMN_VISIBLE_CSV = "ColumnVisibleCsv"; private const string KEY_NAME_DISPLAY_NAME_CSV = "DisplayNameCsv"; /// /// OKボタンクリック時 /// /// /// private void OkButton_Click(object sender, EventArgs e) { //List indexList = new List(); //foreach (DataGridViewRow row in ColumnDgv.Rows) indexList.Add(((DataGridViewColumn)row.Tag).Index); ////MessageBox.Show(string.Join(" ", indexList)); //if (indexList.Max() != ColumnDgv.Rows.Count - 1) if (_colNameToRowIndexDic.Values.Max() != ColumnDgv.Rows.Count - 1) { MessageBox.Show(this, $"_colNameToRowIndexDicがおかしい![_colNameToRowIndexDicのMax:{_colNameToRowIndexDic.Values.Max()},グリッド行数:{ColumnDgv.Rows.Count}]"); this.DialogResult = DialogResult.None; return; } // 表示順 int[] displayIndexArray = new int[ColumnDgv.Rows.Count]; foreach (DataGridViewRow row in ColumnDgv.Rows) displayIndexArray[_colNameToRowIndexDic[((DataGridViewRow)row).Cells[0].Value.ToString()]] = ((DataGridViewRow)row).Index; // INI書き込み IniHelper.WriteValue(null, _targetDgv.Parent.Name + "." + _targetDgv.Name, KEY_NAME_DISPLAY_INDEX_CSV, string.Join(",", displayIndexArray)); // 表示非表示 bool[] visibleArray = new bool[ColumnDgv.Rows.Count]; foreach (DataGridViewRow row in ColumnDgv.Rows) visibleArray[_colNameToRowIndexDic[((DataGridViewRow)row).Cells[0].Value.ToString()]] = IsVisible(((DataGridViewRow)row).Index); // INI書き込み IniHelper.WriteValue(null, _targetDgv.Parent.Name + "." + _targetDgv.Name, KEY_NAME_COLUMN_VISIBLE_CSV, string.Join(",", visibleArray)); // 表示名称 string[] nameArray = new string[ColumnDgv.Rows.Count]; foreach (DataGridViewRow row in ColumnDgv.Rows) { var name = string.Empty; if (row.Cells[COL_NAME_DISPLAY_NAME].Value.ToString() != row.Cells[COL_NAME_DEFAULT_DISPLAY_NAME].Value.ToString()) { name = row.Cells[COL_NAME_DISPLAY_NAME] .Value .ToString() .Replace(",", "@@@COMMA@@@"); } nameArray[_colNameToRowIndexDic[((DataGridViewRow)row).Cells[0].Value.ToString()]] = name; } // INI書き込み IniHelper.WriteValue(null, _targetDgv.Parent.Name + "." + _targetDgv.Name, KEY_NAME_DISPLAY_NAME_CSV, string.Join(",", nameArray)); } /// /// フォーム閉じるとき /// /// /// private void DgvColumnEditorForm_FormClosing(object sender, FormClosingEventArgs e) { // バウンズの保存 FormHelper.SaveFormBounds(this); // DGV列幅保存 DgvHelper.DgvSaveColWidth(ColumnDgv); } /// /// 表示名の編集メニュークリック /// /// /// private void EditDisplayNameMenu_Click(object sender, EventArgs e) { if (ColumnDgv.SelectedRows.Count < 1) return; var rowIndex = ColumnDgv.SelectedRows[0].Index; var text = ColumnDgv[COL_NAME_DISPLAY_NAME,rowIndex].Value.ToString(); var defaultText= ColumnDgv[COL_NAME_DEFAULT_DISPLAY_NAME, rowIndex].Value.ToString(); if (string.IsNullOrEmpty(text)) text = defaultText; using (var form = new TextInputForm(text)) { if (form.ShowDialog() == DialogResult.Cancel) return; ColumnDgv[COL_NAME_DISPLAY_NAME, rowIndex].Value = form.EditedText == defaultText ? "" : form.EditedText; } } private void VisibleMenu_Click(object sender, EventArgs e) { if (ColumnDgv.SelectedRows.Count < 1) return; var rowIndex = ColumnDgv.SelectedRows[0].Index; SetVisible(rowIndex, !IsVisible(rowIndex)); } private bool IsVisible(int rowIndex) { if (rowIndex < 0 || rowIndex >= ColumnDgv.Rows.Count) return true; if (ColumnDgv.Rows[rowIndex].DefaultCellStyle == null) return true; if (ColumnDgv.Rows[rowIndex].DefaultCellStyle.Font == null) return true; var style = ColumnDgv.Rows[rowIndex].DefaultCellStyle.Font.Style; return !((style & FontStyle.Strikeout) == FontStyle.Strikeout); } private void SetVisible(int rowIndex, bool visible) { if (rowIndex < 0 || rowIndex >= ColumnDgv.Rows.Count) return; if (ColumnDgv.Rows[rowIndex].DefaultCellStyle == null) return; Font font; if (ColumnDgv.Rows[rowIndex].DefaultCellStyle.Font != null) { font = ColumnDgv.Rows[rowIndex].DefaultCellStyle.Font; } else { font = ColumnDgv.DefaultCellStyle.Font; } var style = font.Style; if (visible) { style &= ~FontStyle.Strikeout; } else { style |= FontStyle.Strikeout; } ColumnDgv.Rows[rowIndex].DefaultCellStyle.Font = new Font(font, style); // 背景色 ColumnDgv.Rows[rowIndex].DefaultCellStyle.BackColor = visible ? Color.Empty : Color.DarkGray; } } }