C# 自定义按钮及其事件处理

    科技2026-06-06  12

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace study.NewFolder1.Right.Basic { public delegate void MyEventHandler(object sender, string e); public partial class Title : UserControl { public Title() { InitializeComponent(); } public Title(string str) { InitializeComponent(); bar.Text = str; } public bool show = true; public event MyEventHandler Changed; // 这里也可以用自带的EventHandler private void Title_Click(object sender, EventArgs e) { if (show) { this.pictureBox1.Image = global::study.Properties.Resources._1073328; show = false; } else { this.pictureBox1.Image = global::study.Properties.Resources._1073317; show = true; } Changed(show, this.bar.Text); } #region 组件设计器生成的代码 /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.bar = new System.Windows.Forms.Label(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // bar // this.bar.AutoSize = true; this.bar.Font = new System.Drawing.Font("宋体", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.bar.Location = new System.Drawing.Point(22, 10); this.bar.Name = "bar"; this.bar.Size = new System.Drawing.Size(47, 19); this.bar.TabIndex = 0; this.bar.Text = "标题"; this.bar.Click += new System.EventHandler(this.Title_Click); // // pictureBox1 // this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.pictureBox1.Image = global::study.Properties.Resources._1073328; this.pictureBox1.InitialImage = null; this.pictureBox1.Location = new System.Drawing.Point(610, 6); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(30, 27); this.pictureBox1.TabIndex = 1; this.pictureBox1.TabStop = false; this.pictureBox1.Click += new System.EventHandler(this.Title_Click); // // Title // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.ControlDark; this.Controls.Add(this.pictureBox1); this.Controls.Add(this.bar); this.Name = "Title"; this.Size = new System.Drawing.Size(653, 40); this.Click += new System.EventHandler(this.Title_Click); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label bar; private System.Windows.Forms.PictureBox pictureBox1; } }

     

    Processed: 0.028, SQL: 9