Está en la página 1de 23

Universidad Nacional de Ingeniería

Facultad de Ingeniería Mecánica


Curso: MB545 Programación Orientada a Objetos | Profesor: Ing. Roberto Tello Yuen |

UNIDADES DE APRENDIZAJE 7 (Sem 16)

Unidad 7: GRÁFICOS, MENUS Y BASE DE DATOS EN VISUAL C++

Proyectos basados en dialogo y single document. El CDC de Windows, La ventana Física y la


ventana Lógica, ubicando los ejes de Coordenadas. Las Funciones OnPaint(), OnDraw(),gráfica:
líneas, cuadrados, círculos, polígonos, arcos, tortas, textos, puntos, curvas, plumas, brochas.
El Recurso Menú su creación. El Menú y las Ventanas Hijas. Conexión a Base de Datos. Uso del
proyecto basado en Dialogo. Proyecto Single Document.
Universidad Nacional de Ingeniería
Facultad de Ingeniería Mecánica
Curso: MB545 Programación Orientada a Objetos | Profesor: Ing. Roberto Tello Yuen |

Programación Orientada a Objetos (POO)

Unidad 7: GRÁFICOS, Menús Y Base de Datos


En Visual C++
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
NUEVO PROYECTO
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
CONFIGURACIÓN PROYECTO
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen
AGREGAR FORMULARIO

#include "Form_001.h"
using namespace Proy_UNI_002;
int main(){
Application::EnableVisualStyles();
Application::Run(gcnew Form_001());
return 0;
}
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Ejercicio 1: GRAPHICS

Programación Orientada a Objetos (POO)

Ejercicio 1: Realizar un Programa en Visual C++ que permita dibujar y manipular


un gráfico (círculo) usando clases, objetos, herencia, miembros (atributos y
métodos), formulario y diferentes controles.
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Ejercicio 1: Abstracción

Lbl_ancho Lbl_alto

btn_arriba

btn_izquierda

UpDown_radio
btn_derecha

btn_abajo
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Ejercicio 1: Abstracción
pos_x

pos_y

dy

maxFormY

dx maxFormX
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Proy_UNI_002 : Form_001.h (Design)
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Proy_UNI_002 : Form_001.h (Design)
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
#ifndef __Figura2D_H__ Archivo cabecera: Figura2D.h e incluye la Clase (Padre): CFigura2D
#define__Figura2D_H__
#include <iostream>
#include <ctime>
using namespace System::Drawing;

class CFigura2D
{
protected:
int pos_x; // Posición eje X de la Figura
int pos_y; // Posición eje Y de la Figura
int dx; // Ancho de la Figura en el eje X
int dy; // Alto de la Figura en el eje Y

public:
CFigura2D(){
pos_x = 200;
pos_y = 100;
dx = 100;
dy = 100;
srand(time(NULL));
}

~CFigura2D(){}

virtual void dibujar(Graphics ^g) abstract;


};
#endif // !__Figura2D_H__
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Archivo de cabecera: Circulo.h que incluye la Clase (Hija): CCirculo

void mover(int maxFormX, int maxFormY, char direccion){


#ifndef __Circulo_H__ switch (direccion){
#define __Circulo_H__ case 'R': // R=Right=Derecha
if (pos_x + 2* dx < maxFormX) { pos_x = pos_x + dx;}
//Cabecera Figura2D.h contine la clase CFigura2D break;
#include "Figura2D.h" case 'L': // L=Left=Izquierda
if (pos_x >= dx) { pos_x = pos_x - dx; }
class CCirculo :public CFigura2D break;
{ case 'T': // T=Top=Arriba
protected: if (pos_y >= dy) { pos_y = pos_y - dy;}
int radio; break;
case 'B': // B=Bottom=Abajo
public: if (pos_y + 2*dy < maxFormY) { pos_y = pos_y + dy; }
CCirculo() :CFigura2D(){} break;
~CCirculo(){} default:
break;
void actualizaRadio(int _radio){ }
radio = _radio; }
dx = _radio * 2;
dy = _radio * 2; };
}
#endif // !__Circulo_H_
void dibujar(Graphics ^g){
SolidBrush ^pintura = gcnew SolidBrush(Color::FromArgb(200, 150, 255));
g->FillEllipse(pintura, pos_x, pos_y, dx, dy);
}
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Archivo: Form_001.h (Vista Diseño: Controles, Textos, Captions)

Archivo: Form_001.cpp asociado al Proyecto: Proy_UNI_002


#include "Form_001.h"

using namespace Proy_UNI_002;

int main(){
Application::EnableVisualStyles();
Application::Run(gcnew Form_001());
return 0;
}
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
#pragma once Archivo de cabecera: Form_001.h incluye clases de Circulo.h
#include "Circulo.h“

namespace Proy_UNI_002 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

public ref class Form_001 : public System::Windows::Forms::Form


{
// Se crea el Objeto (dinámico) ObjCirculo
CCirculo *ObjCirculo = new CCirculo();

public:
Form_001(void) {
InitializeComponent();
}
protected:
~Form_001() {
if (components)
{
delete components;
}
}
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Archivo de cabecera: Form_001.h incluye clases de Circulo.h

private: System::Windows::Forms::Timer^ timer1;


private: System::Windows::Forms::Button^ btn_derecha;
private: System::Windows::Forms::Button^ btn_izquierda;
private: System::Windows::Forms::Button^ btn_arriba;
private: System::Windows::Forms::Button^ btn_abajo;

private: System::Windows::Forms::Label^ Label3;


private: System::Windows::Forms::Label^ Label4;
private: System::Windows::Forms::Label^ Lbl_ancho;
private: System::Windows::Forms::Label^ Lbl_alto;
private: System::Windows::Forms::NumericUpDown^ UpDown_radio;
private: System::Windows::Forms::Label^ Lbl_Radio;
private: System::ComponentModel::IContainer^ components;

#pragma region Windows Form Designer generated code


Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Archivo de cabecera: Form_001.h incluye clases de Circulo.h
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->btn_derecha = (gcnew System::Windows::Forms::Button());
this->btn_izquierda = (gcnew System::Windows::Forms::Button());
this->btn_arriba = (gcnew System::Windows::Forms::Button());
this->btn_abajo = (gcnew System::Windows::Forms::Button());
this->Label3 = (gcnew System::Windows::Forms::Label());
this->Label4 = (gcnew System::Windows::Forms::Label());
this->Lbl_ancho = (gcnew System::Windows::Forms::Label());
this->Lbl_alto = (gcnew System::Windows::Forms::Label());
this->UpDown_radio = (gcnew System::Windows::Forms::NumericUpDown());
this->Lbl_Radio = (gcnew System::Windows::Forms::Label());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->UpDown_radio))->BeginInit();
this->SuspendLayout();
//
// timer1
//
this->timer1->Enabled = true;
this->timer1->Tick += gcnew System::EventHandler(this, &Form_001::timer1_Tick);
//
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Archivo de cabecera: Form_001.h incluye clases de Circulo.h
// btn_derecha
this->btn_derecha->Location = System::Drawing::Point(98, 39);
this->btn_derecha->Name = L"btn_derecha";
this->btn_derecha->Size = System::Drawing::Size(72, 24);
this->btn_derecha->TabIndex = 0;
this->btn_derecha->Text = L"Derecha->";
this->btn_derecha->UseVisualStyleBackColor = true;
this->btn_derecha->Click += gcnew System::EventHandler(this, &Form_001::button1_Click);
//
// btn_izquierda
this->btn_izquierda->Location = System::Drawing::Point(12, 39);
this->btn_izquierda->Name = L"btn_izquierda";
this->btn_izquierda->Size = System::Drawing::Size(80, 24);
this->btn_izquierda->TabIndex = 1;
this->btn_izquierda->Text = L"<- Izquierda";
this->btn_izquierda->UseVisualStyleBackColor = true;
this->btn_izquierda->Click += gcnew System::EventHandler(this, &Form_001::button2_Click);
//
// btn_arriba
this->btn_arriba->Location = System::Drawing::Point(59, 12);
this->btn_arriba->Name = L"btn_arriba";
this->btn_arriba->Size = System::Drawing::Size(80, 24);
this->btn_arriba->TabIndex = 2;
this->btn_arriba->Text = L"Arriba";
this->btn_arriba->UseVisualStyleBackColor = true;
this->btn_arriba->Click += gcnew System::EventHandler(this, &Form_001::button3_Click);
//
// btn_abajo
this->btn_abajo->Location = System::Drawing::Point(59, 69);
this->btn_abajo->Name = L"btn_abajo";
this->btn_abajo->Size = System::Drawing::Size(80, 24);
this->btn_abajo->TabIndex = 3;
this->btn_abajo->Text = L"Abajo";
this->btn_abajo->UseVisualStyleBackColor = true;
this->btn_abajo->Click += gcnew System::EventHandler(this, &Form_001::button4_Click);
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Archivo de cabecera: Form_001.h incluye clases de Circulo.h
//
// Label3
//
this->Label3->AutoSize = true;
this->Label3->Location = System::Drawing::Point(196, 9);
this->Label3->Name = L"Label3";
this->Label3->Size = System::Drawing::Size(41, 13);
this->Label3->TabIndex = 4;
this->Label3->Text = L"Ancho:";
//
// Label4
//
this->Label4->AutoSize = true;
this->Label4->Location = System::Drawing::Point(284, 9);
this->Label4->Name = L"Label4";
this->Label4->Size = System::Drawing::Size(28, 13);
this->Label4->TabIndex = 5;
this->Label4->Text = L"Alto:";
//
// Lbl_ancho
//
this->Lbl_ancho->AutoSize = true;
this->Lbl_ancho->Location = System::Drawing::Point(233, 9);
this->Lbl_ancho->Name = L"Lbl_ancho";
this->Lbl_ancho->Size = System::Drawing::Size(35, 13);
this->Lbl_ancho->TabIndex = 6;
this->Lbl_ancho->Text = L"label1";
//
// Lbl_alto
//
this->Lbl_alto->AutoSize = true;
this->Lbl_alto->Location = System::Drawing::Point(308, 9);
this->Lbl_alto->Name = L"Lbl_alto";
this->Lbl_alto->Size = System::Drawing::Size(35, 13);
this->Lbl_alto->TabIndex = 7;
this->Lbl_alto->Text = L"label2";
//
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Archivo de cabecera: Form_001.h incluye clases de Circulo.h
//
// UpDown_radio
//
this->UpDown_radio->Location = System::Drawing::Point(421, 7);
this->UpDown_radio->Name = L"UpDown_radio";
this->UpDown_radio->Size = System::Drawing::Size(120, 20);
this->UpDown_radio->TabIndex = 8;
this->UpDown_radio->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 50, 0, 0, 0 });
//
// Lbl_Radio
//
this->Lbl_Radio->AutoSize = true;
this->Lbl_Radio->Location = System::Drawing::Point(377, 9);
this->Lbl_Radio->Name = L"Lbl_Radio";
this->Lbl_Radio->Size = System::Drawing::Size(38, 13);
this->Lbl_Radio->TabIndex = 9;
this->Lbl_Radio->Text = L"Radio:";
//
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Archivo de cabecera: Form_001.h incluye clases de Circulo.h
//
// Form_001
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(659, 259);
this->Controls->Add(this->Lbl_Radio);
this->Controls->Add(this->UpDown_radio);
this->Controls->Add(this->Lbl_alto);
this->Controls->Add(this->Lbl_ancho);
this->Controls->Add(this->Label4);
this->Controls->Add(this->Label3);
this->Controls->Add(this->btn_abajo);
this->Controls->Add(this->btn_arriba);
this->Controls->Add(this->btn_izquierda);
this->Controls->Add(this->btn_derecha);
this->Name = L"Form_001";
this->Text = L"Formulario Visual C++ (incluye Gráficos) - MB545";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->UpDown_radio))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}

#pragma endregion
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Archivo de cabecera: Form_001.h incluye clases de Circulo.h
private:System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {

Graphics ^grafico = this->CreateGraphics();


BufferedGraphicsContext ^espacio = BufferedGraphicsManager::Current;
BufferedGraphics ^Buffer = espacio->Allocate(grafico, this->ClientRectangle);

Buffer->Graphics->Clear(Color::Black);

ObjCirculo-> actualizaRadio(Convert::ToInt32(this->UpDown_radio->Value));
ObjCirculo-> dibujar(Buffer->Graphics);

this->Lbl_ancho->Text = "“ + this->Width;


this->Lbl_alto->Text = "" + this->Height;

Buffer->Render(grafico);

delete Buffer;
delete espacio;
delete grafico;
}
Universidad Nacional de Ingeniería Facultad de Ingeniería Mecánica MB545 POO | Prof: Ing. Roberto Tello Yuen |
Archivo de cabecera: Form_001.h incluye clases de Circulo.h

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {


ObjCirculo-> mover(this->Width, this->Height, 'R’);
}

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {


ObjCirculo-> mover(this->Width, this->Height, 'L’);
}

private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {


ObjCirculo-> mover(this->Width, this->Height, 'T’);
}

private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {


ObjCirculo-> mover(this->Width, this->Height, 'B’);
}

};

}
Universidad Nacional de Ingeniería
Facultad de Ingeniería Mecánica
Curso: MB545 Programación Orientada a Objetos | Profesor: Ing. Roberto Tello Yuen |

UNIDADES DE APRENDIZAJE 7

Unidad 7: GRAFICOS, MENUS Y BASE DE DATOS EN VISUAL C++

Proyectos basados en dialogo y single document. El CDC de Windows, La ventana Física y la


ventana Lógica, ubicando los ejes de Coordenadas. Las Funciones OnPaint(), OnDraw(),gráfica:
líneas, cuadrados, círculos, polígonos, arcos, tortas, textos, puntos, curvas, plumas, brochas.
El Recurso Menú su creación. El Menú y las Ventanas Hijas. Conexión a Base de Datos. Uso del
proyecto basado en Dialogo. Proyecto Single Document.

También podría gustarte