// --------------------------- top of file Button2_Pusheffective.cs ---------------------------- using System.Windows.Forms; using System.Drawing; using System; public class Button2_Pusheffective : Panel { // ============== new versions ================ public new string Text; public new Font Font; public new EventHandler Click; // ============================================ bool pushed = false, valid=false; public Button2_Pusheffective() { MouseMove += (s, e) => { if(pushed && e.X > 0 && e.Y > 0 && e.X < this.Width && e.Y < this.Height) { if (!valid) { valid = true; Invalidate(); } } else if (valid) { valid = false; Invalidate(); } }; } // ========================================================== protected override void OnSizeChanged(EventArgs e) { FontFamily family = base.Font.FontFamily; if (this.Font != null) { family = this.Font.FontFamily; } this.Font = new Font(family, this.Height*3 / 8); } // ========================================================== protected override void OnMouseDown(MouseEventArgs e) { pushed = true; valid = true; Click(this, null); Invalidate(); } // ========================================================== protected override void OnMouseUp(MouseEventArgs e) { pushed = false; valid = false; Invalidate(); } // ========================================================== protected override void OnPaint(PaintEventArgs pevent) { Graphics g = pevent.Graphics; base.OnPaint(pevent); g.DrawRectangle(new Pen(ForeColor), 0, 0, this.Width-1, this.Height-1); g.DrawRectangle(new Pen(Color.Black,2), 2, 2, this.Width-4, this.Height-4); if (valid) { g.TranslateTransform(2, 2); } else { g.TranslateTransform(1, 1); } g.FillRectangle(new SolidBrush(BackColor), 0, 0, this.Width-4, this.Height-4); stringcenter(g, this.Width / 2, this.Height / 2); } // ========================================================== private void stringcenter(Graphics g, int x, int y) { StringFormat strfmt=StringFormat.GenericTypographic; // フォントの幅が正確に求められる!!! strfmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces; SizeF sizef=g.MeasureString(Text, this.Font, new Point(0,0), strfmt); g.DrawString(Text, this.Font, new SolidBrush(ForeColor), x-sizef.Width / 2-4, y-sizef.Height / 2-2); } // ========================================================== } // ###################### end of class Button2_Pusheffective ###################### // --------------------------- bottom of file Button2_Pusheffective.cs --------------------------