TextAreaForm.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. try { UAS_MES_NEW.I18n.LanguageManager.Localize(this); } catch { }
  70. this.headBar1.MouseDown += new MouseEventHandler(this.headBar1_MouseDown);
  71. }
  72. private void TextAreaForm_Load(object sender, EventArgs e)
  73. {
  74. this.Location = new System.Drawing.Point(X1, Y1);
  75. //指定起始的控件为Form层,然后往下找,因为是按照Parent的顺序填充的字符串,所以需要倒序查找
  76. Control c = fmCollection[FormName1];
  77. for (int i = ControlName.Length - 1; i >= 0; i--)
  78. {
  79. //如果遍历到i=0就找到了最底层的TextBox
  80. if (i == 0)
  81. {
  82. //将界面上输入的值填充到文本框中
  83. TextArea.Text = c.Controls[ControlName1[i]].Text;
  84. }
  85. else
  86. {
  87. c = c.Controls[ControlName1[i]];
  88. }
  89. }
  90. }
  91. private void headBar1_MouseDown(object sender, MouseEventArgs e)
  92. {
  93. ReleaseCapture();
  94. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  95. }
  96. //确认按钮,找到窗体,然后找到控件赋值
  97. private void Confirm_Click(object sender, EventArgs e)
  98. {
  99. //同上,只是赋值反过来
  100. Control c = fmCollection[FormName1];
  101. for (int i = ControlName.Length - 1; i >= 0; i--)
  102. {
  103. if (i == 0)
  104. {
  105. c.Controls[ControlName1[i]].Text = TextArea.Text;
  106. }
  107. else
  108. {
  109. c = c.Controls[ControlName1[i]];
  110. }
  111. }
  112. this.Close();
  113. this.Dispose();
  114. }
  115. //清除按钮
  116. private void Clean_Click(object sender, EventArgs e)
  117. {
  118. TextArea.Clear();
  119. }
  120. private void TextAreaForm_KeyDown(object sender, KeyEventArgs e)
  121. {
  122. if (e.KeyData == Keys.Escape) {
  123. this.Close();
  124. this.Dispose();
  125. }
  126. }
  127. }
  128. }