IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MFC Discussion :

CMDIChildWnd template


Sujet :

MFC

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2003
    Messages
    224
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2003
    Messages : 224
    Par défaut CMDIChildWnd template
    Bonjour je cherche à créer un template pour MDIChildWnd:

    le.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     
    #pragma once
     
    template <typename T>
    class DefaultMDIChild : public CMDIChildWnd
    {
    	DECLARE_DYNCREATE(DefaultMDIChild<T>)
     
    protected:
    	//! @brief Constructeur protégé utilisé par la création dynamique
    	DefaultMDIChild();
    	//! @brief Destructeur
    	virtual ~DefaultMDIChild();
    	virtual void PostNcDestroy();
    	DECLARE_MESSAGE_MAP()
     
    public:
    	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    	//! @brief Fonction de recuperation du pointeur de la form
    	inline T* GetForm() { return form; }
     
    private:
    	T* form;	
    };
    le .cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
     
    #include "stdafx.h"
    #include "DefaultMDIChild.h"
     
     
    // CFrmDeal
     
    IMPLEMENT_DYNCREATE(DefaultMDIChild<T>, CMDIChildWnd)
     
    template <typename T>
    DefaultMDIChild<T>::DefaultMDIChild()
    {
     
    }
     
    template <typename T>
    DefaultMDIChild<T>::~DefaultMDIChild()
    {
    }
     
    BEGIN_MESSAGE_MAP(DefaultMDIChild<T>, CMDIChildWnd)
    	ON_WM_CREATE()
    END_MESSAGE_MAP()
     
     
    // Gestionnaires de messages de CFrmDeal
     
    template <typename T>
    int DefaultMDIChild<T>::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
    		return -1;
    	form = new T();
    	if(!form->Create(NULL,_T("Deal"), AFX_WS_DEFAULT_VIEW,CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST, NULL))	
    	{
    		TRACE0("Impossible de créer la fenêtre d'affichage\n");
    		return -1;
    	}
     
    	return 0;
    }
     
    template <typename T>
    void DefaultMDIChild<T>::PostNcDestroy() {
        CMDIChildWnd::PostNcDestroy();
    }
    et evidement j'ai plein d erreur à cause des MACRO MFC:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    IMPLEMENT_DYNCREATE(DefaultMDIChild<T>, CMDIChildWnd)
     
    BEGIN_MESSAGE_MAP(DefaultMDIChild<T>, CMDIChildWnd)
    	ON_WM_CREATE()
    END_MESSAGE_MAP()
     
    	DECLARE_DYNCREATE(DefaultMDIChild<T>)
    car elle n'aime pas trop le concept de template

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Error	3	error C2065: 'T' : undeclared identifier	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	11	error C2143: syntax error : missing ';' before '{'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	1	error C2143: syntax error : missing ';' before '<'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.h	6
    Error	10	error C2143: syntax error : missing ';' before '<'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	2	error C2238: unexpected token(s) preceding ';'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.h	6
    Error	12	error C2447: '{' : missing function header (old-style formal list?)	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	7	error C2509: '_GetBaseClass' : member function not declared in 'DefaultMDIChild'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	5	error C2509: 'CreateObject' : member function not declared in 'DefaultMDIChild'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	18	error C2509: 'GetMessageMap' : member function not declared in 'DefaultMDIChild'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	21
    Error	16	error C2509: 'GetRuntimeClass' : member function not declared in 'DefaultMDIChild'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	14	error C2509: 'GetThisClass' : member function not declared in 'DefaultMDIChild'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	20	error C2509: 'GetThisMessageMap' : member function not declared in 'DefaultMDIChild'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	21
    Error	4	error C2955: 'DefaultMDIChild' : use of class template requires template argument list	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	6	error C2955: 'DefaultMDIChild' : use of class template requires template argument list	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	8	error C2955: 'DefaultMDIChild' : use of class template requires template argument list	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	13	error C2955: 'DefaultMDIChild' : use of class template requires template argument list	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	15	error C2955: 'DefaultMDIChild' : use of class template requires template argument list	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Error	17	error C2955: 'DefaultMDIChild' : use of class template requires template argument list	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	21
    Error	19	error C2955: 'DefaultMDIChild' : use of class template requires template argument list	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	21
    Warning	9	warning C4356: 'DefaultMDIChild<T>::classDefaultMDIChild' : static data member cannot be initialized via derived class	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\defaultmdichild.cpp	8
    Première question, est ce possible de faire des templates avec MFC?
    Si oui, comment?

  2. #2
    Membre chevronné
    Avatar de PetitPapaNoël
    Développeur informatique
    Inscrit en
    Septembre 2006
    Messages
    559
    Détails du profil
    Informations personnelles :
    Âge : 49

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 559
    Par défaut
    Bonjour,

    La réponse est oui : on peut créer une classe modèle (template) comme tu tentes de le faire. Par contre, il faut savoir qu'avec une classe modèle, tout le code doit se trouver dans le .h (pas de .cpp).

    Ensuite, le bloc BEGIN_MESSAGE_MAP doit être transformé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    BEGIN_TEMPLATE_MESSAGE_MAP(DefaultMDIChild, CMDIChildWnd, CMDIChildWnd)
    	ON_WM_CREATE()
    END_MESSAGE_MAP()
    De plus, il doit se trouver à la fin du .h, en dehors de la déclaration de la classe.

    J'ai déjà créé plusieurs classes modèles de boîtes de dialogue comme ça, et ça marche bien. Par contre, avec le DECLARE_DYNCREATE, je ne sais pas s'il y a d'autres subtilités...

  3. #3
    Rédacteur
    Avatar de farscape
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2003
    Messages
    9 055
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2003
    Messages : 9 055
    Par défaut
    salut,
    la réponse dans ce post :
    http://www.developpez.net/forums/sho...0&postcount=13
    note : lis les messages précédents pour comprendre le contexte.
    la solution donnée fonctionne pour vc6 et vc2005..

  4. #4
    Membre chevronné
    Avatar de PetitPapaNoël
    Développeur informatique
    Inscrit en
    Septembre 2006
    Messages
    559
    Détails du profil
    Informations personnelles :
    Âge : 49

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 559
    Par défaut
    Dans la version 2005 la macro BEGIN_TEMPLATE_MESSAGE_MAP existe déjà, dans afxwin.h.

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2003
    Messages
    224
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2003
    Messages : 224
    Par défaut
    merci je vais etudier tout cela.

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2003
    Messages
    224
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2003
    Messages : 224
    Par défaut
    alors j'ai moins d'erreur mais c 'est toujours pas la fete

    j'ai decouvert la MACRO IMPLEMENT_DYNCREATE_T pour les templates mais j'ai toujours des pbs:

    DefaultMDIChild.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    #pragma once
     
     
    #ifndef BEGIN_TEMPLATE_MESSAGE_MAP
    #ifdef _AFXDLL
    #define BEGIN_TEMPLATE_MESSAGE_MAP(TemplArg, theClass, baseClass) \
       template <class type_name> \
          const AFX_MSGMAP* PASCAL theClass < type_name >::_GetBaseMessageMap() \
             { return &baseClass::messageMap; } \
       template <class type_name> \
          const AFX_MSGMAP* theClass < type_name >::GetMessageMap() const \
             { return &theClass < type_name >::messageMap; } \
       template <class type_name> \
          AFX_COMDAT AFX_DATADEF const AFX_MSGMAP theClass < type_name >::messageMap = \
          { &theClass < type_name >::_GetBaseMessageMap, &theClass < type_name >::_messageEntries[0] }; \
       template <class type_name> \
          AFX_COMDAT const AFX_MSGMAP_ENTRY theClass < type_name >::_messageEntries[] = \
          { \
     
    #else
    #define BEGIN_TEMPLATE_MESSAGE_MAP(theClass, type_name, baseClass) \
       template <class type_name> \
          const AFX_MSGMAP* theClass < type_name >::GetMessageMap() const \
             { return &theClass < type_name >::messageMap; } \
       template <class type_name> \
          AFX_COMDAT AFX_DATADEF const AFX_MSGMAP theClass < type_name >::messageMap = \
          { &baseClass::messageMap, &theClass < type_name >::_messageEntries[0] }; \
       template <class type_name> \
          AFX_COMDAT const AFX_MSGMAP_ENTRY theClass < type_name >::_messageEntries[] = \
          { \
     
    #endif 
    #endif
     
    template <typename T>
    class DefaultMDIChild : public CMDIChildWnd
    {
    	DECLARE_DYNCREATE(DefaultMDIChild)
     
    protected:
    	//! @brief Constructeur protégé utilisé par la création dynamique
    	DefaultMDIChild();
    	//! @brief Destructeur
    	virtual ~DefaultMDIChild();
    	virtual void PostNcDestroy();
    	DECLARE_MESSAGE_MAP()
     
    public:
    	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    	//! @brief Fonction de recuperation du pointeur de la form
    	inline T* GetForm() { return form; }
     
    protected:
    	T* form;	
    };
     
    template <typename T>
    DefaultMDIChild<T>::DefaultMDIChild()
    {
     
    }
     
    template <typename T>
    DefaultMDIChild<T>::~DefaultMDIChild()
    {
    }
     
    BEGIN_TEMPLATE_MESSAGE_MAP(DefaultMDIChild, CMDIChildWnd, CMDIChildWnd)
    	ON_WM_CREATE()
    END_MESSAGE_MAP()
     
     
    template <typename T>
    int DefaultMDIChild<T>::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
    		return -1;
    	form = new T();
    	if(!form->Create(NULL,_T("Deal"), AFX_WS_DEFAULT_VIEW,CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST, NULL))	
    	{
    		TRACE0("Impossible de créer la fenêtre d'affichage\n");
    		return -1;
    	}
     
    	return 0;
    }
     
    template <typename T>
    void DefaultMDIChild<T>::PostNcDestroy() {
        CMDIChildWnd::PostNcDestroy();
    }
    Ma Form que je veux mettre dedans:
    #include "FormCoherenceControl.h"
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #pragma once
     
    #include "formviewex.h"
     
    // FormCoherenceControl dialog
     
    class FormCoherenceControl : public CFormViewEx
    {
    	DECLARE_DYNAMIC(FormCoherenceControl)
     
    public:
    	FormCoherenceControl(CWnd* pParent = NULL);   // standard constructor
    	virtual ~FormCoherenceControl();
        virtual void OnInitialUpdate();
     
    // Dialog Data
    	enum { IDD = IDD_FORMCoherenceControl };
     
    protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
     
    	DECLARE_MESSAGE_MAP()
    };
    le .cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    // FormCoherenceControl.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "FXTops.h"
    #include "FormCoherenceControl.h"
    #include "DefaultMDIChild.h"
    
    // FormCoherenceControl dialog
    
    IMPLEMENT_DYNCREATE_T(DefaultMDIChild, FormCoherenceControl, CMDIChildWnd)
    
    IMPLEMENT_DYNAMIC(FormCoherenceControl, CFormViewEx)
    
    FormCoherenceControl::FormCoherenceControl(CWnd* pParent /*=NULL*/)
    	: CFormViewEx(FormCoherenceControl::IDD)
    {
    
    }
    
    FormCoherenceControl::~FormCoherenceControl()
    {
    }
    
    void FormCoherenceControl::DoDataExchange(CDataExchange* pDX)
    {
    	CFormViewEx::DoDataExchange(pDX);
    }
    
    
    BEGIN_MESSAGE_MAP(FormCoherenceControl, CFormViewEx)
    END_MESSAGE_MAP()
    
    
    void FormCoherenceControl::OnInitialUpdate()
    {
    	CFormViewEx::OnInitialUpdate();
    }
    j'ai mis le
    IMPLEMENT_DYNCREATE_T(DefaultMDIChild, FormCoherenceControl, CMDIChildWnd) ici car je crois que le deuxieme argument de cette macro doit etre justement mon template.

    voici les erreurs:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Error	3	error C2039: 'GetThisClass' : is not a member of '`global namespace''	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\formcoherencecontrol.cpp	11
    Error	1	error C2275: 'CMDIChildWnd' : illegal use of this type as an expression	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\formcoherencecontrol.cpp	11
    Error	2	error C2275: 'CMDIChildWnd' : illegal use of this type as an expression	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\formcoherencecontrol.cpp	11
    Error	5	error C2509: 'GetThisClass' : member function not declared in 'DefaultMDIChild'	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\formcoherencecontrol.cpp	11
    Error	4	error C2955: 'DefaultMDIChild' : use of class template requires template argument list	c:\cc_fxtops_maint_evol\gr\fxtops\cpp\fxtops\formcoherencecontrol.cpp	11
    et ensuite je ne sais pas comment utiliser tout ca,
    comme ceci d apres ce que j'ai compris:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
        static DefaultMDIChild<FormCoherenceControl>* form = NULL;
     
        if(form!=NULL && ::IsWindow(form->GetSafeHwnd()) && false == form->IsKindOf(RUNTIME_CLASS(DefaultMDIChild<FormCoherenceControl>)))
        {
            form = NULL;
        }
     
        if(form==NULL || false == ::IsWindow(form->GetSafeHwnd()))
        {
            form = static_cast<DefaultMDIChild<FormCoherenceControl>*>(CreateNewChild(RUNTIME_CLASS(DefaultMDIChild<FormCoherenceControl>), IDR_MAINFRAME));
        }
     
        m_pFrmDeal->ShowWindow(SW_SHOWMAXIMIZED);

Discussions similaires

  1. [Templates] Quel système utilisez-vous ? Pourquoi ?
    Par narmataru dans le forum Bibliothèques et frameworks
    Réponses: 270
    Dernier message: 26/03/2011, 00h15
  2. Template XHTML
    Par Sylvain James dans le forum XSL/XSLT/XPATH
    Réponses: 14
    Dernier message: 16/06/2003, 21h45
  3. appliquer plusieurs templates
    Par Manu_Just dans le forum XSL/XSLT/XPATH
    Réponses: 7
    Dernier message: 04/04/2003, 16h26
  4. template match="node() mais pas text()"
    Par Manu_Just dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 26/03/2003, 10h52
  5. [XSLT] template
    Par demo dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 09/09/2002, 11h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo