Curs10
-
Upload
ratiu-silviu -
Category
Documents
-
view
16 -
download
0
Transcript of Curs10
-
Programare Windows I Visual C++ Curs 10
lect. dr. Marius Apetrii 1
Exemplu pentru utilizarea metodelor grafice
In cadrul cursului, metodele grafice vor fi exemplificate folosind programul de mai jos.
System::Windows::Forms::Cursor ^cursor_creion;
Image ^img_bg,^img,^img_temp;
Graphics ^g_bg,^g,^g_temp;
int x0,y0;
bool apasat;
Pen^ creion;
Brush^ pensula;
Color culoare_transparenta;
void nou(){
img_bg=gcnew Bitmap(p->Width,p->Height);
g_bg=Graphics::FromImage(img_bg);
img=gcnew Bitmap(p->Width,p->Height);
g=Graphics::FromImage(img);
img_temp=gcnew Bitmap(p->Width,p->Height);
g_temp=Graphics::FromImage(img_temp);
}
void salveaza(){
Image^ img1=gcnew Bitmap(p->Width,p->Height);
Graphics^ g1=Graphics::FromImage(img1);
g1->DrawImage(img_bg,0,0,p->Width,p->Height);
-
Programare Windows I Visual C++ Curs 10
lect. dr. Marius Apetrii 2
g1->DrawImage(img,0,0,p->Width,p->Height);
try{
sf->DefaultExt="jpeg";
sf->FileName="Figura.jpeg";
sf->Filter = "JPEG files (*.jpeg)|*.jpeg|All files
(*.*)|*.*" ;
sf->FilterIndex = 1 ;
sf->RestoreDirectory = true ;
sf->ShowDialog();
img1->Save(sf-
>FileName,System::Drawing::Imaging::ImageFormat::Jpeg);
}
catch(Exception^){}
}
void creaza_creion(){
creion=gcnew Pen(culoare->BackColor,dimensiune-
>SelectedIndex*2+1);
creion->DashStyle=(Drawing::Drawing2D::DashStyle)stil-
>SelectedIndex;
}
void creaza_penson(){
pensula=gcnew SolidBrush(culoare->BackColor);
}
void deseneaza_instrument(String^ nume_instrument,Rectangle
r,Graphics^ g){
if(nume_instrument=="Fundal")
g->DrawImage(imageList1->Images[0],r);
else if(nume_instrument=="Creion")
g->DrawImage(imageList1->Images[1],r);
else if(nume_instrument=="Radiera")
g->DrawImage(imageList1->Images[2],r);
else if(nume_instrument=="Linie")
g->DrawLine(gcnew
Pen(Color::Blue,3),r.X+2,r.Y+r.Height-4,r.X+r.Width-4,r.Y+2);
if(nume_instrument=="Patrat")
g->DrawRectangle(gcnew
Pen(Color::Blue,3),r.X+2,r.Y+2,r.Height-4,r.Height-4);
else if(nume_instrument=="Dreptunghi")
g->DrawRectangle(gcnew
Pen(Color::Blue,3),r.X+2,r.Y+7,r.Height-4,r.Height-14);
else if(nume_instrument=="Cerc")
g->DrawEllipse(gcnew
Pen(Color::Blue,3),r.X+2,r.Y+2,r.Height-4,r.Height-4);
else if(nume_instrument=="Elipsa")
g->DrawEllipse(gcnew
Pen(Color::Blue,3),r.X+2,r.Y+7,r.Height-4,r.Height-14);
if(nume_instrument=="Patrat plin")
g-
>FillRectangle(Brushes::Blue,r.X+2,r.Y+2,r.Height-4,r.Height-4);
else if(nume_instrument=="Dreptunghi plin")
g-
>FillRectangle(Brushes::Blue,r.X+2,r.Y+7,r.Height-4,r.Height-14);
else if(nume_instrument=="Cerc plin")
g->FillEllipse(Brushes::Blue,r.X+2,r.Y+2,r.Height-
4,r.Height-4);
-
Programare Windows I Visual C++ Curs 10
lect. dr. Marius Apetrii 3
else if(nume_instrument=="Elipsa plina")
g->FillEllipse(Brushes::Blue,r.X+2,r.Y+7,r.Height-
4,r.Height-14);
}
void schimba_culoare_fundal(){
g_bg->Clear(culoare->BackColor);
p->Refresh();
}
void schimba_imagine_fundal(){
try{
of->DefaultExt="jpeg";
of->FileName="";
of->Filter = "JPEG (*.jpeg)|*.jpeg|Bitmap Files
(*.bmp)|*.bmp|All files (*.*)|*.*";
of->FilterIndex = 1 ;
of->RestoreDirectory = true ;
of->ShowDialog();
Image^ img1=Image::FromFile(of->FileName);
g_bg->Clear(culoare_transparenta);
g_bg->DrawImage(img1,0,0,img1->Width,img1-
>Height);
p->Refresh();
}
catch(Exception^){}
}
void instrument_creion(int x,int y){
g->FillEllipse(gcnew SolidBrush(creion->Color),x0-creion-
>Width/2,y0-creion->Width/2,creion->Width,creion->Width);
g->DrawLine(creion,x0,y0,x,y);
x0=x;
y0=y;
p->Refresh();
}
void instrument_linie(int x,int y,bool temporar){
g_temp->Clear(culoare_transparenta);
if(temporar)
g_temp->DrawLine(creion,x0,y0,x,y);
else
g->DrawLine(creion,x0,y0,x,y);
p->Refresh();
}
void instrument_patrat(int x,int y,bool temporar,bool plin){
int l=Math::Abs(x-x0);
if(Math::Abs(y-y0)x0?0:1),y0-
l*(y>y0?0:1),l,l);
g_temp->Clear(culoare_transparenta);
if(temporar)
if(plin)
-
Programare Windows I Visual C++ Curs 10
lect. dr. Marius Apetrii 4
g_temp->FillRectangle(pensula,r);
else
g_temp->DrawRectangle(creion,r);
else
if(plin)
g->FillRectangle(pensula,r);
else
g->DrawRectangle(creion,r);
p->Refresh();
}
void instrument_dreptunghi(int x,int y,bool temporar,bool
plin){
Rectangle r=Rectangle(x0FillRectangle(pensula,r);
else
g_temp->DrawRectangle(creion,r);
else
if(plin)
g->FillRectangle(pensula,r);
else
g->DrawRectangle(creion,r);
p->Refresh();
}
void instrument_cerc(int x,int y,bool temporar,bool plin){
int l=Math::Abs(x-x0);
if(Math::Abs(y-y0)x0?0:1),y0-
l*(y>y0?0:1),l,l);
g_temp->Clear(culoare_transparenta);
if(temporar)
if(plin)
g_temp->FillEllipse(pensula,r);
else
g_temp->DrawEllipse(creion,r);
else
if(plin)
g->FillEllipse(pensula,r);
else
g->DrawEllipse(creion,r);
p->Refresh();
}
void instrument_elipsa(int x,int y,bool temporar,bool plin){
Rectangle r=Rectangle(x0FillEllipse(pensula,r);
else
g_temp->DrawEllipse(creion,r);
-
Programare Windows I Visual C++ Curs 10
lect. dr. Marius Apetrii 5
else
if(plin)
g->FillEllipse(pensula,r);
else
g->DrawEllipse(creion,r);
p->Refresh();
}
void instrument_radiera(int x,int y,bool mouse_up){
g_temp->Clear(culoare_transparenta);
Rectangle r=Rectangle(x-creion->Width/2,y-creion-
>Width/2,creion->Width,creion->Width);
if(!mouse_up)g_temp->FillRectangle(Brushes::DarkGray,r);
g->FillRectangle(pensula,r);
p->Refresh();
}
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e) {
culoare_transparenta=Color::FromArgb(0,0,0,0);
cursor_creion=gcnew
System::Windows::Forms::Cursor("resurse\\cursor.cur");
apasat=false;
nou();
for(int i=0;iItems->Add((i).ToString());
stil->SelectedIndex=0;
for(int i=0;iItems-
>Add((i).ToString());
dimensiune->SelectedIndex=1;
instrument->Items->Add("Fundal");
instrument->Items->Add("Creion");
instrument->Items->Add("Linie");
instrument->Items->Add("Patrat");
instrument->Items->Add("Dreptunghi");
instrument->Items->Add("Cerc");
instrument->Items->Add("Elipsa");
instrument->Items->Add("Patrat plin");
instrument->Items->Add("Dreptunghi plin");
instrument->Items->Add("Cerc plin");
instrument->Items->Add("Elipsa plina");
instrument->Items->Add("Radiera");
instrument->SelectedIndex=0;
creaza_creion();
creaza_penson();
p->Refresh();
}
private: System::Void pictureBox2_Click(System::Object^ sender,
System::EventArgs^ e) {
System::Windows::Forms::DialogResult raspuns;
-
Programare Windows I Visual C++ Curs 10
lect. dr. Marius Apetrii 6
raspuns = MessageBox::Show( "Creati alt desen?", "???",
MessageBoxButtons::YesNo);
if ( raspuns ==
System::Windows::Forms::DialogResult::Yes )
{
nou();
p->Refresh();
}
}
private: System::Void pictureBox1_Click(System::Object^ sender,
System::EventArgs^ e) {
salveaza();
}
private: System::Void culoare_Click(System::Object^ sender,
System::EventArgs^ e) {
cd->ShowDialog();
culoare->BackColor=cd->Color;
creaza_creion();
creaza_penson();
}
private: System::Void stil_SelectedIndexChanged(System::Object^ sender,
System::EventArgs^ e) {
creaza_creion();
}
private: System::Void dimensiune_SelectedIndexChanged(System::Object^
sender, System::EventArgs^ e) {
creaza_creion();
}
private: System::Void stil_DrawItem(System::Object^ sender,
System::Windows::Forms::DrawItemEventArgs^ e) {
try{
e->DrawBackground();
Pen^ creion = gcnew Pen(Color::Black,6);
creion-
>DashStyle=(System::Drawing::Drawing2D::DashStyle)e->Index;
int x=2;
int y=e->Bounds.Top+e->Bounds.Height/2+3;
e->Graphics->DrawLine(creion,x,y,x+e->Bounds.Width-4,y);
e->DrawFocusRectangle();
}
catch (Exception^) { }
}
private: System::Void dimensiune_DrawItem(System::Object^ sender,
System::Windows::Forms::DrawItemEventArgs^ e) {
try{
e->DrawBackground();
Rectangle r = Rectangle((e->Bounds.Width-(e-
>Index*2+1))/2,e->Bounds.Top +(e->Bounds.Height-(e->Index*2+1))/2,(e-
>Index*2+1),(e->Index*2+1));
e->Graphics->FillRectangle(Brushes::Black, r);
e->DrawFocusRectangle();
}
catch (Exception^) { }
}
private: System::Void instrument_DrawItem(System::Object^ sender,
System::Windows::Forms::DrawItemEventArgs^ e) {
-
Programare Windows I Visual C++ Curs 10
lect. dr. Marius Apetrii 7
try{
e->DrawBackground();
Rectangle r1 = Rectangle( 2, e->Bounds.Top + 2,e-
>Bounds.Height-4, e->Bounds.Height - 4 );
Rectangle r2 = Rectangle( e->Bounds.Height, e-
>Bounds.Top + 2,e->Bounds.Width-e->Bounds.Height-2, e->Bounds.Height - 4 );
deseneaza_instrument(instrument->Items[e->Index]-
>ToString(),r1,e->Graphics);
System::Drawing::Font^ font = gcnew
System::Drawing::Font( "Arial", 16, FontStyle::Bold );
StringFormat ^sf=gcnew StringFormat();
sf->LineAlignment=StringAlignment::Center;
e->Graphics->DrawString(instrument->Items[e->Index]-
>ToString(), font,Brushes::Black, r2,sf);
e->DrawFocusRectangle();
}
catch (Exception^) { }
}
private: System::Void p_Paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^ e) {
e->Graphics->DrawImage(img_bg,0,0,p->Width,p->Height);
e->Graphics->DrawImage(img,0,0,p->Width,p->Height);
e->Graphics->DrawImage(img_temp,0,0,p->Width,p->Height);
}
private: System::Void culoareFundalToolStripMenuItem_Click(System::Object^
sender, System::EventArgs^ e) {
schimba_culoare_fundal();
}
private: System::Void imagineFundalToolStripMenuItem_Click(System::Object^
sender, System::EventArgs^ e) {
schimba_imagine_fundal();
}
private: System::Void p_MouseDown(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e) {
apasat=true;
x0=e->X;
y0=e->Y;
switch(instrument->SelectedIndex){
case 1:
instrument_creion(e->X,e->Y);
break;
case 11:
instrument_radiera(e->X,e->Y,false);
break;
}
}
private: System::Void p_MouseMove(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e) {
if(apasat){
switch(instrument->SelectedIndex){
case 1:
instrument_creion(e->X,e->Y);
break;
case 2:
instrument_linie(e->X,e->Y,true);
break;
-
Programare Windows I Visual C++ Curs 10
lect. dr. Marius Apetrii 8
case 3:
instrument_patrat(e->X,e-
>Y,true,false);
break;
case 4:
instrument_dreptunghi(e->X,e-
>Y,true,false);
break;
case 5:
instrument_cerc(e->X,e->Y,true,false);
break;
case 6:
instrument_elipsa(e->X,e-
>Y,true,false);
break;
case 7:
instrument_patrat(e->X,e->Y,true,true);
break;
case 8:
instrument_dreptunghi(e->X,e-
>Y,true,true);
break;
case 9:
instrument_cerc(e->X,e->Y,true,true);
break;
case 10:
instrument_elipsa(e->X,e->Y,true,true);
break;
case 11:
instrument_radiera(e->X,e->Y,false);
break;
}
}
}
private: System::Void p_MouseUp(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e) {
apasat=false;
switch(instrument->SelectedIndex){
case 1:
instrument_creion(e->X,e->Y);
break;
case 2:
instrument_linie(e->X,e->Y,false);
break;
case 3:
instrument_patrat(e->X,e->Y,false,false);
break;
case 4:
instrument_dreptunghi(e->X,e->Y,false,false);
break;
case 5:
instrument_cerc(e->X,e->Y,false,false);
break;
case 6:
instrument_elipsa(e->X,e->Y,false,false);
break;
-
Programare Windows I Visual C++ Curs 10
lect. dr. Marius Apetrii 9
case 7:
instrument_patrat(e->X,e->Y,false,true);
break;
case 8:
instrument_dreptunghi(e->X,e->Y,false,true);
break;
case 9:
instrument_cerc(e->X,e->Y,false,true);
break;
case 10:
instrument_elipsa(e->X,e->Y,false,true);
break;
case 11:
instrument_radiera(e->X,e->Y,true);
break;
}
}
private: System::Void p_MouseEnter(System::Object^ sender,
System::EventArgs^ e) {
if(instrument->SelectedIndex==1){
p->Cursor=cursor_creion;
}
}
private: System::Void p_MouseLeave(System::Object^ sender,
System::EventArgs^ e) {
p->Cursor=System::Windows::Forms::Cursors::Default;
}