123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 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();
- }
- }
- }
- }
|