Programare Windows I.

Visual C++

Laborator 9

Laborator 9
1) Să se scrie o aplicaţie pentru realizarea unui ceas analogic.
Interfaţa aplicaţiei:

Vom crea un proiect nou şi pe formularul Form1 vom adăuga un control PictureBox, un control Timer şi un
ContextMenuStrip.
Pentru controlul ContextMenuStrip vom avea:
Name = L"contextMenuStrip1";
ShowImageMargin = false;

Meniul de context va avea două elemente:
Iesire şi Schimba forma (ca în imaginea de mai sus).
Controlului PictureBox îi vom da numele p, adică
Name = L"p";

Pentru formularul Form1 vom avea:
1

P1. g->DrawString("IX". sf->Alignment=StringAlignment::Center. g->DrawLine(gcnew Pen(Color::Blue.ms). g->DrawString("XII".//centrul Image ^img. ss=ss-15.Brushes::Black.y0ms.3).97*Math::Cos(i*Math::PI/30).ms).Width/2.2*r.ss.f).secunda float r.f).Brushes::Black.Brushes::Black. ms=g->MeasureString("IX".y0-r.y0+r*Math::Sin(i*Math::PI/6)). Text = L"Ceas".sf).mm.Width. g->DrawString("III".f).2*r). Visual C++ Laborator 9 ContextMenuStrip = this->contextMenuStrip1.97*Math::Sin(i*Math::PI/30)). g->DrawEllipse(Pens::Blue.92*Math::Cos(i*Math::PI/6).f. } for(int i=0. Name = L"Form1".Width/2.Height/2).1). SizeF ms.minut.Height). Point P2(x0+r*Math::Cos(i*Math::PI/6).92*Math::Sin(i*Math::PI/6)). Point P2(x0+r*Math::Cos(i*Math::PI/30). Pen ^creion1. ms=g->MeasureString("VI".f.i++){ Point P1(x0+r*0.y0+r).y0+r*Math::Sin(i*Math::PI/30)).Brushes::Black.P2). StringFormat ^sf=gcnew StringFormat().i++){ Point P1(x0+r*0.f.P2).RectangleF(Point(x0ms. Graphics ^g.y0ms. Pen ^creion2. for(int i=0.P1.sf).RectangleF(Point(x0-ms.RectangleF(Point(x0+r. float hh.i<12.//ora. ms=g->MeasureString("III".sf).sf).//raza int x0.Programare Windows I.y0-rms. g->DrawLine(gcnew Pen(Color::Blue.RectangleF(Point(x0-r-ms.Height/2).h.y0+r*0. //ora 2 . Definim funcţiile: void deseneaza(){ g->Clear(Color::White).ms). g->DrawString("VI". mm=mm-15. ms=g->MeasureString("XII".f).y0. Definim variabilele: int w.y0+r*0.ms). } hh=hh-3+mm/60. System::Drawing::Font ^f.x0-r.i<60.f.

//distanta de la cercul ceasului la cea mai apropiata margine r=(w<=h)?w/2-temp:h/2-temp.7*Math::Cos(mm*Math::PI/30). p->Location=Point(0.f->Style). hh=DateTime::Now. //secunda P1=Point(x0-r*0. System::EventArgs^ f=gcnew System::Drawing::Font("Time New Roman". p->Width=w.y0+r*0. w=this->ClientSize.y0r*0. int temp=20+(ms. x0=w/2.h). p->Height=h. g->DrawLine(gcnew Pen(Color::Red. Point P2(x0+r*0. h=this->ClientSize.P2). P2=Point(x0+r*0. Visual C++ Laborator 9 Point P1(x0-r*0. g->DrawLine(gcnew Pen(Color::Green. mm=DateTime::Now.4).P2).5*Math::Cos(hh*Math::PI/6). g->FillEllipse(Brushes::Blue.Width.f).y0r*0. } private: void redimensioneaza(){ hh=DateTime::Now.4).P1.y0+r*0. mm=DateTime::Now. y0=h/2.18+(w<h?w-300:h-300)/20.x0-4.Height.09*Math::Sin(ss*Math::PI/30)). img=gcnew Bitmap(w. ss=DateTime::Now.05*Math::Sin(hh*Math::PI/6)).Minute.0).y0-4.P2). g=Graphics::FromImage(img). this->Refresh(). deseneaza().8.FontStyle::Bold).8). f=gcnew System::Drawing::Font(f->Name.Width.Programare Windows I.9*Math::Sin(ss*Math::PI/30)).09*Math::Cos(ss*Math::PI/30). 3 e) { .05*Math::Cos(hh*Math::PI/6).6). creion1=gcnew Pen(Color::Blue. timer1->Start().Minute. } La evenimentul Load al formularului Form1 scriem: private: System::Void Form1_Load(System::Object^ sender. //minut P1=Point(x0-r*0.P1.Second. ms=g->MeasureString("XII".9*Math::Cos(ss*Math::PI/30).P1.y0+r*0.07*Math::Sin(mm*Math::PI/30)). P2=Point(x0+r*0.5*Math::Sin(hh*Math::PI/6)).y0r*0.2). g->DrawLine(gcnew Pen(Color::Red.18.Hour.Second.Width<=ms.07*Math::Cos(mm*Math::PI/30).Hour.7*Math::Sin(mm*Math::PI/30)).Height)?ms.Height:ms. ss=DateTime::Now.

int ww=this->Width-10. ss=DateTime::Now. int yy=5.yy+hh/3. gp->AddLine(xx+2*ww/3.yy+hh). gp->AddLine(xx+ww*2/3.yy+2*hh/3). gp->AddLine(xx+ww. System::Windows::Forms::PaintEventArgs^ e) { e->Graphics->DrawImage(img. la evenimentul Tick vom avea: private: System::Void timer1_Tick(System::Object^ hh=DateTime::Now. Visual C++ } Laborator 9 redimensioneaza().0. La evenimentul Resize al formularului Form1 scriem: private: System::Void Form1_Resize(System::Object^ if(this->Width<200)this->Width=200.yy+hh. int hh=this->Height-10.0.xx+2*ww/3. this->ControlBox=false. redimensioneaza(). System::EventArgs^ e) { if(this->ControlBox){ this->Text="".Hour.yy+hh/3). gp->AddLine(xx.yy.yy+2*hh/3.xx+ww. Pentru evenimentul Click asociat butonului Schimba forma din meniul de context avem: private: System::Void schimbaFormaToolStripMenuItem_Click(System::Object^ sender.xx. System::EventArgs^ e) { Pentru timer1.yy+2*hh/3). } sender. 4 . System::Drawing::Drawing2D::GraphicsPath ^gp=gcnew System::Drawing::Drawing2D::GraphicsPath(). int xx=5.yy+2*hh/3. deseneaza().h). System::EventArgs^ e) { La evenimentul Paint al PictureBox-ului p scriem: private: System::Void p_Paint(System::Object^ sender.xx.yy+hh.yy+hh). this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None. } sender. gp->AddLine(xx+ww/3. gp->AddLine(xx+ww.Programare Windows I. } Pentru evenimentul Click asociat butonului Iesire din meniul de context avem: private: System::Void iesireToolStripMenuItem_Click(System::Object^ System::EventArgs^ e) { System::Environment::Exit(1). mm=DateTime::Now.xx+ww/3.yy). gp->AddLine(xx+ww/3.xx+ww.Second. if(this->Height<200)this->Height=200.yy+hh/3). } sender.w.yy.Minute.xx+2*ww/3.

Programare Windows I. } 2) Exemplul 2 (click aici pentru download). this->Text="Ceas analogic". this->Region=nullptr. this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Sizable. Visual C++ } else{ } Laborator 9 this->Region=gcnew System::Drawing::Region(gp). this->ControlBox=true. 5 .

Sign up to vote on this title
UsefulNot useful