begin process at 2012 02 12 07:14:09
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Tutoriaux

 > TOSTRING DES MFC

TOSTRING DES MFC


 Information sur la source

Note :
8 / 10 - par 1 personne
8,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Tutoriaux Niveau :Débutant Date de création :06/09/2004 Vu :5 133

Auteur : magic_Nono

Ecrire un message privé
Site perso
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

 Description

je sais, les MFC ne sont pas très appréciées, mais bon, on les utilise qd mm C pratique...

le but de ce post est de réaliser un toString ie des fonctions permettant d'afficher les valeur de classes, comme en Java
de nimporte quelle classe héritant de CWnd

G déjà posé les bases, reste à compléter cette classe

C relativement simple, il suffit d'explorer les msdn et ça permet d'enrichir ses connaissances, dc si vs ne voulez que faire un petit ajout, po de pb....
ça sera intégré et petit à petit, cette classe grossira !

Magicalement !



actuellement géré :
[CWndType]
com=les types convertissables en CWnd => utilisation du toString de BWnd
com=pour les toString des élts graphiques...
nb=8
1=CButton
2=CComboBox
3=CSlider Ctrl
4=CStatusBar
5=CStatic
6=CTabCtrl
7=CToolTipC trl
8=CWnd


au fait, en même temps, ce post fait office de tutorial des CWnd & des classes en dérivant !

Source

  • voici la classe : toute simple
  • #if !defined(AFX_WNDBC_HPP__128C21B9_C4E9_4888_80E8_DDB8DAC2BFB2__INCLUDED_)
  • #define AFX_WNDBC_HPP__128C21B9_C4E9_4888_80E8_DDB8DAC2BFB2__INCLUDED_
  • #if _MSC_VER > 1000
  • #pragma once
  • #endif /* _MSC_VER> 1000 */
  • // wndbc.hpp : header file
  • //
  • /////////////////////////////////////////////////////////////////////////////
  • // CWndBC window
  • /// doc class : deb
  • class BWnd:public CWnd
  • {
  • // Construction
  • public:
  • BWnd();
  • // Attributes
  • public:
  • // Operations
  • public:
  • // Overrides
  • // ClassWizard generated virtual function overrides
  • //{{AFX_VIRTUAL(CWndBC)
  • //}}AFX_VIRTUAL
  • // Implementation
  • public:
  • CString toString();
  • virtual ~BWnd();
  • // Generated message map functions
  • protected:
  • //{{AFX_MSG(CWndBC)
  • // NOTE - the ClassWizard will add and remove member functions here.
  • //}}AFX_MSG
  • DECLARE_MESSAGE_MAP();
  • };
  • ______________________________________
  • et le code associé
  • /
  • #include "stdafx.h"
  • #include "wndbc.hpp"
  • #ifdef _DEBUG
  • #define new DEBUG_NEW
  • #undef THIS_FILE
  • static char THIS_FILE[]=__FILE__;
  • #endif /* _DEBUG */
  • /////////////////////////////////////////////////////////////////////////////
  • // BWnd
  • BWnd::BWnd()
  • {
  • /// ----------------------------------------------
  • /// ---------------- BWnd::BWnd() ----------------
  • /// ----------------------------------------------
  • /// ----- Objectif : Constructeur de la classe : BWnd
  • /// ----- PostCond : Toutes les variables de la classe doivent être instanciées avec une valeur par défaut ou contextuelle
  • /// ----- Etat : 1 (-1<0<1<2)
  • /// ----------------------------------------------
  • }
  • BWnd::~BWnd()
  • {
  • /// -----------------------------------------------
  • /// ---------------- BWnd::~BWnd() ----------------
  • /// -----------------------------------------------
  • /// ----- Objectif : Destructeur de la classe : BWnd
  • /// ----- PostCond : Toutes les variables de la classe sont détruites
  • /// ----- Etat : 1 (-1<0<1<2)
  • /// -----------------------------------------------
  • }
  • BEGIN_MESSAGE_MAP(BWnd, CWnd)
  • //{{AFX_MSG_MAP(BWnd)
  • // NOTE - the ClassWizard will add and remove mapping macros here.
  • //}}AFX_MSG_MAP
  • END_MESSAGE_MAP()
  • /////////////////////////////////////////////////////////////////////////////
  • // BWnd message handlers
  • CString BWnd::toString()
  • {
  • /// -------------------------------------------------------------
  • /// ---------------- BWnd::toString() -> BString ----------------
  • /// -------------------------------------------------------------
  • /// ----- Objectif : Retourner sous forme textuelle le contenu de la classe
  • /// ----- PreCond : Toutes les variables de ce conténaire doivent être instanciées et les conténaires de données qu'il comporte éventuellement doivent posséder une fonction toString.
  • /// ----- PostCond : /
  • /// ----- Etat : 1 (-1<0<1<2)
  • /// ----- MaJ 09/07/04 : CButton, CComboBox, CSliderCtrl
  • /// -------------------------------------------------------------
  • /// ----- retour (CString) : cf.obj
  • /// -------------------------------------------------------------
  • /// ----- Var Internes à la fonction (6) : listeOK ,max ,min ,rep ,style ,tmp
  • /// ----- Var In Globales (3) : CButton ,CComboBox ,CSliderCtrl
  • /// ----- Var In Globales Constantes (11) : BS_3STATE ,BS_AUTO3STATE ,BS_AUTOCHECKBOX ,BS_AUTORADIOBUTTON ,BS_CHECKBOX ,BS_DEFPUSHBUTTON ,BS_GROUPBOX ,BS_LEFTTEXT ,BS_OWNERDRAW ,BS_PUSHBUTTON ,BS_RADIOBUTTON
  • CString rep;
  • CString tmp;
  • rep+=" valeur : \"";
  • GetWindowText(tmp);
  • rep+=tmp;
  • rep+="\"";
  • if(!IsWindowEnabled())
  • rep+=", non-activable ";
  • //else
  • // rep+=", activable ";
  • if(!IsWindowVisible())
  • rep+=", invisible ";
  • //else
  • // rep+=", visible ";
  • ///algo : SI CButton
  • if(IsKindOf(RUNTIME_CLASS(CButton)))
  • {
  • rep+=", CButton , style : ";
  • unsigned int style=((CButton *) this)->GetButtonStyle();
  • switch(style)
  • {
  • case BS_AUTOCHECKBOX:
  • //Same as a check box, except that a check mark appears in the check box when the user selects the box; the check mark disappears the next time the user selects the box.
  • rep+="BS_AUTOCHECKBOX";
  • break;
  • case BS_AUTORADIOBUTTON:
  • // Same as a radio button, except that when the user selects it, the button automatically highlights itself and removes the selection from any other radio buttons with the same style in the same group.
  • rep+="BS_AUTORADIOBUTTON";
  • break;
  • case BS_AUTO3STATE:
  • // Same as a three-state check box, except that the box changes its state when the user selects it.
  • rep+="BS_AUTO3STATE";
  • break;
  • case BS_CHECKBOX:
  • // Creates a small square that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style).
  • rep+="BS_CHECKBOX";
  • break;
  • case BS_DEFPUSHBUTTON:
  • // Creates a button that has a heavy black border. The user can select this button by pressing the ENTER key. This style enables the user to quickly select the most likely option (the default option).
  • rep+="BS_DEFPUSHBUTTON";
  • break;
  • case BS_GROUPBOX:
  • // Creates a rectangle in which other buttons can be grouped. Any text associated with this style is displayed in the rectangle’s upper-left corner.
  • rep+="BS_GROUPBOX";
  • break;
  • case BS_LEFTTEXT:
  • // When combined with a radio-button or check-box style, the text appears on the left side of the radio button or check box.
  • rep+="BS_LEFTTEXT";
  • break;
  • case BS_OWNERDRAW:
  • // Creates an owner-drawn button. The framework calls the DrawItem member function when a visual aspect of the button has changed. This style must be set when using the CBitmapButton class.
  • rep+="BS_OWNERDRAW";
  • break;
  • case BS_PUSHBUTTON:
  • // Creates a pushbutton that posts a WM_COMMAND message to the owner window when the user selects the button.
  • rep+="BS_PUSHBUTTON";
  • break;
  • case BS_RADIOBUTTON:
  • // Creates a small circle that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style). Radio buttons are usually used in groups of related but mutually exclusive choices.
  • rep+="BS_RADIOBUTTON";
  • break;
  • case BS_3STATE:
  • // Same as a check box, except that the box can be dimmed as well as checked. The dimmed state typically is used to show that a check box has been disabled.
  • rep+="BS_3STATE";
  • break;
  • }
  • //rep+=" , ";
  • if(style==BS_AUTOCHECKBOX
  • ||style==BS_AUTORADIOBUTTON
  • ||style==BS_AUTO3STATE
  • ||style==BS_CHECKBOX
  • ||style==BS_RADIOBUTTON
  • ||style==BS_3STATE)
  • {
  • rep+=", Check : ";
  • rep+=((CButton *) this)->GetCheck()==TRUE;
  • }
  • }
  • else ///algo : SI CComboBox
  • if(IsKindOf(RUNTIME_CLASS(CComboBox)))
  • {
  • rep+=", CComboBox , Contenu : ";
  • ///algo : contenu
  • {
  • CString tmp,tmp2;
  • int nb,i;
  • nb=((CComboBox *) this)->GetCount();
  • tmp.Format("%d",nb); //sprintf(tmp,"%d",nb);
  • //// ini.setIniFile(nomVar,"nbr",FichierIniCible,tmp);
  • rep+='[';
  • for(i=0;i<nb;i++)
  • {
  • tmp.Format("%d",i);
  • //sprintf(tmp,"%d",i);
  • ((CComboBox *) this)->GetLBText(i,tmp2);
  • /// ini.setIniFile(nomVar,tmp,FichierIniCible,tmp2);
  • rep+=tmp2;rep+='\n';
  • }
  • rep+=']';
  • rep+='\n';
  • }
  • }
  • else
  • if(IsKindOf(RUNTIME_CLASS(CSliderCtrl)))
  • {
  • rep+=", CSliderCtrl, actu : ";
  • rep+=((CSliderCtrl*) this)->GetPos();
  • rep+=", bornes : [";
  • int min=0,max=0;
  • ((CSliderCtrl*) this)->GetRange(min,max);
  • rep+=min;
  • rep+=',';
  • rep+=max;
  • rep+=']';
  • min=0,max=0;
  • ((CSliderCtrl*) this)->GetSelection(min,max);
  • if(min!=0 || max!=0)
  • {
  • rep+=", sélection : [";
  • rep+=min;
  • rep+=',';
  • rep+=max;
  • rep+=']';
  • }
  • }
  • //else BVisuel::alerte("elt non détaillé");
  • return rep;
  • }
voici la classe : toute simple 


#if !defined(AFX_WNDBC_HPP__128C21B9_C4E9_4888_80E8_DDB8DAC2BFB2__INCLUDED_)
#define AFX_WNDBC_HPP__128C21B9_C4E9_4888_80E8_DDB8DAC2BFB2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif /* _MSC_VER> 1000 */

// wndbc.hpp : header file
//
/////////////////////////////////////////////////////////////////////////////
// CWndBC window
/// doc class : deb
class BWnd:public CWnd
{
	// Construction
public:
	BWnd();
	// Attributes
public:
	// Operations
public:
	// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CWndBC)
	//}}AFX_VIRTUAL
	// Implementation
public:
	CString toString();
	virtual ~BWnd();
	// Generated message map functions
protected:
	//{{AFX_MSG(CWndBC)
	// NOTE - the ClassWizard will add and remove member functions here.
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP();
};


______________________________________

et le code associé

/
#include "stdafx.h"
#include "wndbc.hpp"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE

static char THIS_FILE[]=__FILE__;
#endif /* _DEBUG */

/////////////////////////////////////////////////////////////////////////////
// BWnd



BWnd::BWnd()
{
/// ----------------------------------------------
/// ---------------- BWnd::BWnd() ----------------
/// ----------------------------------------------
/// -----  Objectif	: Constructeur de la classe : BWnd
/// -----  PostCond	: Toutes les variables de la classe doivent être instanciées avec une valeur par défaut ou contextuelle
/// -----  Etat		: 1			(-1<0<1<2)
/// ----------------------------------------------
}



BWnd::~BWnd()
{
/// -----------------------------------------------
/// ---------------- BWnd::~BWnd() ----------------
/// -----------------------------------------------
/// -----  Objectif	: Destructeur de la classe : BWnd
/// -----  PostCond	: Toutes les variables de la classe sont détruites
/// -----  Etat		: 1			(-1<0<1<2)
/// -----------------------------------------------
}

BEGIN_MESSAGE_MAP(BWnd, CWnd)
	//{{AFX_MSG_MAP(BWnd)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// BWnd message handlers

CString BWnd::toString()
{
/// -------------------------------------------------------------
/// ---------------- BWnd::toString() -> BString ----------------
/// -------------------------------------------------------------
/// -----  Objectif	: Retourner sous forme textuelle le contenu de la classe
/// -----  PreCond	: Toutes les variables de ce conténaire doivent être instanciées et les conténaires de données qu'il comporte éventuellement doivent posséder une fonction toString.
/// -----  PostCond	: /
/// -----  Etat		: 1			(-1<0<1<2)
/// -----  MaJ 09/07/04 : CButton, CComboBox, CSliderCtrl
/// -------------------------------------------------------------
/// -----  retour (CString)	: cf.obj
/// -------------------------------------------------------------
/// -----  Var Internes à la fonction (6)	: listeOK ,max ,min ,rep ,style ,tmp
/// -----  Var In  Globales (3)	: CButton ,CComboBox ,CSliderCtrl
/// -----  Var In  Globales Constantes (11)	: BS_3STATE ,BS_AUTO3STATE ,BS_AUTOCHECKBOX ,BS_AUTORADIOBUTTON ,BS_CHECKBOX ,BS_DEFPUSHBUTTON ,BS_GROUPBOX ,BS_LEFTTEXT ,BS_OWNERDRAW ,BS_PUSHBUTTON ,BS_RADIOBUTTON
	CString rep;
	CString tmp;
	rep+=" valeur : \"";
	GetWindowText(tmp);
	rep+=tmp;
	rep+="\"";
	if(!IsWindowEnabled())
		rep+=", non-activable ";
	//else
	//	rep+=", activable ";
	if(!IsWindowVisible())
		rep+=", invisible ";
	//else
	//	rep+=", visible ";
	///algo : SI CButton
	if(IsKindOf(RUNTIME_CLASS(CButton)))
	{
		rep+=", CButton , style : ";
		unsigned int style=((CButton *) this)->GetButtonStyle();
		switch(style)
		{
		case BS_AUTOCHECKBOX:
			//Same as a check box, except that a check mark appears in the check box when the user selects the box; the check mark disappears the next time the user selects the box.
			rep+="BS_AUTOCHECKBOX";
			break;
		case BS_AUTORADIOBUTTON:
			// Same as a radio button, except that when the user selects it, the button automatically highlights itself and removes the selection from any other radio buttons with the same style in the same group.
			rep+="BS_AUTORADIOBUTTON";
			break;
		case BS_AUTO3STATE:
			// Same as a three-state check box, except that the box changes its state when the user selects it.
			rep+="BS_AUTO3STATE";
			break;
		case BS_CHECKBOX:
			// Creates a small square that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style).
			rep+="BS_CHECKBOX";
			break;
		case BS_DEFPUSHBUTTON:
			// Creates a button that has a heavy black border. The user can select this button by pressing the ENTER key. This style enables the user to quickly select the most likely option (the default option).
			rep+="BS_DEFPUSHBUTTON";
			break;
		case BS_GROUPBOX:
			// Creates a rectangle in which other buttons can be grouped. Any text associated with this style is displayed in the rectangle’s upper-left corner.
			rep+="BS_GROUPBOX";
			break;
		case BS_LEFTTEXT:
			// When combined with a radio-button or check-box style, the text appears on the left side of the radio button or check box.
			rep+="BS_LEFTTEXT";
			break;
		case BS_OWNERDRAW:
			// Creates an owner-drawn button. The framework calls the DrawItem member function when a visual aspect of the button has changed. This style must be set when using the CBitmapButton class.
			rep+="BS_OWNERDRAW";
			break;
		case BS_PUSHBUTTON:
			// Creates a pushbutton that posts a WM_COMMAND message to the owner window when the user selects the button.
			rep+="BS_PUSHBUTTON";
			break;
		case BS_RADIOBUTTON:
			// Creates a small circle that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style). Radio buttons are usually used in groups of related but mutually exclusive choices.
			rep+="BS_RADIOBUTTON";
			break;
		case BS_3STATE:
			// Same as a check box, except that the box can be dimmed as well as checked. The dimmed state typically is used to show that a check box has been disabled.
			rep+="BS_3STATE";
			break;
		}
		//rep+=" , ";
		if(style==BS_AUTOCHECKBOX
		||style==BS_AUTORADIOBUTTON
		||style==BS_AUTO3STATE
		||style==BS_CHECKBOX
		||style==BS_RADIOBUTTON
		||style==BS_3STATE)
		{
			rep+=", Check : ";
			rep+=((CButton *) this)->GetCheck()==TRUE;
		}
	}
	else	///algo : SI CComboBox
		if(IsKindOf(RUNTIME_CLASS(CComboBox)))
		{
			rep+=", CComboBox , Contenu : ";
			///algo : contenu
			{
				CString tmp,tmp2;
				int nb,i;
				nb=((CComboBox *) this)->GetCount();
				tmp.Format("%d",nb);	//sprintf(tmp,"%d",nb);
				////	ini.setIniFile(nomVar,"nbr",FichierIniCible,tmp);
rep+='[';				
for(i=0;i<nb;i++)
				{
					tmp.Format("%d",i);
					//sprintf(tmp,"%d",i);
					((CComboBox *) this)->GetLBText(i,tmp2);
					///		ini.setIniFile(nomVar,tmp,FichierIniCible,tmp2);
					rep+=tmp2;rep+='\n';

				}
rep+=']';
rep+='\n';
							}
		}
		else
			if(IsKindOf(RUNTIME_CLASS(CSliderCtrl)))
			{
				rep+=", CSliderCtrl, actu : ";
				rep+=((CSliderCtrl*) this)->GetPos();
				rep+=", bornes : [";
				int min=0,max=0;
				((CSliderCtrl*) this)->GetRange(min,max);
				rep+=min;
				rep+=',';
				rep+=max;
				rep+=']';
				min=0,max=0;
				((CSliderCtrl*) this)->GetSelection(min,max);
				if(min!=0 || max!=0)
				{
					rep+=", sélection : [";
					rep+=min;
					rep+=',';
					rep+=max;
					rep+=']';
				}
			}
	//else	BVisuel::alerte("elt non détaillé");
	return rep;
}


 Conclusion

voilà faut faire évoluer ce toString

(PS: en fait, je l'ai po testé avec les CString mais mes BString, G fait ici les modif pr supprimer ttes mes Bxxx de ce post... normalement, ça devrait marcher...)

allé a vos claviers & bonne prog !


 Sources du même auteur

Source avec Zip [MFC] BCOMBOBOX & BLISTBOX
Source avec Zip Source avec une capture [ C++ WIN32 ] REMPLISSAGE DE FICHE - FENETRE DYNAMIQUE
Source avec Zip Source avec une capture WIN32 - MFC - VC6 - PLACE D'UNE APPLI : BARRE DES TACHES OU ...
Source avec Zip [WIN32] [VC6] BOITE À CHOIX MULTIPLE PAR BOUTONS
Source avec Zip TEMPLATE DE DIALOG/FENETRE (VC++)

 Sources de la même categorie

LISTER FICHIERS ET RÉPERTOIRES (MULTIPLATEFORME) par christophedlr
UTILISATION DES TYPELIST EN C++ par wyden
Source avec Zip Source avec une capture QCSSCOMPRESSOR par alphaone
AFFICHAGE D'UN TRIANGLE ISOCELE par nabche
Source avec Zip GESTION D'UNE BIBLOTHEQUE par leclerro19

Commentaires et avis

Commentaire de BruNews le 06/09/2004 22:33:01 administrateur CS

TOFILE pour bientot ?

Commentaire de magic_Nono le 07/09/2004 09:20:23

toFile :

ben aucun intéret, mais si tu y tiens et si tu sais faire une fonction template sans classe,

f=fopen("fic","wt");
fprintf(f,"%s",truc->toString());
fclose(f);

;)
++

Nono.

Commentaire de magic_Nono le 07/09/2004 09:20:58

ou la mm choses avec les '<<' et '>>'

lol

Commentaire de magic_Nono le 09/09/2004 10:57:19

repris d'une discution sur forum:

un prj génial serait d'avoir comme en Java des toSring pour tout
et en particulier pour les MFC et STL

il est toujours intéressant d'avoir le contenu exact des objets manipulés

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,217 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales