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

Windows Discussion :

[C][API Windows] changt de couleur


Sujet :

Windows

  1. #1
    Membre à l'essai
    Inscrit en
    Août 2006
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 31
    Points : 12
    Points
    12
    Par défaut [C][API Windows] changt de couleur
    Bonjour

    Je voudrais savoir comment changer, via une boite de dialogue commune, la couleur de dessins et de textes créés avec WM_PAINT dans une fenêtre.

    Merci d'avance

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2005
    Messages : 72
    Points : 77
    Points
    77
    Par défaut
    Salut,

    Si j'ai bien compris ce que tu veux, tu dois mémoriser une variable de type COLORREF puis l'utiliser lorsque tu traites le message WM_PAINT avec des fonctions comme SetTextColor(), SetBkColor(), SetDCPenColor(), etc.

  3. #3
    Membre à l'essai
    Inscrit en
    Août 2006
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 31
    Points : 12
    Points
    12
    Par défaut
    Salut

    Oui, c'est bien ça. Mais j'ai en plus un souci avec SetDCPenColor(): j'ai à la compilation "[Link error] undefined reference to 'SetDCPenColor'".

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2005
    Messages : 72
    Points : 77
    Points
    77
    Par défaut
    SetDCPenColor() est une API disponible à partir de Windows 2000.
    Assures-toi que tu lies bien la librairie GDI32.LIB et que tu as ajouté #define _WIN32_WINNT 0x0500 avant l'inclusion de <windows.h>.

  5. #5
    Membre à l'essai
    Inscrit en
    Août 2006
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 31
    Points : 12
    Points
    12
    Par défaut
    salut

    En rajoutant #define _WIN32_WINNT 0x0500, SetDCPenColor() ne me pose plus de problèmes. Par contre, la couleur n'est changé que si la fenêtre est redessinée. J'ai un autre problème, cette fois-ci pour changer la fonte d'un texte, ça ne marche pas du tout.

  6. #6
    Membre à l'essai
    Inscrit en
    Août 2006
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 31
    Points : 12
    Points
    12
    Par défaut
    salut

    J'ai réussi à tégler le problème du changement de couleur. Il suffisait de rajouter la ligne InvalidateRect(hwnd, NULL, TRUE). Par contre, pour changer la fonte, j'ai toujours des soucis.

    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
    else if (LOWORD(wParam) == IDG_FONT)
                    {
                        CHOOSEFONT cg;
                        ZeroMemory(&cg, sizeof(CHOOSEFONT));
                        cg.lStructSize = sizeof (CHOOSEFONT);
                        cg.hwndOwner = hGraph;
                        cg.lpLogFont = &lfg;
                        cg.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
    
                        if (ChooseFont(&cg))
                        {
                                    DeleteObject(NewFont);
                                    NewFont = CreateFontIndirect(&lfg);
                                    SendMessage(hwnd,WM_PAINT,(UINT)NewFont,TRUE);
                                    InvalidateRect(hwnd,NULL,TRUE);
                                    
                        }
                    }
    visiblement, avec InvalidateRect(), ça ne marche pas.

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2005
    Messages : 72
    Points : 77
    Points
    77
    Par défaut
    Pour la font as-tu essayé SetTextColor() au lieu de SetDCPenColor() ? Je crois que la première fonction est plus adaptée...

  8. #8
    Membre à l'essai
    Inscrit en
    Août 2006
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 31
    Points : 12
    Points
    12
    Par défaut
    salut
    Pour la font, j'utilise bien SetTextColor et ça change bien la couleur. Mais pour changer la police, le style ou la taille, ça ne marche pas.

    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
     PAINTSTRUCT pg;
        HDC hdg;
        static BOOL GraphNotChg = TRUE;
        static COLORREF GraphColor;
        LOGFONT lfg;
        HPEN hpNew, hpOld;
        HFONT NewFont, OldFont;
        int m = 0;
        char *szBuffer1;
        szBuffer1 = (char*)malloc(256*sizeof(char));
    
        ZeroMemory(&lfg, sizeof(LOGFONT));
        lstrcpy(lfg.lfFaceName, "Times New Roman");
        lfg.lfHeight = 20;
        lfg.lfItalic = TRUE;
        lfg.lfUnderline = FALSE;
        lfg.lfWeight = FW_NORMAL;
                
        NewFont = CreateFontIndirect(&lfg);
         
        switch(uMsg)
        {
            case WM_CREATE:
                GraphColor = RGB(0,0,0);
    
            case WM_PAINT:
            {
    
                 char *buffer;
                 buffer = (char*)malloc(256*sizeof(char));
    
                 hdg = BeginPaint(hGraph, &pg);
                 SetBkMode(hdg, TRANSPARENT);
    
                 OldFont = SelectObject(hdg,NewFont);
    
                 SetTextColor(hdg, GraphColor);
                 sprintf(buffer, "p1(x) = %s", afficher(&p[0], szBuffer1));
                 TextOut(hdg, 5, 5, buffer, lstrlen(buffer));
                 free(buffer);
    
                 SelectObject(hdg,OldFont);
    
                 EndPaint(hGraph, &pg);
    
                 return 0L;
            }
    
            case WM_COMMAND:
            {
                    if (LOWORD(wParam) == IDG_FONT)
                    {
                        CHOOSEFONT cg;
                        ZeroMemory(&cg, sizeof(CHOOSEFONT));
                        cg.lStructSize = sizeof (CHOOSEFONT);
                        cg.hwndOwner = hGraph;
                        cg.lpLogFont = &lfg;
                        cg.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
    
                        if (ChooseFont(&cg))
                        {
                                    
                                    DeleteObject(NewFont);
                                    NewFont = CreateFontIndirect(&lfg);
                                    SendMessage(hGraph,WM_PAINT,(UINT)NewFont,TRUE);
                                    InvalidateRect(hGraph, NULL, TRUE);
                        }
                    }
       
                    else if (LOWORD(wParam) == IDG_TEXTCOLOR)
                    {
                        CHOOSECOLOR ccg;
                        static COLORREF acrCustClrg[16];
    
                        ZeroMemory(&ccg, sizeof(CHOOSECOLOR));
                        ccg.lStructSize = sizeof(CHOOSECOLOR);
                        ccg.hwndOwner = hGraph;
                        ccg.lpCustColors = NULL;
                        ccg.lpCustColors = (LPDWORD) acrCustClrg;
                        ccg.rgbResult = GraphColor;
                        ccg.Flags = CC_FULLOPEN | CC_RGBINIT;
                        if (ChooseColor(&ccg)==TRUE)
                        {
                                    GraphColor = ccg.rgbResult;
                                    PostMessage(hGraph, WM_PAINT, TRUE, 0);
                                    InvalidateRect(hGraph, NULL, TRUE);
                        }
                    }
    
                    else if (HIWORD(wParam) == EN_CHANGE) GraphNotChg = FALSE;
                    
                    return 0;
            }
           
            case WM_DESTROY:
            {
                free(szBuffer1);
                free(p);
                DeleteObject(NewFont);
                ShowWindow(hGraph, SW_HIDE);
                return 0;
            }

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2005
    Messages : 72
    Points : 77
    Points
    77
    Par défaut
    Il y a plusieurs choses bizarres dans ton code:
    - tu envoies le message WM_PAINT avec WPARAM renseigné, mais dans le traitement du dit message tu ne récupères ce paramètre
    - tu crées une font à chaque fois que tu reçois un message (le code avant le switch(uMsg) ), de même pour le malloc(), c'est inutile et tu risques d'épuiser tes ressources.
    - il ne vaut mieux pas demander le dessin de la fenêtre par SendMessage(Hwnd,WM_PAINT,0,0) mais plutot avec UpdateWindow(Hwnd)

    Je te propose:
    - de mettre HFONT NewFont, OldFont en static et de créer ta font de départ dans WM_CREATE
    - d'appeler SelectObject(hdc,NewFont) après CreateFontIndirect() dans WM_COMMAND
    - d'utiliser UpdateWindow(Hwnd)

    En gros ça pourrait donner le code suivant:

    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
    static BOOL GraphNotChg = TRUE;
    static COLORREF GraphColor;
    static HFONT NewFont, OldFont;
    static char *szBuffer1;
    PAINTSTRUCT pg;
    HDC hdg;
    LOGFONT lfg;
    HPEN hpNew, hpOld;
    int m = 0;
    
    
    switch(uMsg)
    {
       case WM_CREATE:
          szBuffer1 = (char*)malloc(256*sizeof(char));
    
          GraphColor = RGB(0,0,0);
    
          ZeroMemory(&lfg, sizeof(LOGFONT));
          lstrcpy(lfg.lfFaceName, "Times New Roman");
          lfg.lfHeight = 20;
          lfg.lfItalic = TRUE;
          lfg.lfUnderline = FALSE;
          lfg.lfWeight = FW_NORMAL;
                
          hdg = GetDC(hwnd);
          NewFont = CreateFontIndirect(&lfg);
          OldFont = SelectObject(hdg,NewFont);
          ReleaseDC(hwnd, hdg);
          break;
    
       case WM_PAINT:
       {
          char *buffer;
          buffer = (char*)malloc(256*sizeof(char));
    
          hdg = BeginPaint(hGraph, &pg);
          SetBkMode(hdg, TRANSPARENT);
    
          SetTextColor(hdg, GraphColor);
          sprintf(buffer, "p1(x) = %s", afficher(&p[0], szBuffer1));
          TextOut(hdg, 5, 5, buffer, lstrlen(buffer));
          free(buffer);
    
    
          EndPaint(hGraph, &pg);
    
          return 0L;
       }
    
       case WM_COMMAND:
       {
          if (LOWORD(wParam) == IDG_FONT)
          {
             CHOOSEFONT cg;
             ZeroMemory(&cg, sizeof(CHOOSEFONT));
             cg.lStructSize = sizeof (CHOOSEFONT);
             cg.hwndOwner = hGraph;
             cg.lpLogFont = &lfg;
             cg.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
    
             if (ChooseFont(&cg))
             {
                hdg = GetDC(hwnd);
                SelectObject(hdg,OldFont);
                DeleteObject(NewFont);
                NewFont = CreateFontIndirect(&lfg);
                OldFont = SelectObject(hdg,NewFont);
                ReleaseDC(hwnd, hdg);
                InvalidateRect(hGraph, NULL, TRUE);
                UpdateWindow(Hwnd);
             }
          }
    
          else if (LOWORD(wParam) == IDG_TEXTCOLOR)
          {
             CHOOSECOLOR ccg;
             static COLORREF acrCustClrg[16];
    
             ZeroMemory(&ccg, sizeof(CHOOSECOLOR));
             ccg.lStructSize = sizeof(CHOOSECOLOR);
             ccg.hwndOwner = hGraph;
             ccg.lpCustColors = NULL;
             ccg.lpCustColors = (LPDWORD) acrCustClrg;
             ccg.rgbResult = GraphColor;
             ccg.Flags = CC_FULLOPEN | CC_RGBINIT;
             if (ChooseColor(&ccg)==TRUE)
             {
                GraphColor = ccg.rgbResult;
                InvalidateRect(hGraph, NULL, TRUE);
                UpdateWindow(Hwnd);
             }
          }
          else if (HIWORD(wParam) == EN_CHANGE) GraphNotChg = FALSE;
             
          return 0;
       }
       
       case WM_DESTROY:
       {
          free(szBuffer1);
          free(p);
          SelectObject(hdg,OldFont);
          DeleteObject(NewFont);
          ShowWindow(hGraph, SW_HIDE);
          return 0;
       }
    }

  10. #10
    Membre à l'essai
    Inscrit en
    Août 2006
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 31
    Points : 12
    Points
    12
    Par défaut
    Ca marche très bien.
    J'ai juste rajouté "GetObject(NewFont, sizeof(LOGFONT), &lfg);" après "ZeroMemory(&cg, sizeof(CHOOSEFONT));".
    Je te remercie beaucoup pour ton aide et ta patience.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 29
    Dernier message: 14/01/2013, 10h40
  2. tutoriel : La programmation de l'API Windows en C++ par Bob
    Par Aurelien.Regat-Barrel dans le forum Windows
    Réponses: 19
    Dernier message: 21/06/2008, 14h34
  3. Modifier la couleur d'un STATIC avec les API windows
    Par Mirsa dans le forum Visual C++
    Réponses: 31
    Dernier message: 27/11/2006, 11h18
  4. Documentation gratuite sur l'API Windows, COM, DCOM, OLE, etc.
    Par Community Management dans le forum Windows
    Réponses: 1
    Dernier message: 16/11/2006, 15h28
  5. Utilisation de Pointeurs dans API windows
    Par Drooxy dans le forum API, COM et SDKs
    Réponses: 4
    Dernier message: 13/03/2003, 22h39

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