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 :

[Win32/ATL] Horizontal Scrolling pour une ListBox


Sujet :

MFC

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 11
    Points : 6
    Points
    6
    Par défaut [Win32/ATL] Horizontal Scrolling pour une ListBox
    Pour avoir une barre de defilement horizontale dans une listbox, il faut calculer la taille de la chaine de caractere la plus longue... en pixel soit-meme... (a croire que microsoft n'a pas eu le temps de finir l'api)

    Je n'arrive pas a faire ce calcul en utilisant GDI, et sans utiliser MFC :
    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
     
    //Use GetDC to retrieve the handle to the display context for the list box 
    //and store it in hDCListBox:
    	HDC hDCListBox = GetDC(hwnd);
    //Send the list box a WM_GETFONT message to retrieve the handle to the font
    //that the list box is using, and store this handle in hFontNew:
    	HFONT hFontNew = (HFONT) ::SendMessage(hwnd, WM_GETFONT, NULL, NULL);
    //Use SelectObject to select the font into the display context. Retain the
    //return value from the SelectObject call in hFontOld:
    	HFONT hFontOld = (HFONT) SelectObject(hDCListBox, hFontNew);
    //Call GetTextMetrics to get additional information about the font being used:
    	TEXTMETRICS tm;
    	GetTextMetrics(hDCListBox , (LPTEXTMETRIC)&tm);
    //For each string, the value of the extent to be used is calculated as follows:
    	DWORD dwExtent = GetTextExtent(hDCListBox, lpszText, strlen(lpszText))
    					+ tm.tmAveCharWidth;
    //After all the extents have been calculated, select the old font back into 
    //hDCListBox and then release it:
    	SelectObject(hDCListBox, hFontOld);
          ReleaseDC(hwnd, hDCListBox);												
    	  return dwExtent;
    J'ai trouve ce bout de code (sur MSDN) mais je n'arrive pas a appeler la fonction getDC(hwnd) de GDI, le compilateur propose uniquement la fonction getDC() de ATL, je ne vois pas ce qu'il faut rajouter pour utiliser celle de GDI, utiliser TEXTMETRICS...

    Merci pour votre aide

  2. #2
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 11
    Points : 6
    Points
    6
    Par défaut
    Bon j'ai pas du tout comprendre dans les namespace des applis windows. Pour getDC(hwnd), il suffit d'ecrire :: getDC(hwnd) ce qui fait reference au namespace System??? Il a vraiment pas de nom explicite ?
    Sinon pour TEXTMETRICS, bah au fait c'est TEXTMETRIC sans S, voila, je devrais plus prendre les exemples de code sur MSDN pour argent comptant (http://support.microsoft.com/kb/q66370/), 10 lignes de code, avec au moins 2 erreurs...
    Et il faut remplacer GetTextExtent par ::GetTextExtentPoint32, avec les bons param ca roule...
    Je laisse ce sujet au cas ou ca puisse servir a quelqu'un, on sait jamais...

    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
     
    long getTextLength(HWND hwndListBox, LPCTSTR lpszText)
    {
    	//Use GetDC to retrieve the handle to the display context for the list box 
    	//and store it in hDCListBox:
    	HDC hDCListBox = ::GetDC(hwndListBox);
    	//Send the list box a WM_GETFONT message to retrieve the handle to the font
    	//that the list box is using, and store this handle in hFontNew:
    	HFONT hFontNew = (HFONT) ::SendMessage(hwndListBox, WM_GETFONT, NULL, NULL);
    	//Use SelectObject to select the font into the display context. Retain the
    	//return value from the SelectObject call in hFontOld:
    	HFONT hFontOld = (HFONT) SelectObject(hDCListBox, hFontNew);
    	//Call GetTextMetrics to get additional information about the font being used:
    	TEXTMETRIC tm;
    	GetTextMetrics(hDCListBox, (LPTEXTMETRIC)&tm); 
    	//For each string, the value of the extent to be used is calculated as follows:
    	SIZE sizeRect;
    	::GetTextExtentPoint32(hDCListBox, lpszText, strlen(lpszText), &sizeRect);
    	sizeRect.cx += tm.tmAveCharWidth;
    	//After all the extents have been calculated, select the old font back into 
    	//hDCListBox and then release it:
    	SelectObject(hDCListBox, hFontOld);
    	::ReleaseDC(hwndListBox, hDCListBox);												
     
    	  return sizeRect.cx;
    }

Discussions similaires

  1. Nombre d'enregistrements maximums pour une listbox
    Par Zenerox dans le forum VBA Access
    Réponses: 2
    Dernier message: 09/12/2008, 17h17
  2. couleur des polices lignes par lignes pour une Listbox
    Par gwenc_hlan dans le forum Tkinter
    Réponses: 4
    Dernier message: 27/03/2008, 10h26
  3. [WPF]Scroll dans une ListBox
    Par guitoux1 dans le forum Windows Presentation Foundation
    Réponses: 7
    Dernier message: 12/06/2007, 10h43
  4. Scrolling sur une ListBox
    Par dede92 dans le forum Windows
    Réponses: 2
    Dernier message: 02/06/2006, 18h56
  5. Soucis pour une Listbox
    Par ]3l@c]{bUll dans le forum Composants VCL
    Réponses: 23
    Dernier message: 13/07/2005, 14h42

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