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

C++ Discussion :

fonction static dans une class


Sujet :

C++

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Février 2005
    Messages
    80
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 80
    Points : 63
    Points
    63
    Par défaut fonction static dans une class
    Bonjour,
    pour crée un client socket avec une intreface windows j'ai crée une class WindowClient dont voici le code :
    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
     
    #include <windows.h>
     
    #define WCICON 1
    #define WCMENU 2
     
    class WindowClient
    {
    	public :
    		bool CreateWindowClient(HWND,int);
    		LRESULT CALLBACK WindowClientProc(HWND ,UINT ,WPARAM ,LPARAM );
    	private :
    		HWND hdWindow,hdEditSend,hdEditRecv,hdButton;
    		HINSTANCE hInstance;
    	protected :
    };
    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
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
     
    #include "WindowClient.h"
    #include <iostream>
     
    using namespace std;
     
    bool WindowClient::CreateWindowClient(HWND WinPar,int visible)
    {
    	hInstance =(HINSTANCE) GetWindowLong(WinPar,GWL_HINSTANCE);
     
    	WNDCLASSEX wmclCClient;
    	wmclCClient.cbSize = sizeof(WNDCLASSEX);
    	wmclCClient.style = 0;
    	wmclCClient.lpfnWndProc=WindowClientProc;
    	wmclCClient.cbClsExtra = 0;
    	wmclCClient.cbWndExtra = 0;
    	wmclCClient.hInstance= hInstance;
    	wmclCClient.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(WCICON) );
    	wmclCClient.hCursor = NULL;
    	wmclCClient.hbrBackground = GetSysColorBrush(15) ;
    	wmclCClient.lpszMenuName = MAKEINTRESOURCE(WCMENU);
    	wmclCClient.lpszClassName = "WindowClientClass";
    	wmclCClient.hIconSm = NULL;
    	if(!RegisterClassEx(&wmclCClient))
    		return 0;
     
    	hdWindow = CreateWindowEx(0,
    				"WindowClientClass",
    				"Client",
    				WS_OVERLAPPEDWINDOW,
    				GetSystemMetrics(SM_CXSCREEN)/4,
    				GetSystemMetrics(SM_CYSCREEN)/4,
    				GetSystemMetrics(SM_CXSCREEN)/2,
    				GetSystemMetrics(SM_CYSCREEN)/2,
    				WinPar,
    				NULL,
    				hInstance, 
    				NULL);
    	if(hdWindow==NULL)
    		return 0;
    	ShowWindow(hdWindow, visible);
    	return 1;
    }
     
    WNDPROC OrigEditSendProc;
    LRESULT CALLBACK EditSendProc (HWND , UINT , WPARAM , LPARAM );
     
    LRESULT CALLBACK WindowClient::WindowClientProc(HWND hClient, UINT msgClient, WPARAM wParam, LPARAM lParam)
    {
    	switch(msgClient)
    	{
    		case WM_CREATE :
    		{
    			NOTIFYICONDATA nid;
    			RECT rcClient;
    			GetClientRect (hClient, &rcClient);
    			hdEditRecv = CreateWindowEx(0,
    				"EDIT",
    				"",
    				WS_VISIBLE|WS_CHILD|ES_MULTILINE|WS_VSCROLL,
    				rcClient.left+10,
    				rcClient.top+10,
    				rcClient.right-20,
    				rcClient.bottom-110,
    				hClient,
    				NULL,
    				hInstance,
    				NULL);
    			if(hdEditRecv==NULL)
    				return 0;
    			hdEditSend = CreateWindowEx(0,
    				"EDIT",
    				"",
    				WS_VISIBLE|WS_CHILD|ES_MULTILINE|WS_VSCROLL,
    				rcClient.left+10,
    				rcClient.bottom-60,
    				rcClient.right-80,
    				50,
    				hClient,
    				NULL,
    				hInstance,
    				NULL);
    			if(hdEditSend==NULL)
    				return 0;
    			OrigEditSendProc = (WNDPROC)SetWindowLong(hdEditSend,GWL_WNDPROC, (LONG)EditSendProc);
    			hdButton = CreateWindowEx(0,
    				"BUTTON",
    				"OK",
    				WS_VISIBLE|WS_CHILD,
    				rcClient.right-60,
    				rcClient.bottom-60,
    				50,
    				50,
    				hClient,
    				NULL,
    				hInstance,
    				NULL);
    			if(hdButton==NULL)
    				return 0;
    			EnableWindow(hdButton,false);
    			break;
    		}
    		case WM_COMMAND :
    			switch(HIWORD(wParam))
    			{
    				case BN_CLICKED :
    				{	//(HWND)lParam HANDLE TO THE BUTTON
    					char* buffer=new char[GetWindowTextLength(hdEditSend)+1];
    					GetWindowText(hdEditSend,buffer,GetWindowTextLength(hdEditSend)+1);
    					SetWindowText(hdEditRecv,buffer);
    					delete[] buffer;
    					while(HIWORD(SendMessage(hdEditRecv,EM_SCROLL,SB_PAGEDOWN,0)));
    				}
    					break;
    				case EN_CHANGE :
    					if (hdEditSend==(HWND)lParam)
    						if (GetWindowTextLength(hdEditSend)>0)
    							EnableWindow(hdButton,true);
    						else
    							EnableWindow(hdButton,false);
    					break;
    			}
    		case WM_CHAR :
    			if(wParam == 27)
    				SendMessage(hClient,WM_CLOSE,0,0);
    			break;
    		case WM_SIZE :
    		{
    			RECT rcClient;
    			GetClientRect (hClient, &rcClient);
    			MoveWindow(hdEditRecv,rcClient.left+10,
    								rcClient.top+10,
    								rcClient.right-rcClient.left-20,
    								rcClient.bottom-rcClient.top-110,true);
    			MoveWindow(hdEditSend,rcClient.left+10,
    								rcClient.bottom-60,
    								rcClient.right-rcClient.left-80,
    								50,true);
    			MoveWindow(hdButton,rcClient.right-60,
    								rcClient.bottom-60,
    								50,
    								50,true);
    			break;
    		}
    		case WM_CLOSE :
    			break;
    		case WM_DESTROY :
    			break;
    		default: // Messages non gérés
    			return DefWindowProc(hClient, msgClient, wParam, lParam);
    	}
    }
     
    LRESULT CALLBACK EditSendProc(HWND hEdit, UINT msgEdit, WPARAM wParam, LPARAM lParam)
    {
    	if (msgEdit == WM_CHAR)
    	{
    		cout<<wParam<<endl;
    	}
    	return CallWindowProc(OrigEditSendProc, hEdit, msgEdit, wParam, lParam);
    }
    a la compilation avec Dev-C++ j'obtien l'erreur suivante :
    argument of type `LRESULT (WindowClient:(HWND__*, UINT, WPARAM, LPARAM)' does not match `LRESULT (*)(HWND__*, UINT, WPARAM, LPARAM)'

    cependant cela devrait marché (enfin je veux)

    ou quand le met
    static LRESULT CALLBACK WindowClientProc(HWND ,UINT ,WPARAM ,LPARAM );

    je ne peux pas utiliser les diffèrentes variable HWND et HINSTANCE
    je prefert mettre static car c'est un callback et que c'est pas utile de le recée a chaque fois !

    Si quelqu'un peu m'aider j'en serai ravi
    Merci

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 379
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

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

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 379
    Points : 41 573
    Points
    41 573
    Par défaut
    1°) Tu peux stocker un pointeur dans une zone mémoire faite pour cela avec SetWindowLong()/GetWindowLong() : L'index disponible pour toi est GWL_USERDATA

    2°) Tu peux passer un pointeur en paramètre à CreateWindow(), lequel peut être reçu dans le WM_CREATE

    3°) En utilisant 1 et 2, tu passe this en paramètre de CreateWindow, tu le récupères dans WM_CREATE et tu le mémorises avec SetWindowLong() (le tout, dans une WindowProc statique).
    Pour chaque message, tu récupères this avec GetWindowLong(), et s'il n'est pas NULL, tu appelles une WindowProc non-statique dans laquelle tu as accès à toutes tes variables
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Février 2005
    Messages
    80
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 80
    Points : 63
    Points
    63
    Par défaut
    Merci,
    j'ai mis un peu de temps a comprendre ce que tu me disais mais j'ai compris le principe. Sauf que les HWND ne sont pas fix, pour les hdEdit et hdButton ainsi que pour hInstance qui elle varie mais reprend une bonne valeur !!!

    hdEdit ->1<-0x2703f4
    hdEdit ->2<-0x19035a
    hdButton ->3<-0x2103a6
    ->1<-0x3903de
    ->2<-0x5b091af6
    ->3<-0x47
    ->1<-0x77d18734
    ->2<-0x22f45c
    ->3<-0x3903de
    ->1<-0x77d18734
    ->2<-0x22f45c
    ->3<-0x3903de

    La fonction Proc est en fonction firend et il y a un pointeur WindowClient.
    je ne vois pas trop ce qui cloche car les valeurs ne sont pas réatribué ( ...= ..)



    c'est quoi le diffèrence entre GWL_USERDATA et DWL_USER ?

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Février 2005
    Messages
    80
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 80
    Points : 63
    Points
    63
    Par défaut
    J'ai essayer de comprendre ce qui se passe et au bout de nombreux teste je m'apercois que je n'arrive pas a récupéré l'adresse de l'objet passé dans le lparam de createwindow(...) j'ai essayer avec un int et in adress in valeur ont été récupèrer. Y a -t-il une manique a faire pour le récupèrer ?

    ( int ou int * ou int & )lParam

    le test avec l'int pour faitre un parallèle avec WindowClient.

    une aide serai bienvenue !

Discussions similaires

  1. Une fonction SessionState dans une classe?
    Par CWagon dans le forum ASP.NET
    Réponses: 7
    Dernier message: 24/08/2007, 21h00
  2. Réponses: 2
    Dernier message: 22/12/2006, 11h35
  3. Réponses: 3
    Dernier message: 14/12/2006, 10h09
  4. variable Static dans une classe
    Par cubitus91 dans le forum Delphi
    Réponses: 3
    Dernier message: 29/11/2006, 00h07
  5. Fonction callback dans une classe
    Par julian_ross dans le forum MFC
    Réponses: 8
    Dernier message: 02/03/2004, 11h42

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