SelectButton.cs 6.6 KB

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