Accueil > > > TOSTRING DES MFC
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=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
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|