آموزش ساخت Analog Clock در #C
سلام بر تمامی برنامه نویسان سی شارپ …
در این مطلب آموزشی سی شارپ ، یاد می گیرین که چگونه یک ساعت آنالوگ بسازین.
ابتدا کدهای زیر را قرار دهید:(این کدها نباید درون رویداد خاصی باشند و خودشان مستقل هستند)
// Set the form's size according to the menu item's caption.
private void SetSize(ToolStripMenuItem menu_item)
{
// Resize the form.
string text = menu_item.Text.Replace("&", "");
int width = int.Parse(text.Split('x')[0]);
int height = int.Parse(text.Split('x')[1]);
ClientSize = new Size(width, height);
// Set the form's region.
GraphicsPath path = new GraphicsPath();
path.AddEllipse(this.ClientRectangle);
this.Region = new Region(path);
// Redraw.
Refresh();
// Check the correct menu item.
ToolStripMenuItem[] items =
{
ctxSize100x100,
ctxSize150x150,
ctxSize100x150,
ctxSize200x200
};
foreach (ToolStripMenuItem item in items)
item.Checked = (item == menu_item);
}
private void SetSize(ToolStripMenuItem menu_item)
{
// Resize the form.
string text = menu_item.Text.Replace("&", "");
int width = int.Parse(text.Split('x')[0]);
int height = int.Parse(text.Split('x')[1]);
ClientSize = new Size(width, height);
// Set the form's region.
GraphicsPath path = new GraphicsPath();
path.AddEllipse(this.ClientRectangle);
this.Region = new Region(path);
// Redraw.
Refresh();
// Check the correct menu item.
ToolStripMenuItem[] items =
{
ctxSize100x100,
ctxSize150x150,
ctxSize100x150,
ctxSize200x200
};
foreach (ToolStripMenuItem item in items)
item.Checked = (item == menu_item);
}
سپس کدهای زیر را در رویداد Form1_MouseDown بنویسید:
if (e.Button == MouseButtons.Left)
{
// Release the mouse capture started by the mouse down.
this.Capture = false;
// Create and send a WM_NCLBUTTONDOWN message.
const int WM_NCLBUTTONDOWN = 0x00A1;
const int HTCAPTION = 2;
Message msg =
Message.Create(this.Handle, WM_NCLBUTTONDOWN,
new IntPtr(HTCAPTION), IntPtr.Zero);
this.DefWndProc(ref msg);
}
{
// Release the mouse capture started by the mouse down.
this.Capture = false;
// Create and send a WM_NCLBUTTONDOWN message.
const int WM_NCLBUTTONDOWN = 0x00A1;
const int HTCAPTION = 2;
Message msg =
Message.Create(this.Handle, WM_NCLBUTTONDOWN,
new IntPtr(HTCAPTION), IntPtr.Zero);
this.DefWndProc(ref msg);
}
سپس این کدها را درون رویداد Paint مربوط به فرمتان قرار دهید:
e.Graphics.Clear(BackColor);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint =
TextRenderingHint.AntiAliasGridFit;
// Translate to center the drawing.
e.Graphics.TranslateTransform(
ClientSize.Width / 2,
ClientSize.Height / 2);
// Draw the face including tick marks.
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint =
TextRenderingHint.AntiAliasGridFit;
// Translate to center the drawing.
e.Graphics.TranslateTransform(
ClientSize.Width / 2,
ClientSize.Height / 2);
// Draw the face including tick marks.
// بقیه ی کدها مخصوص کاربران وی آی پی
DrawClockFace(e.Graphics);
// Show the time digitally.
ShowDigitalTime(e.Graphics);
// Draw the hands.
DrawClockHands(e.Graphics);
// Draw the center.
e.Graphics.FillEllipse(Brushes.Blue, -5, -5, 10, 10);
DrawClockFace(e.Graphics);
// Show the time digitally.
ShowDigitalTime(e.Graphics);
// Draw the hands.
DrawClockHands(e.Graphics);
// Draw the center.
e.Graphics.FillEllipse(Brushes.Blue, -5, -5, 10, 10);