SelectButton.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Resources;
  11. namespace SmartCoalApplication.Core.CustomControl
  12. {
  13. public partial class SelectButton : BaseUserControl
  14. {
  15. private PictureBox pictureBox1;
  16. private Label buttonText;
  17. private string textEX;
  18. public SelectButton()
  19. {
  20. InitializeComponent();
  21. InitializeLanguageText();
  22. }
  23. private void InitializeLanguageText()
  24. {
  25. if(!DesignMode)
  26. {
  27. this.buttonText.Text = PdnResources.GetString("Menu.butto.Text");
  28. this.textEX = PdnResources.GetString("Menu.butto.Text");
  29. }
  30. }
  31. private void InitializeComponent()
  32. {
  33. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  34. this.buttonText = new System.Windows.Forms.Label();
  35. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  36. this.SuspendLayout();
  37. //
  38. // pictureBox1
  39. //
  40. this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  41. | System.Windows.Forms.AnchorStyles.Left)
  42. | System.Windows.Forms.AnchorStyles.Right)));
  43. this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlLight;
  44. this.pictureBox1.Location = new System.Drawing.Point(1, 1);
  45. this.pictureBox1.Name = "pictureBox1";
  46. this.pictureBox1.Size = new System.Drawing.Size(144, 37);
  47. this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  48. this.pictureBox1.TabIndex = 0;
  49. this.pictureBox1.TabStop = false;
  50. //
  51. // buttonText
  52. //
  53. this.buttonText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  54. | System.Windows.Forms.AnchorStyles.Left)
  55. | System.Windows.Forms.AnchorStyles.Right)));
  56. this.buttonText.BackColor = System.Drawing.SystemColors.ControlLight;
  57. this.buttonText.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  58. this.buttonText.Location = new System.Drawing.Point(2, 2);
  59. this.buttonText.Name = "buttonText";
  60. this.buttonText.Size = new System.Drawing.Size(142, 35);
  61. this.buttonText.TabIndex = 1;
  62. this.buttonText.Text = "按钮";
  63. this.buttonText.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  64. this.buttonText.Click += new System.EventHandler(this.buttonText_Click);
  65. this.buttonText.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
  66. this.buttonText.MouseLeave += new System.EventHandler(this.pictureBox1_MouseLeave);
  67. this.buttonText.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
  68. this.buttonText.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
  69. //
  70. // SelectButton
  71. //
  72. this.BackColor = System.Drawing.SystemColors.Highlight;
  73. this.Controls.Add(this.buttonText);
  74. this.Controls.Add(this.pictureBox1);
  75. this.Name = "SelectButton";
  76. this.Size = new System.Drawing.Size(146, 39);
  77. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  78. this.ResumeLayout(false);
  79. }
  80. /// <summary>
  81. /// 按钮文字显示
  82. /// </summary>
  83. [Description("按钮显示的文字")]
  84. public string BtnText
  85. {
  86. get { return textEX; }
  87. set
  88. {
  89. textEX = value;
  90. buttonText.Text = textEX;
  91. }
  92. }
  93. /// <summary>
  94. /// 按钮选中状态
  95. /// </summary>
  96. private bool selectEX = false;
  97. [Description("按钮显示的文字")]
  98. public bool BtnSelect
  99. {
  100. get { return selectEX; }
  101. set
  102. {
  103. selectEX = value;
  104. if (selectEX)
  105. {
  106. this.BackColor = System.Drawing.SystemColors.Highlight;
  107. this.pictureBox1.BackColor = System.Drawing.SystemColors.Highlight;
  108. }
  109. else
  110. {
  111. this.BackColor = System.Drawing.SystemColors.ControlDark;
  112. this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlLight;
  113. }
  114. }
  115. }
  116. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  117. {
  118. this.buttonText.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
  119. this.pictureBox1.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
  120. this.BackColor = System.Drawing.SystemColors.Highlight;
  121. }
  122. private void pictureBox1_MouseLeave(object sender, EventArgs e)
  123. {
  124. this.buttonText.BackColor = System.Drawing.SystemColors.ControlLight;
  125. if (selectEX)
  126. {
  127. this.pictureBox1.BackColor = System.Drawing.SystemColors.Highlight;
  128. this.BackColor = System.Drawing.SystemColors.Highlight;
  129. }
  130. else
  131. {
  132. this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlLight;
  133. this.BackColor = System.Drawing.SystemColors.ControlDark;
  134. }
  135. }
  136. public void buttonText_Click(object sender, EventArgs e)
  137. {
  138. OnClick(e);//这句话很关键!!!
  139. }
  140. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  141. {
  142. this.Focus();
  143. this.pictureBox1.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
  144. }
  145. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  146. {
  147. this.pictureBox1.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
  148. }
  149. //————————————————
  150. //版权声明:本文为CSDN博主「bgbgxia」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
  151. //原文链接:https://blog.csdn.net/weixin_42266753/article/details/100803493
  152. }
  153. }