< 필드 (멤버변수) >
private Graphics g; // 아날로그 시계를 그릴 GRPHICS 객체
private bool aClockFlag = false;
// aclockflag bool함수가 false > 디지털 , ture > 아날로그
private Point center; // 중심점
private double radius; // 반지름
private int hourHand; // 시침 길이
private int minuteHand; // 분침 길이
private int secondHand; // 초침 길이
const int clientSize = 300; // 크기를 지정
const int clockSize = 200; // 시계 크기
(const 상수를 지정해주면 그 값은 바뀔 수 없음)
public Form1()
{
// 클라이언트 속성 지정
InitializeComponent();
this.Text = "formClock";
this.ClientSize = new Size(clientSize, clockSize + menuStrip1.Height);
//pancel 지정
panel1.BackColor = Color.Cyan;
panel1.Width = clockSize + 1;
panel1.Height = clockSize + 1;
panel1.Location = new Point(clientSize / 2 - clockSize / 2, clientSize / 2 - clockSize / 2 + menuStrip1.Height);
g = panel1.CreateGraphics(); // pancel에서 그려주는 ~ 그래픽 객체 !!!
aClockSetting();
dClockSetting();
timerSetting();
}
aClockSetting 메소드
private void aClockSetting()
{
center = new Point(clientSize / 2, clockSize / 2);
radius = clockSize / 2;
hourHand = (int)(radius * 0.45);
minuteHand = (int)(radius * 0.55);
secondHand = (int)(radius * 0.65);
}
dClockSetting매소드
private void dClockSetting()
{
fDate = new Font("맑은고딕", 12, FontStyle.Bold);
fTime = new Font("맑은고딕", 32, FontStyle.Bold | FontStyle.Italic);
bDate = Brushes.Goldenrod;
bTime = Brushes.Black;
}
T_tick 매소드
private void T_tick(object sender, EventArgs e)
{
DateTime c = DateTime.Now;
panel1.Refresh();
if (aClockFlag == true)
{//아날로그라면?
DrawClockFace();
double radHr = (c.Hour % 12 + c.Minute / 60.0) * 30 * Math.PI / 180;
double radMin = (c.Minute + c.Second / 60.0) * 60.0 * 6 * Math.PI / 180;
double radSec = c.Second * 6 * Math.PI / 180;
DrawHands(radHr, radMin, radSec);
}
else
{//디지털이라면?
string date = DateTime.Today.ToString("D");
string time = string.Format("{0:D2}:{1:D2}:{2:D2}", c.Hour, c.Minute, c.Second);
g.DrawString(date, fDate, bDate, new Point(10, 70));
g.DrawString(time, fDate, bDate, new Point(10, 100));
}
}
DrawClockFace 매소드
private void DrawClockFace()
{
const int penWidth = 30;
Pen p = new Pen(Brushes.LightSteelBlue, penWidth);
g.DrawEllipse(p, penWidth / 2, penWidth / 2, clockSize - penWidth, clockSize - penWidth);
}
private void DrawHands(double radHr, double radMin, double radSec)
{
}