123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- namespace BeatLyrics.Tool.Dialogs
- {
- internal class ContextDialog
- {
- public static void PopTextBox(Point screenPoint, string orig, Action<DialogResult, string> ret)
- {
- var form = new Form
- {
- FormBorderStyle = FormBorderStyle.None,
- StartPosition = FormStartPosition.Manual,
- Location = screenPoint,
- ShowInTaskbar = false,
- };
- var textBox = new TextBox
- {
- Left = 0,
- Top = 0,
- };
- form.Controls.Add(textBox);
- var closed = false;
- textBox.PreviewKeyDown += delegate (object sender, PreviewKeyDownEventArgs args)
- {
- if (closed) return;
- if (args.KeyCode == Keys.Escape)
- {
- form.DialogResult = DialogResult.Cancel;
- form.Close();
- }
- if (args.KeyCode == Keys.Enter)
- {
- form.DialogResult = DialogResult.OK;
- form.Close();
- }
- };
- textBox.LostFocus += delegate
- {
- if (closed) return;
- form.DialogResult = DialogResult.OK;
- form.Close();
- };
- form.FormClosed += delegate { closed = true; ret(form.DialogResult, textBox.Text); };
- form.Show();
- form.Height = textBox.Height;
- textBox.Text = orig;
- textBox.SelectAll();
- var tw = TextRenderer.MeasureText(orig, textBox.Font).Width;
- if (tw > textBox.Width) textBox.Width = tw;
- form.Width = textBox.Width;
- }
- }
- }
|