forked from lipoyang/SOEM.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JoystickBar.cs
53 lines (48 loc) · 1.75 KB
/
JoystickBar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Drawing;
using System.Windows.Forms;
namespace EasyTest
{
class JoystickBar : ProgressBar
{
public JoystickBar()
{
this.SetStyle(ControlStyles.UserPaint, true);
/*
//Paintイベントが発生するようにする
//ダブルバッファリングを有効にする
base.SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true);
*/
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rec = e.ClipRectangle;
Brush foreBrush = new SolidBrush(this.ForeColor);
// 枠線描画
if (ProgressBarRenderer.IsSupported)
ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle);
int Neutral = Maximum / 2;
int center = rec.Width / 2;
// プラスの場合
if (Value > Neutral)
{
int offset = Value - Neutral;
int width = (rec.Width-4) * offset / Maximum;
int height = rec.Height - 4;
e.Graphics.FillRectangle(foreBrush, 2 + center, 2, width, height);
}
// マイナスの場合
else if (Value < Neutral)
{
int offset = Neutral - Value;
int width = (rec.Width - 4) * offset / Maximum;
int height = rec.Height - 4;
e.Graphics.FillRectangle(foreBrush, 2 + center - width, 2, width, height);
}
// 真ん中の線
e.Graphics.DrawLine(Pens.DarkGray, center+1, 1, center+1, rec.Height - 1);
}
}
}