private void TextBox_TextChanged(object sender, EventArgs e) { if (!(sender is TextBox textBox)) { return; } var path = PathEx.Combine(textBox.Text); if (string.IsNullOrWhiteSpace(path) || _path == path || !File.Exists(path) || GetIconFromFile(path, 0, true) == null) { return; } TaskBarProgress.SetState(Handle, TaskBarProgressState.Indeterminate); _path = path; _panel.Enabled = false; _textBox.Enabled = false; _buttonPanel.SuspendLayout(); _buttonPanel.BorderStyle = BorderStyle.None; _buttonPanel.Controls.Clear(); _buttonPanel.Controls.Add(_progressCircle); _buttonPanel.ResumeLayout(false); _progressCircle.Active = true; _timer.Enabled = true; }
private void Timer_Tick(object sender, EventArgs e) { lock (Locker) { if (!(sender is Timer timer)) { return; } if (_count == 0 && _panel.Controls.Count > 0) { _panel.Controls.Clear(); } for (var i = 0; i < 3; i++) { _panel.SuspendLayout(); try { var box = new IconBox(_path, _count++, _button.BackColor, _button.ForeColor, _button.FlatAppearance.MouseOverBackColor); if (_panel.Controls.Contains(box)) { box.Dispose(); continue; } _iconBoxes.Add(box); _panel.Controls.Add(box); } catch (Exception ex) when(ex.IsCaught()) { timer.Enabled = false; if (_count > 0) { _count = 0; } break; } finally { _panel.ResumeLayout(false); } } var max = _panel.Width / _panel.Controls[0].Width; for (var i = 0; i < _panel.Controls.Count; i++) { if (_panel.Controls[i] == null) { continue; } var line = i / max; var column = i - line * max; _panel.Controls[i].Location = new Point(column * _panel.Controls[i].Width, line * _panel.Controls[i].Height); } if (timer.Enabled || _panel.Enabled) { return; } _panel.Enabled = true; _textBox.Enabled = true; _progressCircle.Active = false; _buttonPanel.SuspendLayout(); _buttonPanel.Controls.Clear(); _buttonPanel.Controls.Add(_button); _buttonPanel.BorderStyle = BorderStyle.FixedSingle; _buttonPanel.ResumeLayout(false); TaskBarProgress.SetState(Handle, TaskBarProgressState.NoProgress); if (!_panel.Focus()) { _panel.Select(); } } }
/// <summary> /// Initializes an instance of the <see cref="IconBrowserDialog"/> class. /// </summary> /// <param name="path"> /// The path of the file to open. /// </param> /// <param name="backColor"> /// The background color of the dialog box. /// </param> /// <param name="foreColor"> /// The foreground color of the dialog box. /// </param> /// <param name="buttonFace"> /// The button color of the dialog box. /// </param> /// <param name="buttonText"> /// The button text color of the dialog box. /// </param> /// <param name="buttonHighlight"> /// The button highlight color of the dialog box. /// </param> public IconBrowserDialog(string path = "%system%\\imageres.dll", Color?backColor = null, Color?foreColor = null, Color?buttonFace = null, Color?buttonText = null, Color?buttonHighlight = null) { _components = new Container(); SuspendLayout(); var resPath = PathEx.Combine(path); if (PathEx.IsDir(resPath)) { resPath = PathEx.Combine(path, "imageres.dll"); } if (!File.Exists(resPath)) { resPath = PathEx.Combine("%system%", "imageres.dll"); } var resLoc = Path.GetDirectoryName(resPath); AutoScaleDimensions = new SizeF(96f, 96f); AutoScaleMode = AutoScaleMode.Dpi; BackColor = backColor ?? SystemColors.Control; ForeColor = foreColor ?? SystemColors.ControlText; Font = new Font("Consolas", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0); Icon = GetSystemIcon(IconIndex.DirectorySearch, true, resLoc); MaximizeBox = false; MaximumSize = new Size(680, Screen.FromHandle(Handle).WorkingArea.Height); MinimizeBox = false; MinimumSize = new Size(680, 448); Name = "IconBrowserForm"; Size = MinimumSize; SizeGripStyle = SizeGripStyle.Hide; StartPosition = FormStartPosition.CenterScreen; Text = UIStrings.ResourceBrowser; _tableLayoutPanel = new TableLayoutPanel { BackColor = Color.Transparent, CellBorderStyle = TableLayoutPanelCellBorderStyle.None, Dock = DockStyle.Fill, Name = "tableLayoutPanel", RowCount = 2 }; _tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100f)); _tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 36)); Controls.Add(_tableLayoutPanel); _panel = new Panel { AutoScroll = true, BackColor = buttonFace ?? SystemColors.ButtonFace, BorderStyle = BorderStyle.FixedSingle, Enabled = false, ForeColor = buttonText ?? SystemColors.ControlText, Dock = DockStyle.Fill, Name = "panel", TabIndex = 0 }; _panel.Scroll += (s, e) => (s as Panel)?.Update(); _tableLayoutPanel.Controls.Add(_panel, 0, 0); _innerTableLayoutPanel = new TableLayoutPanel { BackColor = Color.Transparent, ColumnCount = 2, CellBorderStyle = TableLayoutPanelCellBorderStyle.None, Dock = DockStyle.Fill, Name = "innerTableLayoutPanel" }; _innerTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f)); _innerTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 24)); _tableLayoutPanel.Controls.Add(_innerTableLayoutPanel, 0, 1); _textBox = new TextBox { BorderStyle = BorderStyle.FixedSingle, Dock = DockStyle.Top, Font = Font, Name = "textBox", TabIndex = 1 }; _textBox.TextChanged += TextBox_TextChanged; _innerTableLayoutPanel.Controls.Add(_textBox, 0, 0); _buttonPanel = new Panel { Anchor = AnchorStyles.Top | AnchorStyles.Right, BackColor = Color.Transparent, BorderStyle = BorderStyle.FixedSingle, Name = "buttonPanel", Size = new Size(20, 20) }; _innerTableLayoutPanel.Controls.Add(_buttonPanel, 1, 0); _button = new Button { BackColor = buttonFace ?? SystemColors.ButtonFace, BackgroundImage = GetSystemIcon(IconIndex.Directory, false, resLoc).ToBitmap(), BackgroundImageLayout = ImageLayout.Zoom, Dock = DockStyle.Fill, FlatStyle = FlatStyle.Flat, Font = Font, ForeColor = buttonText ?? SystemColors.ControlText, Name = "button", TabIndex = 2, UseVisualStyleBackColor = false }; _button.FlatAppearance.BorderSize = 0; _button.FlatAppearance.MouseOverBackColor = buttonHighlight ?? ProfessionalColors.ButtonSelectedHighlight; _button.Click += Button_Click; _buttonPanel.Controls.Add(_button); _progressCircle = new ProgressCircle { Active = false, Anchor = AnchorStyles.Top | AnchorStyles.Right, BackColor = Color.Transparent, Dock = DockStyle.Fill, ForeColor = (backColor ?? SystemColors.Control).InvertRgb().ToGrayScale(), RotationSpeed = 80, Thickness = 2, Visible = true }; _timer = new Timer(_components) { Interval = 1 }; _timer.Tick += Timer_Tick; _iconBoxes = new List <IconBox>(); Shown += (sender, args) => TaskBarProgress.SetState(Handle, TaskBarProgressState.Indeterminate); ResumeLayout(false); PerformLayout(); var curPath = PathEx.Combine(path); if (!File.Exists(curPath)) { curPath = resPath; } if (!File.Exists(curPath)) { return; } _textBox.Text = curPath; }