using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace UAS_MES_NEW.CustomControl.TextBoxWithIcon { public partial class TextAreaForm : Form { [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; //用于接收TextBoxWithTextArea所在的窗体和对应的控件的名称 string FormName1; string[] ControlName1; int X1; int Y1; //打开窗体的集合 FormCollection fmCollection = Application.OpenForms; public string FormName { get { return FormName1; } set { FormName1 = value; } } public string[] ControlName { get { return ControlName1; } set { ControlName1 = value; } } public int X { get { return X1; } set { X1 = value; } } public int Y { get { return Y1; } set { Y1 = value; } } public TextAreaForm() { InitializeComponent(); this.headBar1.MouseDown += new MouseEventHandler(this.headBar1_MouseDown); } private void TextAreaForm_Load(object sender, EventArgs e) { this.Location = new System.Drawing.Point(X1, Y1); //指定起始的控件为Form层,然后往下找,因为是按照Parent的顺序填充的字符串,所以需要倒序查找 Control c = fmCollection[FormName1]; for (int i = ControlName.Length - 1; i >= 0; i--) { //如果遍历到i=0就找到了最底层的TextBox if (i == 0) { //将界面上输入的值填充到文本框中 TextArea.Text = c.Controls[ControlName1[i]].Text; } else { c = c.Controls[ControlName1[i]]; } } } private void headBar1_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } //确认按钮,找到窗体,然后找到控件赋值 private void Confirm_Click(object sender, EventArgs e) { //同上,只是赋值反过来 Control c = fmCollection[FormName1]; for (int i = ControlName.Length - 1; i >= 0; i--) { if (i == 0) { c.Controls[ControlName1[i]].Text = TextArea.Text; } else { c = c.Controls[ControlName1[i]]; } } this.Close(); this.Dispose(); } //清除按钮 private void Clean_Click(object sender, EventArgs e) { TextArea.Clear(); } private void TextAreaForm_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Escape) { this.Close(); this.Dispose(); } } } }