|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
TOSTRING DES MFC
Information sur la source
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=CSliderCtrl 4=CStatusBar 5=CStatic 6=CTabCtrl 7=CToolTipCtrl 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
Sources de la même categorie
Commentaires et avis
|
|