Accueil > > > SHAPE, DES DESSIN DANS UN FENÊTRE [CODE ET COMPIL AVEC VC++ 6.0]
SHAPE, DES DESSIN DANS UN FENÊTRE [CODE ET COMPIL AVEC VC++ 6.0]
Information sur la source
Description
IL va dessiner 2 figures, un rectangle et une ellipse
Source
- // Vous allez devoir créer plusierus document, mais je vous les mets dans le
- // zip
-
- /* TOUT D'ABORD LE FICHIER D'EN-TETE: CPoint.h */
-
- struct Point
- {
- // Constructeurs:
- Point() { x = 0; y = 0; };
- Point(int ix, int iy) { x =ix; y = iy; };
-
- // Attributs:
- int x;
- int y;
- };
-
- /* MAINTENANT LE FICHIER D'EN-TETE: CRect.h */
-
- #include "CPoint.h"
-
- struct Rect
- {
- //Constructeurs:
- Rect() { m_ptTopLeft = Point(); m_ptBotRight = Point(10,10); }
- Rect(Point tl, Point br) { m_ptTopLeft = tl; m_ptBotRight = br; };
-
- // Attributs:
- Point m_ptTopLeft;
- Point m_ptBotRight;
-
- //Fonctions membres:
- void SetRect(Point tl, Point br)
- {m_ptTopLeft = tl; m_ptBotRight = br; };
- void SetRect(int x1, int y1, int x2, int y2) {
- m_ptTopLeft.x = x1; m_ptTopLeft.y = y1;
- m_ptBotRight.x = x2; m_ptBotRight.y = y2; };
- };
-
- /* MAINTENANT LE FICHIER D'EN-TETE: Shape.h
-
- #include "CRect.h"
-
- // Constantes pour génération aléatoire de types de figure
- const int NUM_TYPES = 2;
-
- // Types de figure possible
- enum ShpType
- {
- shpRectangle,
- shpEllipse
- // On peut en ajouter d'autres
- };
-
- // Classe Shape
- struct Shape
- {
- Rect m_rectShape; // rectangle circonscrit
- ShpType m_typeShape; // rectangle, ellipse, etc.
- };
-
- /* MAINTENANT LE FICHIER SOURCE: Shape.cpp */
-
- #include "Shape.h"
- #include <stdio.h> // Ppour printf, sprintf
- #include <assert.h> // Pour assert
- #include <stdlib.h> // Pour rand, srand, abs
- #include <time.h> // Pour time
- #include <string.h> // Pour strcpy
-
- // Prototype de fonctiion globale
- void DrawShape(Shape* pS);
- void MoveShape(Shape* pS, Point p);
- int RandomCoord();
- ShpType RandomType();
-
- const int COORD_MAX = 1000;
-
- // main
- int main(int argc, char* argv[])
- {
- // Crée une figure sur la pile
- Shape shp1;
- shp1.m_rectShape = Rect(Point(20,20), Point(50,50));
- shp1.m_typeShape = shpRectangle;
- DrawShape(&shp1);
-
- // Déplace une figure
- MoveShape(&shp1, Point(25,25));
- DrawShape(&shp1);
-
- // Crée une figure sur le tas
- Shape* pShp2 = new Shape;
- assert(pShp2 != NULL);
- pShp2 ->m_rectShape = Rect(Point(100,100), Point(150,150));
- pShp2 ->m_typeShape = shpEllipse;
- DrawShape(pShp2);
-
- delete pShp2;
-
- // Crée 20 figures aléatoires dans le tableau
- Point pt1, pt2;
- Shape* arShps[20];
- srand((unsigned)time(NULL)); // Nombre aléatoire
- for(int i = 0; i < 20; i++)
- {
- // Crée une nouvelle figure
- Shape* pShp = new Shape;
- assert(pShp != NULL);
- // Propose des coordonnée
- pt1 = Point(RandomCoord(), RandomCoord());
- pt2 = Point(RandomCoord(), RandomCoord());
- pShp ->m_rectShape = Rect(pt1, pt2);
- // Précise si rectangle ou ellipse
- pShp ->m_typeShape = RandomType();
- // Ajoute un tableau
- arShps[i] = pShp;
- // "Dessine" la figure sous forme de chaîne
- DrawShape(arShps[i]);
- }
- // Déplace une figure
- MoveShape(arShps[0], Point(20,20));
- DrawShape(arShps[0]);
-
- // Efface toutes les figures
- for(int j = 0; j < 20; j++) delete arShps[j];
-
- return 0;
- } // Fin du main
-
- // Définitions des fonctions globales.
- // "Dessine" la figure
- void DrawShape(Shape* pS)
- {
- Rect rect = pS ->m_rectShape;
- int x1 = rect.m_ptTopLeft.x;
- int y1 = rect.m_ptTopLeft.y;
- int x2 = rect.m_ptBotRight.x;
- int y2 = rect.m_ptBotRight.y;
- // Détermine le type de figure sous forme de chaîne
- char szType[20];
- switch(pS ->m_typeShape)
- {
- case shpRectangle: strcpy(szType, "retangle");
- break;
- case shpEllipse: strcpy(szType, "ellipse");
- break;
- default: strcpy(szType, "erreur type de figure");
- };
-
- // "Dessine" la figure sous forme de chaîne
- // Exemple: "rectangle à (34, 76, 987, 800) "
- printf("%s à (%d, %d, %d, %d)\n", szType, x1, y1, x2, y2);
- } // Fin de DrawShape
-
- // Déplace la ifgure à la nouvelle position p
- void MoveShape(Shape *pS, Point p /* New m_TopLeft*/)
- {
- Rect rect = pS ->m_rectShape;
- int width = abs(rect.m_ptBotRight.x - rect.m_ptTopLeft.x);
- int height = abs(rect.m_ptBotRight.y - rect.m_ptTopLeft.y);
-
- // Nouveau CSG m_TopLeft = p.
- pS ->m_rectShape.m_ptTopLeft = p;
- // Nouveau CID m_ptBotRight calculé à partir de p
- // en utilisant la taille de la figure
- pS ->m_rectShape.m_ptBotRight = Point(p.x + width, p.y + height);
- } // Fin de MoveShape
-
- // Fonctions auxiliaires globale
-
- // Génère une coordonée poitive aléatoire
- // dans une zone de dessin COORD_MAX x COORD_MAN
- int RandomCoord()
- {
- // Fonde les nouvelles coordonées sur les anciennes
- static int nLastCoord; // Initialisée
- // automatiquement à 0
- // puis modifié lors de
- // chaque appel
-
- // Obtient un nombre aléatoire compris entre 0 et RAND_MAX (32767)
- int nNextCoord = rand();
- int nFudge = rand() % 100; // Génère facteur entre
- // 0 et 99
- nLastCoord = (nNextCoord > nLastCoord ? nNextCoord : nLastCoord);
-
- // Limite le nombre à une valeur entre 0 et COORD_MAX - 1(inclus)
- nLastCoord = (nLastCoord + nFudge) % COORD_MAX;
- return nLastCoord;
- } // Fin de RandomCoord
-
- // Le nombre aléatoire génère un type de figure
- ShpType RandomTypz()
- {
- // 0 à 1 (= shpRetangle à shpEllipse)
- return (ShpType)(rand() % NUM_TYPES);
- }
// Vous allez devoir créer plusierus document, mais je vous les mets dans le
// zip
/* TOUT D'ABORD LE FICHIER D'EN-TETE: CPoint.h */
struct Point
{
// Constructeurs:
Point() { x = 0; y = 0; };
Point(int ix, int iy) { x =ix; y = iy; };
// Attributs:
int x;
int y;
};
/* MAINTENANT LE FICHIER D'EN-TETE: CRect.h */
#include "CPoint.h"
struct Rect
{
//Constructeurs:
Rect() { m_ptTopLeft = Point(); m_ptBotRight = Point(10,10); }
Rect(Point tl, Point br) { m_ptTopLeft = tl; m_ptBotRight = br; };
// Attributs:
Point m_ptTopLeft;
Point m_ptBotRight;
//Fonctions membres:
void SetRect(Point tl, Point br)
{m_ptTopLeft = tl; m_ptBotRight = br; };
void SetRect(int x1, int y1, int x2, int y2) {
m_ptTopLeft.x = x1; m_ptTopLeft.y = y1;
m_ptBotRight.x = x2; m_ptBotRight.y = y2; };
};
/* MAINTENANT LE FICHIER D'EN-TETE: Shape.h
#include "CRect.h"
// Constantes pour génération aléatoire de types de figure
const int NUM_TYPES = 2;
// Types de figure possible
enum ShpType
{
shpRectangle,
shpEllipse
// On peut en ajouter d'autres
};
// Classe Shape
struct Shape
{
Rect m_rectShape; // rectangle circonscrit
ShpType m_typeShape; // rectangle, ellipse, etc.
};
/* MAINTENANT LE FICHIER SOURCE: Shape.cpp */
#include "Shape.h"
#include <stdio.h> // Ppour printf, sprintf
#include <assert.h> // Pour assert
#include <stdlib.h> // Pour rand, srand, abs
#include <time.h> // Pour time
#include <string.h> // Pour strcpy
// Prototype de fonctiion globale
void DrawShape(Shape* pS);
void MoveShape(Shape* pS, Point p);
int RandomCoord();
ShpType RandomType();
const int COORD_MAX = 1000;
// main
int main(int argc, char* argv[])
{
// Crée une figure sur la pile
Shape shp1;
shp1.m_rectShape = Rect(Point(20,20), Point(50,50));
shp1.m_typeShape = shpRectangle;
DrawShape(&shp1);
// Déplace une figure
MoveShape(&shp1, Point(25,25));
DrawShape(&shp1);
// Crée une figure sur le tas
Shape* pShp2 = new Shape;
assert(pShp2 != NULL);
pShp2 ->m_rectShape = Rect(Point(100,100), Point(150,150));
pShp2 ->m_typeShape = shpEllipse;
DrawShape(pShp2);
delete pShp2;
// Crée 20 figures aléatoires dans le tableau
Point pt1, pt2;
Shape* arShps[20];
srand((unsigned)time(NULL)); // Nombre aléatoire
for(int i = 0; i < 20; i++)
{
// Crée une nouvelle figure
Shape* pShp = new Shape;
assert(pShp != NULL);
// Propose des coordonnée
pt1 = Point(RandomCoord(), RandomCoord());
pt2 = Point(RandomCoord(), RandomCoord());
pShp ->m_rectShape = Rect(pt1, pt2);
// Précise si rectangle ou ellipse
pShp ->m_typeShape = RandomType();
// Ajoute un tableau
arShps[i] = pShp;
// "Dessine" la figure sous forme de chaîne
DrawShape(arShps[i]);
}
// Déplace une figure
MoveShape(arShps[0], Point(20,20));
DrawShape(arShps[0]);
// Efface toutes les figures
for(int j = 0; j < 20; j++) delete arShps[j];
return 0;
} // Fin du main
// Définitions des fonctions globales.
// "Dessine" la figure
void DrawShape(Shape* pS)
{
Rect rect = pS ->m_rectShape;
int x1 = rect.m_ptTopLeft.x;
int y1 = rect.m_ptTopLeft.y;
int x2 = rect.m_ptBotRight.x;
int y2 = rect.m_ptBotRight.y;
// Détermine le type de figure sous forme de chaîne
char szType[20];
switch(pS ->m_typeShape)
{
case shpRectangle: strcpy(szType, "retangle");
break;
case shpEllipse: strcpy(szType, "ellipse");
break;
default: strcpy(szType, "erreur type de figure");
};
// "Dessine" la figure sous forme de chaîne
// Exemple: "rectangle à (34, 76, 987, 800) "
printf("%s à (%d, %d, %d, %d)\n", szType, x1, y1, x2, y2);
} // Fin de DrawShape
// Déplace la ifgure à la nouvelle position p
void MoveShape(Shape *pS, Point p /* New m_TopLeft*/)
{
Rect rect = pS ->m_rectShape;
int width = abs(rect.m_ptBotRight.x - rect.m_ptTopLeft.x);
int height = abs(rect.m_ptBotRight.y - rect.m_ptTopLeft.y);
// Nouveau CSG m_TopLeft = p.
pS ->m_rectShape.m_ptTopLeft = p;
// Nouveau CID m_ptBotRight calculé à partir de p
// en utilisant la taille de la figure
pS ->m_rectShape.m_ptBotRight = Point(p.x + width, p.y + height);
} // Fin de MoveShape
// Fonctions auxiliaires globale
// Génère une coordonée poitive aléatoire
// dans une zone de dessin COORD_MAX x COORD_MAN
int RandomCoord()
{
// Fonde les nouvelles coordonées sur les anciennes
static int nLastCoord; // Initialisée
// automatiquement à 0
// puis modifié lors de
// chaque appel
// Obtient un nombre aléatoire compris entre 0 et RAND_MAX (32767)
int nNextCoord = rand();
int nFudge = rand() % 100; // Génère facteur entre
// 0 et 99
nLastCoord = (nNextCoord > nLastCoord ? nNextCoord : nLastCoord);
// Limite le nombre à une valeur entre 0 et COORD_MAX - 1(inclus)
nLastCoord = (nLastCoord + nFudge) % COORD_MAX;
return nLastCoord;
} // Fin de RandomCoord
// Le nombre aléatoire génère un type de figure
ShpType RandomTypz()
{
// 0 à 1 (= shpRetangle à shpEllipse)
return (ShpType)(rand() % NUM_TYPES);
}
Conclusion
PAs de bug vec VC++ 6.0
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
ETENDRE LE TEAM WEB ACCESS DE TFS 2012 - STEP 0ETENDRE LE TEAM WEB ACCESS DE TFS 2012 - STEP 0 par Philess
L'extensibilité du Team Web Access
Le Web Access (site d'équipe) de Team Foundation Server a été complètement réécrit dans la version 2012 avec pas moins de 400.000 lignes de JavaScript. Ce nouveau modèle a été pensé pour offrir de grandes...
Cliquez pour lire la suite de l'article par Philess SIMULER FACILEMENT L'ENVOI DE MAILSIMULER FACILEMENT L'ENVOI DE MAIL par JeremyJeanson
il m'a été demandé, à plusieurs reprises, comment je faisais pour simuler l'envoi de mail lors de mes démos de Workflow Foundation. Ma solution est plutôt simple : j'utilise la configuration par défaut du SmtpClient et j'oriente les mails vers un dossier ...
Cliquez pour lire la suite de l'article par JeremyJeanson VOTEZ POUR LE TOP 10 DES INFLUENCEURS SHAREPOINT FRANCOPHONES !VOTEZ POUR LE TOP 10 DES INFLUENCEURS SHAREPOINT FRANCOPHONES ! par Patrick Guimonet
Si ce n'est déjà fait (comme plus de 600 personnes déjà), il est encore temps de voter pour le concours TOP 10 des influenceurs SharePoint francophones ! Il est organisé par harmon.ie et accessible ici : http://harmon.ie/top-...
Cliquez pour lire la suite de l'article par Patrick Guimonet [CONF'SHAREPOINT] DERNIER RAPPEL ! :-)[CONF'SHAREPOINT] DERNIER RAPPEL ! :-) par Patrick Guimonet
La Conf'SharePoint en chiffres c'est : 3 jours de SharePoint ! 4 parcours et 60 sessions 17 partenaires représentant toutes les fac...
Cliquez pour lire la suite de l'article par Patrick Guimonet
Forum
MéMORY EN C++MéMORY EN C++ par C6Dj
Cliquez pour lire la suite par C6Dj
Logiciels
Easy-Planning (4.5.0.11)EASY-PLANNING (4.5.0.11)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté a... Cliquez pour télécharger Easy-Planning CVEasy (3.1.0.51)CVEASY (3.1.0.51)PHMSD-CVEasy est un logiciel d'aide à la rédaction de CV d'une simplicité déconcertante.
PHMSD-C... Cliquez pour télécharger CVEasy LettresFaciles 2011 (8.6.0.31)LETTRESFACILES 2011 (8.6.0.31)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011 sDEVIS-FACTURES vlPRO (8.4.2.62)SDEVIS-FACTURES VLPRO (8.4.2.62)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO Devis-Factures PHMSD (2.1.0.11)DEVIS-FACTURES PHMSD (2.1.0.11)Configuration minimale
Nécessite Windows™ 2000, XP, Windows 7, 8, Vista (Service Pack à... Cliquez pour télécharger Devis-Factures PHMSD
|