TextAreaForm.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4. namespace UAS_MES_NEW.CustomControl.TextBoxWithIcon
  5. {
  6. public partial class TextAreaForm : Form
  7. {
  8. [DllImport("user32.dll")]
  9. public static extern bool ReleaseCapture();
  10. [DllImport("user32.dll")]
  11. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  12. public const int WM_SYSCOMMAND = 0x0112;
  13. public const int SC_MOVE = 0xF010;
  14. public const int HTCAPTION = 0x0002;
  15. //用于接收TextBoxWithTextArea所在的窗体和对应的控件的名称
  16. string FormName1;
  17. string[] ControlName1;
  18. int X1;
  19. int Y1;
  20. //打开窗体的集合
  21. FormCollection fmCollection = Application.OpenForms;
  22. public string FormName
  23. {
  24. get
  25. {
  26. return FormName1;
  27. }
  28. set
  29. {
  30. FormName1 = value;
  31. }
  32. }
  33. public string[] ControlName
  34. {
  35. get
  36. {
  37. return ControlName1;
  38. }
  39. set
  40. {
  41. ControlName1 = value;
  42. }
  43. }
  44. public int X
  45. {
  46. get
  47. {
  48. return X1;
  49. }
  50. set
  51. {
  52. X1 = value;
  53. }
  54. }
  55. public int Y
  56. {
  57. get
  58. {
  59. return Y1;
  60. }
  61. set
  62. {
  63. Y1 = value;
  64. }
  65. }
  66. public TextAreaForm()
  67. {
  68. InitializeComponent();
  69. this.headBar1.MouseDown += new MouseEventHandler(this.headBar1_MouseDown);
  70. }
  71. private void TextAreaForm_Load(object sender, EventArgs e)
  72. {
  73. this.Location = new System.Drawing.Point(X1, Y1);
  74. //指定起始的控件为Form层,然后往下找,因为是按照Parent的顺序填充的字符串,所以需要倒序查找
  75. Control c = fmCollection[FormName1];
  76. for (int i = ControlName.Length - 1; i >= 0; i--)
  77. {
  78. //如果遍历到i=0就找到了最底层的TextBox
  79. if (i == 0)
  80. {
  81. //将界面上输入的值填充到文本框中
  82. TextArea.Text = c.Controls[ControlName1[i]].Text;
  83. }
  84. else
  85. {
  86. c = c.Controls[ControlName1[i]];
  87. }
  88. }
  89. }
  90. private void headBar1_MouseDown(object sender, MouseEventArgs e)
  91. {
  92. ReleaseCapture();
  93. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  94. }
  95. //确认按钮,找到窗体,然后找到控件赋值
  96. private void Confirm_Click(object sender, EventArgs e)
  97. {
  98. //同上,只是赋值反过来
  99. Control c = fmCollection[FormName1];
  100. for (int i = ControlName.Length - 1; i >= 0; i--)
  101. {
  102. if (i == 0)
  103. {
  104. c.Controls[ControlName1[i]].Text = TextArea.Text;
  105. }
  106. else
  107. {
  108. c = c.Controls[ControlName1[i]];
  109. }
  110. }
  111. this.Close();
  112. this.Dispose();
  113. }
  114. //清除按钮
  115. private void Clean_Click(object sender, EventArgs e)
  116. {
  117. TextArea.Clear();
  118. }
  119. private void TextAreaForm_KeyDown(object sender, KeyEventArgs e)
  120. {
  121. if (e.KeyData == Keys.Escape) {
  122. this.Close();
  123. this.Dispose();
  124. }
  125. }
  126. }
  127. }