// --------------------------- top of file Button4_Direction.cs ---------------------------- using System.Windows.Forms; using System.Drawing; using System; public class Button4_Direction : Panel { // ============== new versions ================ public new string Text="S"; public new EventHandler Click; // ============================================ bool pushed = false, valid=false; Timer timer; public Button4_Direction() { timer = new Timer(); timer.Interval = 100; timer.Tick += tick; 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 OnMouseDown(MouseEventArgs e) { pushed = true; valid = true; tick(null, null); timer.Start(); Invalidate(); } // ========================================================== protected override void OnMouseUp(MouseEventArgs e) { pushed = false; valid = false; timer.Stop(); count = 0; Invalidate(); } // ========================================================== int count = 0; private void tick(object o, EventArgs e) { if (valid && (count == 0 || count > 8)) { Click(this, null); } count++; } // ========================================================== 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); int x0 = this.Width / 2-3, y0 = this.Height / 2-3; int s = this.Width / 7, m = this.Width*2 / 7; if (Text == "W") { g.FillPolygon(new SolidBrush(ForeColor), new Point[]{ new Point(x0-s,y0), new Point(x0+s,y0+m), new Point(x0+s,y0-m)}); } else if (Text == "E") { g.FillPolygon(new SolidBrush(ForeColor), new Point[]{ new Point(x0+s,y0), new Point(x0-s,y0-m), new Point(x0-s,y0+m)}); } else if (Text == "N") { g.FillPolygon(new SolidBrush(ForeColor), new Point[]{ new Point(x0,y0-s-1), new Point(x0-m,y0+s), new Point(x0+m,y0+s)}); } else if (Text == "S") { g.FillPolygon(new SolidBrush(ForeColor), new Point[]{ new Point(x0,y0+s), new Point(x0+m,y0-s), new Point(x0-m,y0-s)}); } else { 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 Button4_Direction ###################### // --------------------------- bottom of file Button4_Direction.cs --------------------------