begin process at 2012 05 28 15:35:12
  Trouver un code source :
 
dans
 
Accueil > Forum > 

C++ & C++ .NET

 > 

Windows

 > 

Driver

 > 

Changer couleur par un clique [Win32 sans MFC]


Derniers messages déposésPoser une question dans le forum ou lancer une discussion

Changer couleur par un clique [Win32 sans MFC]

dimanche 22 octobre 2006 à 12:42:02 | Changer couleur par un clique [Win32 sans MFC]

jojomillenium

Bonjour,

j'aimerai pouvoir changer la couleur de fond d'un static, ou meme d'un element picture (mon but et juste d avoir un carré rempli d'une couleur) en cliquant sur des boutons! Par exemple j'ai deux boutons, lorsque je clique sur le premier, le static se colorie en bleue, et l autre bouton le colorie en rouge!

J'ai cherché un moment sur ce site, mais je ne trouve que des sources expliquant comment definire ces couleurs de fond au chargement!
Et quand je tatais les codes pour comprendre, bah ca me definissait tous mes statics de mon dialog de la meme couleur de fond alor que je definissais bien le mon HDC...

Merci de m'aider en m indiquant la procedure!

Jojo

dimanche 22 octobre 2006 à 13:21:53 | Re : Changer couleur par un clique [Win32 sans MFC]

SnOOpss

Si ca marche au chargement pourquoi ca marche pas lors d'un click ? Tu bosses avec quoi ? WM_CTLCOLORSTATIC WM_PAINT WM_ERASEBACKGROUNG ..... IL y a plusieurs moyens d obtenir ca, tu as commencer a programmer avec quoi ?
dimanche 22 octobre 2006 à 13:38:23 | Re : Changer couleur par un clique [Win32 sans MFC]

Ombitious_Developper

Salut :

Supposons qu'on a :

         ---------------------
         |     ID_STATIC     |
         ---------------------

  ------------            ------------
  | ID_ROUGE |            |  ID_BLUE |
  ------------            ------------

// Variables globales
COLORREF clrBr = RGB (0x00, 0x00, 0x00);
RECT     rect;

// Procédure : WndProc
LRESULT CALLBACK WndProc (HWND   hDlg,
                          UINT   uMsg,
                          WPARAM wParam,
                          LPARAM lParam)
{
      int         choice;
      HDC         hDC;
      HWND        hWnd;
      PAINTSTRUCT ps;

      switch (uMsg) {
      case WM_COMMAND:
           choice = LOWORD (wParam);
           switch (choice) {
           case ID_ROUGE:
                hWnd =
GetDlgItem (hWnd, ID_ROUGE)
                GetClientRect (hWnd, &rect);
                return TRUE;

           case ID_BLUE:
               
hWnd = GetDlgItem (hWnd, ID_ROUGE)
                GetClientRect (hWnd, &rect);

                return FALSE;

           default:
                return FALSE;
           }
           break;
      case WM_PAINT:
           hDC = BeginPaint (hDlg, &ps);
           FillRect (hDC,
                     &rect,
                     CreateSolidBrush (clrBr));
           EndPaint (hDlg, &ps);
           break;
      // ...
      }
}
 
dimanche 22 octobre 2006 à 13:40:17 | Re : Changer couleur par un clique [Win32 sans MFC]

Ombitious_Developper

Salut :

J'ai oublié d'ajouter :

case ID_ROUGE:
                hWnd =
GetDlgItem (hWnd, ID_ROUGE)
                GetClientRect (hWnd, &rect);
                clrBr = RGB (0xFF, 0x00, 0x00);
                return TRUE;

case ID_BLUE:
               
hWnd = GetDlgItem (hWnd, ID_ROUGE)
                GetClientRect (hWnd, &rect);

                                        clrBr = RGB (0x00, 0x00, 0xFF);
                return FALSE;
dimanche 22 octobre 2006 à 13:59:13 | Re : Changer couleur par un clique [Win32 sans MFC]

jojomillenium

Je vois, mais ou dis tu que le ID_STATIC devient de la couleur desiré?
Mon truc est un chouillat plus compliqué je t'explique
Dans ma fenetre de dialogue j'ai un treeview et plusieur static dont mon ID_COULEUR_FOND
j'aimerai que lorsque je clique sur un element de mon treeview que le static change de couleur.
Pour l'exemple on dira que le case 1 mon static ID_COULEUR_FOND a une couleur background noir, case 2 il est bleu... et ainsi de suite.
Si vous pensez que je devrai utiliser un autre type que le static pour arriver a mon but y a pas de souci! Ce que je veux c'est betement un carré rempli d'une couleur!

Je code sous VSC++ 6 en API win32 sans MFC

Voici mon code: avec indication de l'endroit ou l'action doit changer la couleur:

#include <windows.h>
#include "General.h"
#include "resource.h"


STVMain objTreeView;    // les données du TreeView

HTREEITEM tvInsertItem(HWND hWnd,HTREEITEM parentItem,LPSTR strText,bool itemChild,long itemParam, bool ItemChecked) {
    TVINSERTSTRUCT tviStr;
    ZeroMemory(&tviStr,sizeof(TVINSERTSTRUCT));
    tviStr.hParent = parentItem;
    tviStr.hInsertAfter = TVI_LAST;
    tviStr.item.pszText = strText;
    tviStr.item.cchTextMax = strlen(strText);
    tviStr.item.cChildren = itemChild;
    tviStr.item.lParam = itemParam;
    tviStr.item.state = NULL;
    tviStr.item.mask = TVIF_CHILDREN | TVIF_TEXT | TVIF_PARAM | TVIF_STATE;
    return TreeView_InsertItem(GetDlgItem(hWnd,ID_TREE), (LPTVINSERTSTRUCT) &tviStr);
}

void tvItemLeftClick(HWND hWnd) {
    TVITEM tvItem;
    ZeroMemory(&tvItem,sizeof(TVITEM));
    tvItem.hItem = TreeView_GetSelection(GetDlgItem(hWnd,ID_TREE));
    tvItem.mask = TVIF_PARAM;
    ListView_GetCheckState(hWnd, ID_TREE);
    TreeView_GetItem(GetDlgItem(hWnd,ID_TREE),&tvItem);
    switch(tvItem.lParam) {
    case 1: // PREMIER CAS OU LE STATIC DEVRAI PASSER D'UNE CERTAIN COULEUR
        SetDlgItemText(hWnd,ID_TITRE,"Aucune couche séléctionnée");
        EnableItems(hWnd, false);
        SelectedLayer = -1;
        CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
       
        break;
    case 2:
// DEUXEME CAS OU LE STATIC DEVRAI PASSER D'UNE CERTAIN COULEUR
        SetDlgItemText(hWnd,ID_TITRE,"Couche NWell");
        EnableItems(hWnd, true);
               
        SelectedLayer = INDICE_NWELL;
        if(LNWell.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        break;
    case 3:
// ET AINSI DE SUITE
        SetDlgItemText(hWnd,ID_TITRE,"Couche Active ");
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_ACTIVE;
        if(LActive.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        break;
    case 4:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Cont");
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_CONT;
        if(LCont.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        break;
    case 5:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Metal1");
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_METAL1;
        if(LMetal1.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        break;
    case 6:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Metal2");
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_METAL2;
        if(LMetal2.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        break;
   
    }
       
}

void InitTreeViewListe(HWND hWnd) {
    STVGroup tvGroup;
    char buftmp[256];
    int NumCouche=0;

    ZeroMemory(&tvGroup,sizeof(STVGroup));
    objTreeView.hMainItem = tvInsertItem(hWnd,NULL,objTreeView.mainName,1,1, true);

    if(LNWell.num_quads>0)
    {
        sprintf(buftmp,"NWell (%d éléments)",LNWell.num_quads);
        objTreeView.hItem_NWell = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,2, true);

    }
    if(LActive.num_quads>0)
    {
        sprintf(buftmp,"Active (%d éléments)",LActive.num_quads);
        objTreeView.hItem_Active = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,3, true);
    }
    if(LCont.num_quads>0)
    {
        sprintf(buftmp,"Cont (%d éléments)",LCont.num_quads);
        objTreeView.hItem_Cont = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,4, true);
    }
    if(LMetal1.num_quads>0)
    {
        sprintf(buftmp,"Metal1 (%d éléments)",LMetal1.num_quads);
        objTreeView.hItem_Metal1 = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,5, true);
    }
    if(LMetal2.num_quads>0)
    {
        sprintf(buftmp,"Metal2 (%d éléments)",LMetal2.num_quads);
        objTreeView.hItem_Metal2 = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,6, true);
    }
   
}

// la procédure CallBack pour la gestion du treeview
LRESULT CALLBACK DlgTreeViewProc(HWND hDlg,UINT msg,WPARAM wParam,LPARAM lParam) {
    switch(msg) {
    case WM_NOTIFY:
        switch(wParam) {
        case ID_TREE:
            UINT cde = ((LPNMHDR)lParam)->code;
            switch(((LPNMHDR)lParam)->code) {
            case TVN_SELCHANGED:
                tvItemLeftClick(hDlg);
                break;
            }
            break;
        }
        break;
    case WM_CONTEXTMENU:
        POINT mpt;
        GetCursorPos(&mpt); // coordonnées de la souris par rapport à l'ecran
        ScreenToClient(GetDlgItem(hDlg, ID_TREE), &mpt); // convertion pour qu'elles soient par rapport au Treeview
        TV_HITTESTINFO tvHit; // on récupère l'item survolé par la souris
        tvHit.pt = mpt;
        break;
       
    case WM_KEYUP:
    case WM_KEYDOWN:
        break;
    case WM_INITDIALOG:
        InitTreeViewListe(hDlg);
        TreeView_Expand(GetDlgItem(hDlg, ID_TREE),objTreeView.hMainItem,TVE_EXPAND);
        break;
    case WM_COMMAND:
        switch (wParam) {
        case ID_CHECK:
            if(SelectedLayer == INDICE_NWELL)
                LNWell.visible= Inv(LNWell.visible);
            if(SelectedLayer == INDICE_ACTIVE)
                LActive.visible= Inv(LActive.visible);
            if(SelectedLayer == INDICE_CONT)
                LCont.visible= Inv(LCont.visible);
            if(SelectedLayer == INDICE_METAL1)
                LMetal1.visible= Inv(LMetal1.visible);
            if(SelectedLayer == INDICE_METAL2)
                LMetal2.visible= Inv(LMetal2.visible);
            break;
        }

    case WM_SYSCOMMAND:
        switch(wParam) {
        case SC_CLOSE: // on empêche de fermer la fenêtre du TreeView
            return 1;
            break;
        }
        return false;
    case WM_CLOSE: // gère la fermeture de la fenêtre
        TreeView_DeleteAllItems(GetDlgItem(hDlg,ID_TREE)); // supprime tous les items
        tvhDlg = NULL; // reset le pointeur de la fenêtre
        EndDialog(hDlg,0); // et la détruit
        return 1;
    }
    return 0;
}

// crée la fenêtre pour le treeview et la remplie avec les infos
bool InitTreeView(const char *mainName) {
    INITCOMMONCONTROLSEX TV_Init_Str;
    TV_Init_Str.dwSize = sizeof(INITCOMMONCONTROLSEX);
    TV_Init_Str.dwICC = ICC_TREEVIEW_CLASSES ;
    InitCommonControlsEx(&TV_Init_Str); // charge les dll utiles pour la gestion du treeview

    objTreeView.mainName = mainName;
    objTreeView.mainName.Cut(objTreeView.mainName.FindFromEnd('\\')+1,objTreeView.mainName.GetLength());
    tvhDlg = CreateDialog(hInstance, MAKEINTRESOURCE(ID_CONTROL), hWnd, (DLGPROC)DlgTreeViewProc);

    return (tvhDlg)?true:false;
}

bool Inv(bool toInv)
{
    if(toInv==true)
        return false;
    else
        return true;
}

void EnableItems(HWND hWnd, bool Set)
{
    EnableWindow(GetDlgItem(hWnd,ID_LAYER),Set);
    EnableWindow(GetDlgItem(hWnd,ID_VISIBILITE),Set);
    EnableWindow(GetDlgItem(hWnd,ID_COULEUR_COUCHE),Set);
    EnableWindow(GetDlgItem(hWnd,ID_CHECK),Set);
    EnableWindow(GetDlgItem(hWnd,ID_HAUTEUR),Set);
    EnableWindow(GetDlgItem(hWnd,ID_SLIDE_HAUTEUR),Set);
    EnableWindow(GetDlgItem(hWnd,ID_SLIDE_ALPHA),Set);
    EnableWindow(GetDlgItem(hWnd,ID_STATIC),Set);
    EnableWindow(GetDlgItem(hWnd,ID_CACHER),Set);
}



Encore merci!


dimanche 22 octobre 2006 à 14:21:31 | Re : Changer couleur par un clique [Win32 sans MFC]

Ombitious_Developper

Salut :

As tu testé mon code?

Une petite rectification :

// Variables globales
COLORREF clrBr = RGB (0xFF, 0x00, 0x00);
RECT     rect;

// Procédure : WndProc
LRESULT CALLBACK WndProc (HWND   hDlg,
                          UINT   uMsg,
                          WPARAM wParam,
                          LPARAM lParam)
{
      int         choice;
      HDC         hDC;
      HWND        hWnd;
      PAINTSTRUCT ps;

      switch (uMsg) {
      case WM_COMMAND:
           choice = LOWORD (wParam);
           switch (choice) {
           case ID_ROUGE:
                hWnd =
GetDlgItem (hWnd, ID_STATIC);
                GetClientRect (hWnd, &rect);
               
clrBr = RGB (0xFF, 0x00, 0x00);
                SendMessage (hDlg, WM_PAINT, 0, 0);

                return TRUE;

           case ID_BLUE:
                
hWnd = GetDlgItem (hWnd, ID_STATIC)
                GetClientRect (hWnd, &rect);

                                        clrBr = RGB (0x00, 0x00, 0xFF);
                SendMessage (hDlg, WM_PAINT, 0, 0);
                return FALSE;

           default:
                return FALSE;
           }
           break;
      case WM_PAINT:
           hDC = BeginPaint (hDlg, &ps);
           FillRect (hDC,
                     &rect,
                     CreateSolidBrush (clrBr));
           EndPaint (hDlg, &ps);
           break;
      // ...
      }
}

dimanche 22 octobre 2006 à 14:36:11 | Re : Changer couleur par un clique [Win32 sans MFC]

vecchio56

Administrateur CodeS-SourceS
SendMessage (hDlg, WM_PAINT, 0, 0); ne sert a rien si aucune zone n'est invalidée auparavant avec InvalidateRect

_____________________________________
Un éditeur de ressources gratuit pour Windows

dimanche 22 octobre 2006 à 14:41:21 | Re : Changer couleur par un clique [Win32 sans MFC]

jojomillenium

oui, voila les changement que j'ai fait en essayant d inserer ton code... mais ca change rien :(
Qu'ai je fais de faux?
Vraiment merci pour ton aide

#include <windows.h>
#include "General.h"
#include "resource.h"


STVMain objTreeView;    // les données du TreeView
COLORREF clrBr = RGB (0xFF, 0x00, 0x00);
RECT     rect;


HTREEITEM tvInsertItem(HWND hWnd,HTREEITEM parentItem,LPSTR strText,bool itemChild,long itemParam, bool ItemChecked) {
    TVINSERTSTRUCT tviStr;
    ZeroMemory(&tviStr,sizeof(TVINSERTSTRUCT));
    tviStr.hParent = parentItem;
    tviStr.hInsertAfter = TVI_LAST;
    tviStr.item.pszText = strText;
    tviStr.item.cchTextMax = strlen(strText);
    tviStr.item.cChildren = itemChild;
    tviStr.item.lParam = itemParam;
    tviStr.item.state = NULL;
    tviStr.item.mask = TVIF_CHILDREN | TVIF_TEXT | TVIF_PARAM | TVIF_STATE;
    return TreeView_InsertItem(GetDlgItem(hWnd,ID_TREE), (LPTVINSERTSTRUCT) &tviStr);
}

void tvItemLeftClick(HWND hWnd) {
    TVITEM tvItem;
    ZeroMemory(&tvItem,sizeof(TVITEM));
    tvItem.hItem = TreeView_GetSelection(GetDlgItem(hWnd,ID_TREE));
    tvItem.mask = TVIF_PARAM;
    ListView_GetCheckState(hWnd, ID_TREE);
    TreeView_GetItem(GetDlgItem(hWnd,ID_TREE),&tvItem);
    switch(tvItem.lParam) {
    case 1:
        SetDlgItemText(hWnd,ID_TITRE,"Aucune couche séléctionnée");
        EnableItems(hWnd, false);
        SelectedLayer = -1;
        CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
       
        break;
    case 2:
        SetDlgItemText(hWnd,ID_TITRE,"Couche NWell");
       

        EnableItems(hWnd, true);
   
        SelectedLayer = INDICE_NWELL;
        if(LNWell.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
            hWnd =  GetDlgItem (hWnd, ID_COULEUR_FOND);
                GetClientRect (hWnd, &rect);
                clrBr = RGB (0xFF, 0x00, 0x00);
                SendMessage (hWnd, WM_PAINT, 0, 0);

               
        break;
    case 3:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Active ");
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_ACTIVE;
        if(LActive.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        break;
    case 4:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Cont");
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_CONT;
        if(LCont.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        break;
    case 5:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Metal1");
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_METAL1;
        if(LMetal1.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        break;
    case 6:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Metal2");
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_METAL2;
        if(LMetal2.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        break;
   
    }
       
}

void InitTreeViewListe(HWND hWnd) {
    STVGroup tvGroup;
    char buftmp[256];
    int NumCouche=0;

    ZeroMemory(&tvGroup,sizeof(STVGroup));
    objTreeView.hMainItem = tvInsertItem(hWnd,NULL,objTreeView.mainName,1,1, true);

    if(LNWell.num_quads>0)
    {
        sprintf(buftmp,"NWell (%d éléments)",LNWell.num_quads);
        objTreeView.hItem_NWell = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,2, true);

    }
    if(LActive.num_quads>0)
    {
        sprintf(buftmp,"Active (%d éléments)",LActive.num_quads);
        objTreeView.hItem_Active = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,3, true);
    }
    if(LCont.num_quads>0)
    {
        sprintf(buftmp,"Cont (%d éléments)",LCont.num_quads);
        objTreeView.hItem_Cont = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,4, true);
    }
    if(LMetal1.num_quads>0)
    {
        sprintf(buftmp,"Metal1 (%d éléments)",LMetal1.num_quads);
        objTreeView.hItem_Metal1 = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,5, true);
    }
    if(LMetal2.num_quads>0)
    {
        sprintf(buftmp,"Metal2 (%d éléments)",LMetal2.num_quads);
        objTreeView.hItem_Metal2 = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,6, true);
    }
   
}

// la procédure CallBack pour la gestion du treeview
LRESULT CALLBACK DlgTreeViewProc(HWND hDlg,UINT msg,WPARAM wParam,LPARAM lParam) {
   
    PAINTSTRUCT ps;
    HDC         hDC;


    switch(msg) {
    case WM_NOTIFY:
        switch(wParam) {
        case ID_TREE:
            UINT cde = ((LPNMHDR)lParam)->code;
            switch(((LPNMHDR)lParam)->code) {
            case TVN_SELCHANGED:
                tvItemLeftClick(hDlg);
                break;
            }
            break;
        }
        break;
    case WM_CONTEXTMENU:
        POINT mpt;
        GetCursorPos(&mpt); // coordonnées de la souris par rapport à l'ecran
        ScreenToClient(GetDlgItem(hDlg, ID_TREE), &mpt); // convertion pour qu'elles soient par rapport au Treeview
        TV_HITTESTINFO tvHit; // on récupère l'item survolé par la souris
        tvHit.pt = mpt;
        break;
       
    case WM_KEYUP:
    case WM_KEYDOWN:
        break;
    case WM_INITDIALOG:
        InitTreeViewListe(hDlg);
        TreeView_Expand(GetDlgItem(hDlg, ID_TREE),objTreeView.hMainItem,TVE_EXPAND);
        break;
    case WM_COMMAND:
        switch (wParam) {
        case ID_CHECK:
            if(SelectedLayer == INDICE_NWELL)
                LNWell.visible= Inv(LNWell.visible);
            if(SelectedLayer == INDICE_ACTIVE)
                LActive.visible= Inv(LActive.visible);
            if(SelectedLayer == INDICE_CONT)
                LCont.visible= Inv(LCont.visible);
            if(SelectedLayer == INDICE_METAL1)
                LMetal1.visible= Inv(LMetal1.visible);
            if(SelectedLayer == INDICE_METAL2)
                LMetal2.visible= Inv(LMetal2.visible);
            break;
        }

    case WM_SYSCOMMAND:
        switch(wParam) {
        case SC_CLOSE: // on empêche de fermer la fenêtre du TreeView
            return 1;
            break;
        }
        return false;
    case WM_CLOSE: // gère la fermeture de la fenêtre
        TreeView_DeleteAllItems(GetDlgItem(hDlg,ID_TREE)); // supprime tous les items
        tvhDlg = NULL; // reset le pointeur de la fenêtre
        EndDialog(hDlg,0); // et la détruit
        return 1;

    case WM_PAINT:
           hDC = BeginPaint (hDlg, &ps);
           FillRect (hDC,
                     &rect,
                     CreateSolidBrush (clrBr));
           EndPaint (hDlg, &ps);
           break;

    }
    return 0;
}

// crée la fenêtre pour le treeview et la remplie avec les infos
bool InitTreeView(const char *mainName) {
    INITCOMMONCONTROLSEX TV_Init_Str;
    TV_Init_Str.dwSize = sizeof(INITCOMMONCONTROLSEX);
    TV_Init_Str.dwICC = ICC_TREEVIEW_CLASSES ;
    InitCommonControlsEx(&TV_Init_Str); // charge les dll utiles pour la gestion du treeview

    objTreeView.mainName = mainName;
    objTreeView.mainName.Cut(objTreeView.mainName.FindFromEnd('\\')+1,objTreeView.mainName.GetLength());
    tvhDlg = CreateDialog(hInstance, MAKEINTRESOURCE(ID_CONTROL), hWnd, (DLGPROC)DlgTreeViewProc);

    return (tvhDlg)?true:false;
}

bool Inv(bool toInv)
{
    if(toInv==true)
        return false;
    else
        return true;
}

void EnableItems(HWND hWnd, bool Set)
{
    EnableWindow(GetDlgItem(hWnd,ID_LAYER),Set);
    EnableWindow(GetDlgItem(hWnd,ID_VISIBILITE),Set);
    EnableWindow(GetDlgItem(hWnd,ID_COULEUR_COUCHE),Set);
    EnableWindow(GetDlgItem(hWnd,ID_CHECK),Set);
    EnableWindow(GetDlgItem(hWnd,ID_HAUTEUR),Set);
    EnableWindow(GetDlgItem(hWnd,ID_SLIDE_HAUTEUR),Set);
    EnableWindow(GetDlgItem(hWnd,ID_SLIDE_ALPHA),Set);
    EnableWindow(GetDlgItem(hWnd,ID_STATIC),Set);
    EnableWindow(GetDlgItem(hWnd,ID_CACHER),Set);
    EnableWindow(GetDlgItem(hWnd,ID_NUM_ALPHA),Set);
    EnableWindow(GetDlgItem(hWnd,ID_NUM_HAUTEUR),Set);
}




dimanche 22 octobre 2006 à 18:03:40 | Re : Changer couleur par un clique [Win32 sans MFC]

Ombitious_Developper

Salut :

Donc on ajoute un InvalidateRect

case ID_ROUGE:
                hWnd =
GetDlgItem (hWnd, ID_STATIC);
                GetClientRect (hWnd, &rect);
               
clrBr = RGB (0xFF, 0x00, 0x00);
               
InvalidateRect (hDlg, &rect, FALSE);
                SendMessage (hDlg, WM_PAINT, 0, 0);
                return TRUE;
case ID_BLUE:
                
hWnd = GetDlgItem (hWnd, ID_STATIC)
                GetClientRect (hWnd, &rect);

                                        clrBr = RGB (0x00, 0x00, 0xFF);
                InvalidateRect (hDlg, &rect, FALSE);
                SendMessage (hDlg, WM_PAINT, 0, 0);
                return FALSE;
}
dimanche 22 octobre 2006 à 19:22:19 | Re : Changer couleur par un clique [Win32 sans MFC]

jojomillenium

MErci pour ton aide, ca marche presque, le probleme c'est que ce n'est pas mon mon static d'id ID_COULEUR_FOND qui se color en rouge, mais un carré se créé en haut a gauche de ma fenetre de dialogue!
voila le code comme je l'ai reecris avec tes indications en rouge, peut etre que tu trouvera mon erreur!
Encore un grand merci!

#include <windows.h>
#include "resource.h"
#include "TreeView.h"
#include "General.h"

STVMain objTreeView;    // les données du TreeView
COLORREF clrBr = RGB (0xFF, 0x00, 0x00);
RECT     rect;


HTREEITEM tvInsertItem(HWND hWnd,HTREEITEM parentItem,LPSTR strText,bool itemChild,long itemParam, bool ItemChecked) {
    TVINSERTSTRUCT tviStr;
    ZeroMemory(&tviStr,sizeof(TVINSERTSTRUCT));
    tviStr.hParent = parentItem;
    tviStr.hInsertAfter = TVI_LAST;
    tviStr.item.pszText = strText;
    tviStr.item.cchTextMax = strlen(strText);
    tviStr.item.cChildren = itemChild;
    tviStr.item.lParam = itemParam;
    tviStr.item.state = NULL;
    tviStr.item.mask = TVIF_CHILDREN | TVIF_TEXT | TVIF_PARAM | TVIF_STATE;
    return TreeView_InsertItem(GetDlgItem(hWnd,ID_TREE), (LPTVINSERTSTRUCT) &tviStr);
}

void tvItemLeftClick(HWND hWnd) {
    HWND hWnd2;
    TVITEM tvItem;
    ZeroMemory(&tvItem,sizeof(TVITEM));
    tvItem.hItem = TreeView_GetSelection(GetDlgItem(hWnd,ID_TREE));
    tvItem.mask = TVIF_PARAM;
    ListView_GetCheckState(hWnd, ID_TREE);
    TreeView_GetItem(GetDlgItem(hWnd,ID_TREE),&tvItem);
    switch(tvItem.lParam) {
    case 1:
        SetDlgItemText(hWnd,ID_TITRE,"Aucune couche séléctionnée");
        EnableItems(hWnd , false);
        SelectedLayer = -1;
        CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
       
        break;
    case 2:
        SetDlgItemText(hWnd,ID_TITRE,"Couche NWell");
        if(LNWell.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_NWELL;
        hWnd2 =  GetDlgItem(hWnd, ID_COULEUR_FOND);
                GetClientRect (hWnd2, &rect);
                clrBr = RGB (0xFF, 0x00, 0x00);
                InvalidateRect (hWnd, &rect, FALSE);
                SendMessage (hWnd, WM_PAINT, 0, 0);

        break;
    case 3:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Active ");
        if(LActive.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_ACTIVE;
        break;
    case 4:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Cont");
        if(LCont.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_CONT;
        break;
    case 5:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Metal1");
        if(LMetal1.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_METAL1;
        break;
    case 6:
        SetDlgItemText(hWnd,ID_TITRE,"Couche Metal2");
        if(LMetal2.visible==true)
            CheckDlgButton(hWnd, ID_CHECK,BST_CHECKED);
        else
            CheckDlgButton(hWnd, ID_CHECK,BST_UNCHECKED);
        EnableItems(hWnd, true);
        SelectedLayer = INDICE_METAL2;
        break;
   
    }
       
}

void InitTreeViewListe(HWND hWnd) {
    STVGroup tvGroup;
    char buftmp[256];
    int NumCouche=0;

    ZeroMemory(&tvGroup,sizeof(STVGroup));
    objTreeView.hMainItem = tvInsertItem(hWnd,NULL,objTreeView.mainName,1,1, true);

    if(LNWell.num_quads>0)
    {
        sprintf(buftmp,"NWell (%d éléments)",LNWell.num_quads);
        objTreeView.hItem_NWell = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,2, true);

    }
    if(LActive.num_quads>0)
    {
        sprintf(buftmp,"Active (%d éléments)",LActive.num_quads);
        objTreeView.hItem_Active = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,3, true);
    }
    if(LCont.num_quads>0)
    {
        sprintf(buftmp,"Cont (%d éléments)",LCont.num_quads);
        objTreeView.hItem_Cont = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,4, true);
    }
    if(LMetal1.num_quads>0)
    {
        sprintf(buftmp,"Metal1 (%d éléments)",LMetal1.num_quads);
        objTreeView.hItem_Metal1 = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,5, true);
    }
    if(LMetal2.num_quads>0)
    {
        sprintf(buftmp,"Metal2 (%d éléments)",LMetal2.num_quads);
        objTreeView.hItem_Metal2 = tvInsertItem(hWnd,objTreeView.hMainItem,buftmp,0,6, true);
    }
   
}

// la procédure CallBack pour la gestion du treeview
LRESULT CALLBACK DlgTreeViewProc(HWND hDlg,UINT msg,WPARAM wParam,LPARAM lParam) {
   
    PAINTSTRUCT ps;
    HDC         hDC;

    switch(msg) {
    case WM_NOTIFY:
        switch(wParam) {
        case ID_TREE:
            UINT cde = ((LPNMHDR)lParam)->code;
            switch(((LPNMHDR)lParam)->code) {
            case TVN_SELCHANGED:
                tvItemLeftClick(hDlg);
                break;
            }
            break;
        }
        break;
    case WM_CONTEXTMENU:
        POINT mpt;
        GetCursorPos(&mpt); // coordonnées de la souris par rapport à l'ecran
        ScreenToClient(GetDlgItem(hDlg, ID_TREE), &mpt); // convertion pour qu'elles soient par rapport au Treeview
        TV_HITTESTINFO tvHit; // on récupère l'item survolé par la souris
        tvHit.pt = mpt;
        break;
       
    case WM_KEYUP:
    case WM_KEYDOWN:
        break;
    case WM_INITDIALOG:
        InitTreeViewListe(hDlg);
        TreeView_Expand(GetDlgItem(hDlg, ID_TREE),objTreeView.hMainItem,TVE_EXPAND);
        break;
    case WM_COMMAND:
        switch (wParam) {
        case ID_CHECK:
            if(SelectedLayer == INDICE_NWELL)
                LNWell.visible= Inv(LNWell.visible);
            if(SelectedLayer == INDICE_ACTIVE)
                LActive.visible= Inv(LActive.visible);
            if(SelectedLayer == INDICE_CONT)
                LCont.visible= Inv(LCont.visible);
            if(SelectedLayer == INDICE_METAL1)
                LMetal1.visible= Inv(LMetal1.visible);
            if(SelectedLayer == INDICE_METAL2)
                LMetal2.visible= Inv(LMetal2.visible);
            break;
        }

    case WM_SYSCOMMAND:
        switch(wParam) {
        case SC_CLOSE: // on empêche de fermer la fenêtre du TreeView
            return 1;
            break;
        }
        return false;
    case WM_CLOSE: // gère la fermeture de la fenêtre
        TreeView_DeleteAllItems(GetDlgItem(hDlg,ID_TREE)); // supprime tous les items
        tvhDlg = NULL; // reset le pointeur de la fenêtre
        EndDialog(hDlg,0); // et la détruit
        return 1;

    case WM_PAINT:
           hDC = BeginPaint (hDlg, &ps);
           FillRect (hDC,
                     &rect,
                     CreateSolidBrush (clrBr));
           EndPaint (hDlg, &ps);
           break;

    }
    return 0;
}

// crée la fenêtre pour le treeview et la remplie avec les infos
bool InitTreeView(const char *mainName) {
    INITCOMMONCONTROLSEX TV_Init_Str;
    TV_Init_Str.dwSize = sizeof(INITCOMMONCONTROLSEX);
    TV_Init_Str.dwICC = ICC_TREEVIEW_CLASSES ;
    InitCommonControlsEx(&TV_Init_Str); // charge les dll utiles pour la gestion du treeview

    objTreeView.mainName = mainName;
    objTreeView.mainName.Cut(objTreeView.mainName.FindFromEnd('\\')+1,objTreeView.mainName.GetLength());
    tvhDlg = CreateDialog(hInstance, MAKEINTRESOURCE(ID_CONTROL), hWnd, (DLGPROC)DlgTreeViewProc);

    return (tvhDlg)?true:false;
}

bool Inv(bool toInv)
{
    if(toInv==true)
        return false;
    else
        return true;
}

void EnableItems(HWND hWnd, bool Set)
{
    EnableWindow(GetDlgItem(hWnd,ID_LAYER),Set);
    EnableWindow(GetDlgItem(hWnd,ID_VISIBILITE),Set);
    EnableWindow(GetDlgItem(hWnd,ID_COULEUR_COUCHE),Set);
    EnableWindow(GetDlgItem(hWnd,ID_CHECK),Set);
    EnableWindow(GetDlgItem(hWnd,ID_HAUTEUR),Set);
    EnableWindow(GetDlgItem(hWnd,ID_SLIDE_HAUTEUR),Set);
    EnableWindow(GetDlgItem(hWnd,ID_SLIDE_ALPHA),Set);
    EnableWindow(GetDlgItem(hWnd,ID_STATIC),Set);
    EnableWindow(GetDlgItem(hWnd,ID_CACHER),Set);
    EnableWindow(GetDlgItem(hWnd,ID_NUM_ALPHA),Set);
    EnableWindow(GetDlgItem(hWnd,ID_NUM_HAUTEUR),Set);
}



1 2

Cette discussion est classée dans : changer, couleur, fond, clique, win32


Répondre à ce message

Sujets en rapport avec ce message

ListCtrl couleur de fond des cases [ par gus2647 ] Bonjour, Je viens de creer une listctrl et j aimerais changer le bkgcolor de la case en fonction de la valeur quelle contient (vert si positif, rouge Changer la couleur du background d'une fenêtre client MDI en Win32 [ par zibo3 ] Bonjour, j'ai un petit soucis, je dois changer la couleur de fond de mes fenêtres clients MDI au cours de mon application et la seule solution que j'a couleur de fond d'une fenetre en WIN32 (VisualC++) [ par glipper ] Bonjour,Je cherche à changer la couleur d'arriere fond d'une fenetre en Win32. Il est en effet possible de faire ça dans la classe de creation de la f couleur de fond boite de dialogue [ par dambeaufort ] Bonjour, Pourriez vous m'indiquer une méthode afin de changer la couleur de fond d'une boite de dialog. Je travaille en MFC sous visual C++.Si vous po changer la couleur du fond de la view [ par glaive ] je veux changer la couleur de fond de la view, je travaille en MFCet je n'est pas une base dans les API de windowsmerci d'avanceglaive l'epée de la ju Changement de toutes les couleurs dans une appli en MFC [ par obby ] Salut,  Je voudrai changer toutes les couleurs dans mon application. J'ai déjà réussi à changer la couleur de fond d'une boite de dialogue mais j'aim Changer la couleur de fond d'une appli [ par Toutoun13 ] Bonjour, J'ai créé une appli de base avec VC++. Par defaut le fond de la fenetre est blanc. Est il possible de modifier ce fond, j'aimerais mettre du Changer la couleur des caractères et du fond d'écran sur Dev-C++ [ par fauve ] Salut à tous Je voudrais savoir si sur Dev-C++, il existe une fonction pour faire changer la couleur des caractères et/ou du fond d'écran ? Ca fait u (win32) couleur de fond d'un static [ par mogwai93 ] Bonjour j'essaie de modifier la couleur de fond d'un static ca passe sauf pour la zone qui entoure que le texte le reste du static etant bien dans la Changer la couleur du fond d'un édit [ par ndubien ] Bonjour, Je cherche à savoir comment faire pour modifier la couleur du fond d'un edit dont je connait le HWND. Merci d'avance pour vos réponses


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,373 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales