/*****************************/ /*** ide : DevC++ 4.9.9.2 ***/ /*** gui_zd © 2007 mrud ***/ /*****************************/ #include #include #include #include //Pour la version TCHAR de strlen()/strcpy()/strcat() #define IDM_QUIT 1 LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); #ifndef ARRAYSIZE #define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0])) #endif /* 1) Utiliser les TCHAR 2) Inutile d'avoir des variables modifiables. 3) Ça be sert à rien qu'elles soient en global (surtout classname). TCHAR szClassName[ ] = TEXT("WindowsApp"); TCHAR titrefenetre[ ] = TEXT("GuiGui !!!";*/ LPCTSTR const titrefenetre = TEXT("GuiGui !!!"); int WINAPI WinMain( HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nShowCommand ) { (void)hPrevInstance; (void)lpszArgument; LPCTSTR const szClassName= TEXT("WindowsApp"); HWND hwnd; /* This is the handle for our window */ MSG lastMessage; /* Here messages to the application are saved */ /* UN SEUL message, en fait. */ WNDCLASSEX wincl; /* Data structure for the windowclass */ HBRUSH windO; windO=CreateSolidBrush(RGB(45,45,45)); /* The Window structure */ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ wincl.style = CS_DBLCLKS; /* Catch double-clicks */ wincl.cbSize = sizeof(wincl); /*Utiliser directement la taille de la structure. */ /* Use default icon and mouse-pointer */ /* Pour avoir les icônes à la bonne taille. */ wincl.hIcon = static_cast(LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR)); wincl.hIconSm = static_cast(LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR)); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; /* No menu */ wincl.cbClsExtra = 0; /* No extra bytes after the window class */ wincl.cbWndExtra = 0; /* structure or the window instance */ /* Use Windows's default color as the background of the window */ wincl.hbrBackground = windO; /* Register the window class, and if it fails quit the program */ if(!RegisterClassEx(&wincl)) return 0; /* The class is registered, let's create the program*/ hwnd = CreateWindowEx( 0, /* Extended possibilites for variation */ szClassName, /* Classname */ titrefenetre, /* Title Text */ WS_POPUP | WS_SYSMENU | WS_VISIBLE, /* default window */ 150, /* Windows decides the position */ 150, /* where the window ends up on the screen */ 546, /* The programs width */ 380, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); /* Make the window visible on the screen */ ShowWindow(hwnd, nShowCommand); /* Run the message loop. It will run until GetMessage() returns 0 */ while(GetMessage(&lastMessage, NULL, 0, 0)) { /* Translate virtual-key messages into character messages */ TranslateMessage(&lastMessage); /* Send message to WindowProcedure */ DispatchMessage(&lastMessage); } /* The program return-value is 0 - The value that PostQuitMessage() gave */ return static_cast(lastMessage.wParam); } //=========================== //Variables Gtext() int type=0;// type : 0 FW_NORMAL, 1 FW_BOLD int Tai=0;//taille du texte int color=0;//couleur du texte int x=0;// int y=0;//coordonnées du texte //==== /* Utiliser les TCHAR ici aussi. Il faudra aussi remettre ça en paramètre quand tu en auras le temps... EDIT: Bon, celle-ci, on va la garder en CHAR. */ char textBna[80] = "";//variables qui contient le texte sur le moment avant //j'avais mis toutes ses variables en paramètre de Gtext //mais suite au bug j'ai testé pas mal... //=========================== void Gtext(HDC hdc) { LOGFONT ChangeFont; ChangeFont.lfCharSet=ANSI_CHARSET; ChangeFont.lfOutPrecision=OUT_CHARACTER_PRECIS; ChangeFont.lfClipPrecision=CLIP_CHARACTER_PRECIS; ChangeFont.lfQuality=DEFAULT_QUALITY; ChangeFont.lfPitchAndFamily=FF_SWISS; ChangeFont.lfItalic=FALSE; ChangeFont.lfUnderline=FALSE; ChangeFont.lfStrikeOut=FALSE; ChangeFont.lfOrientation=FALSE; ChangeFont.lfEscapement=0; if(type==1) { ChangeFont.lfWeight=FW_BOLD; } else if(type==0) { ChangeFont.lfWeight=FW_NORMAL; } ChangeFont.lfHeight=Tai; /* Erreur ici sur le deleteobject: La font est toujours sélectionnée dans le DC fontitre=CreateFontIndirect(&ChangeFont); SelectObject(hdc,fontitre); SetTextColor(hdc,color); SetBkMode(hdc,TRANSPARENT); TextOut(hdc,x,y,textBna,strlen(textBna)); DeleteObject(fontitre);*/ { HFONT hFontitre=CreateFontIndirect(&ChangeFont); HFONT hOldFont = static_cast(SelectObject(hdc,hFontitre)); SetTextColor(hdc,color); SetBkMode(hdc,TRANSPARENT); /* Note: Ici, j'utilise lstrlen() qui retourne un int. Cette fonction n'est pas déclarée dans mais dans */ TextOutA(hdc, x, y, textBna, lstrlenA(textBna)); SelectObject(hdc, hOldFont); DeleteObject(hFontitre); } } //=========================== void Title_bar(HDC hdc) { HBRUSH bbartitle; HPEN pbartitle; /* Erreur ici. */ bbartitle=CreateSolidBrush(RGB(85,85,85)); pbartitle=CreatePen(PS_SOLID,1,RGB(0,0,0)); { HBRUSH hOldBrush = static_cast< HBRUSH >(SelectObject(hdc,bbartitle)); HPEN hOldPen = static_cast< HPEN >(SelectObject(hdc,pbartitle)); RoundRect(hdc,4,4,506,20,6,6); type=1; Tai=16; color=RGB(197,255,0); x=12; y=4; /* strcpy() est dangereux : utiliser strncat() à la place. (d'ailleurs, je me demande bien pourquoi tu copies, surtout que tu le fais à chaque fois...) _tcscpy(textBna,titrefenetre); */ /* Je ne peux plus utiliser ceci comme je l'ai repassé en char : il faut convertir. textBna[0] = TEXT('\0'); _tcsncat(textBna, titrefenetre, ARRAYSIZE(textBna)-1);*/ WideCharToMultiByte(CP_ACP, 0, titrefenetre, -1, textBna, ARRAYSIZE(textBna), NULL, NULL); Gtext(hdc); SelectObject(hdc,hOldBrush); SelectObject(hdc,hOldPen); } DeleteObject(bbartitle); DeleteObject(pbartitle); } //============== //variables Button() int etatBn[20][5]; int IDb=0; //id du bouton pour le repérage "dynamique" aux évenements //BUTTON 0 = RESIZE_WINDOW (réduire) //BUTTON 1 = CLOSE_WINDOW (fermer) //BUTTON 2 = RECHERCHE (recherche) //============== //normalement initButton me sert à charger les données des boutons /*void initButton(int IDb,int etat,int x,int y,int Ht,int Lg) { etatBn[IDb][0]=etat; etatBn[IDb][1]=x; etatBn[IDb][2]=y; etatBn[IDb][3]=x+Lg; //largeur etatBn[IDb][4]=y+Ht; //hauteur }*/ void Button(HDC hdc) { HBRUSH bbutton = NULL; HPEN pbutton = NULL; HBRUSH hOldBrush = static_cast< HBRUSH >(GetCurrentObject(hdc, OBJ_BRUSH)); HPEN hOldPen = static_cast< HPEN >(GetCurrentObject(hdc, OBJ_PEN)); if(etatBn[IDb][0]==2) { //etat click droit bbutton=CreateSolidBrush(RGB(85,85,85)); SelectObject(hdc,bbutton); pbutton=CreatePen(PS_SOLID,1,RGB(197,255,0)); SelectObject(hdc,pbutton); RoundRect(hdc,etatBn[IDb][1],etatBn[IDb][2],etatBn[IDb][3],etatBn[IDb][4],4,4); SelectObject(hdc,hOldBrush); DeleteObject(bbutton); bbutton=NULL; SelectObject(hdc,hOldPen); DeleteObject(pbutton); bbutton=NULL; bbutton=CreateSolidBrush(RGB(160,205,49)); SelectObject(hdc,bbutton); pbutton=CreatePen(PS_SOLID,1,RGB(0,0,0)); SelectObject(hdc,pbutton); RoundRect(hdc,etatBn[IDb][1]+1,etatBn[IDb][2]+1,etatBn[IDb][3]-1,etatBn[IDb][4]-1,4,4); SetTextAlign(hdc,TA_CENTER); type=0; Tai=13; color=RGB(0,0,0); x=(etatBn[IDb][1]+etatBn[IDb][3])/2; y=((etatBn[IDb][2]+etatBn[IDb][4])/2)-7; Gtext(hdc); } else if(etatBn[IDb][0]==1) { //etat passage souris bbutton=CreateSolidBrush(RGB(0,0,0)); SelectObject(hdc,bbutton); pbutton=CreatePen(PS_SOLID,1,RGB(160,205,90)); SelectObject(hdc,pbutton); RoundRect(hdc,etatBn[IDb][1],etatBn[IDb][2],etatBn[IDb][3],etatBn[IDb][4],4,4); SetTextAlign(hdc,TA_CENTER); type=0; Tai=13; color=RGB(197,255,0); x=(etatBn[IDb][1]+etatBn[IDb][3])/2; y=((etatBn[IDb][2]+etatBn[IDb][4])/2)-7; Gtext(hdc); } else if(etatBn[IDb][0]==0) { //etat null bbutton=CreateSolidBrush(RGB(0,0,0)); SelectObject(hdc,bbutton); pbutton=CreatePen(PS_SOLID,1,RGB(80,80,80)); SelectObject(hdc,pbutton); RoundRect(hdc,etatBn[IDb][1],etatBn[IDb][2],etatBn[IDb][3],etatBn[IDb][4],4,4); SetTextAlign(hdc,TA_CENTER); type=0; Tai=13; color=RGB(160,205,49); x=(etatBn[IDb][1]+etatBn[IDb][3])/2; y=((etatBn[IDb][2]+etatBn[IDb][4])/2)-7; Gtext(hdc); } SelectObject(hdc,hOldBrush); DeleteObject(bbutton); bbutton=NULL; SelectObject(hdc,hOldPen); DeleteObject(pbutton); bbutton=NULL; } //============== //variables pour progressbar -> progress() int pro; //etat d'avancement int XP; //x int YP; //y char prct[5]; //sert a convertir int pro; en char pour le textout via Gtext() //============== void progress(HDC hdc) { HPEN pprogress=CreatePen(PS_SOLID,1,RGB(80,80,80)); HPEN hOldPen = static_cast< HPEN >(SelectObject(hdc,pprogress)); HBRUSH bprogress=CreateSolidBrush(RGB(0,0,0)); HBRUSH hOldBrush = static_cast< HBRUSH >(SelectObject(hdc,bprogress)); RoundRect(hdc,XP,YP,102+XP,12+YP,6,6); /* ********************************************* * CA Y EST!!!!!!! * LA FUITE MÉMOIRE EST TROUVÉE : ELLE EST ICI! * Tu ne détruits pas les anciens! */ /* Rajouté par Médinoc */ SelectObject(hdc,hOldBrush); DeleteObject(bprogress); bprogress=NULL; SelectObject(hdc,hOldPen); DeleteObject(pprogress); bprogress=NULL; /* Fin Rajouté par Médinoc */ if((pro>0) && (pro<=100)) { pprogress=CreatePen(PS_SOLID,1,RGB(192,255,0)); SelectObject(hdc,pprogress); bprogress=CreateSolidBrush(RGB(160,205,49)); SelectObject(hdc,bprogress); RoundRect(hdc,XP+2,YP+2,XP+pro,YP+2+8,3,3); itoa(pro,prct,10); strcat(prct," %"); SetTextAlign(hdc,TA_CENTER); type=0; Tai=9; color=RGB(85,85,85); x=XP+52; y=YP+2; strcpy(textBna,prct); Gtext(hdc); } else if(pro==0) { pprogress=CreatePen(PS_SOLID,1,RGB(192,25,0)); SelectObject(hdc,pprogress); bprogress=CreateSolidBrush(RGB(35,35,35)); SelectObject(hdc,bprogress); RoundRect(hdc,XP+1,YP+1,XP+101,YP+11,3,3); SetTextAlign(hdc,TA_CENTER); type=1; Tai=10; color=RGB(250,0,0); x=XP+50; y=YP+1; strcpy(textBna,"E C H E C"); Gtext(hdc); } SelectObject(hdc,hOldBrush); DeleteObject(bprogress); bprogress=NULL; SelectObject(hdc,hOldPen); DeleteObject(pprogress); bprogress=NULL; } void upcontrol(HWND hwnd) //rafraichi le control contenu dans int IDb; sur le moment { RECT upcont; //SetRect(&upcont,etatBn[IDb][1],etatBn[IDb][2],etatBn[IDb][3],etatBn[IDb][4]); upcont.left=etatBn[IDb][1]; upcont.top=etatBn[IDb][2]; upcont.right=etatBn[IDb][3]; upcont.bottom=etatBn[IDb][4]; //UpdateWindow(hwnd); InvalidateRect(hwnd,&upcont,TRUE); } //============================================================================== /* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { /* Ici, on suit le modèle Single Entry, Single Exit */ LRESULT lrRetour = 0; PAINTSTRUCT ps; HDC hdc; int c,d,e,f,g,h,i,u,o; //évenements souris : de c à h variables position souris, i u et o pour les boucles switch(message) { case WM_CREATE: etatBn[0][0]=0; etatBn[0][1]=510; etatBn[0][2]=5; etatBn[0][3]=510+14; etatBn[0][4]=5+14; //initButton(0,0,510,5,14,14);//resize etatBn[1][0]=0; etatBn[1][1]=526; etatBn[1][2]=5; etatBn[1][3]=526+14; etatBn[1][4]=5+14; //initButton(1,0,526,5,14,14);//close etatBn[2][0]=0; etatBn[2][1]=480; etatBn[2][2]=30; etatBn[2][3]=480+60; etatBn[2][4]=30+14; //initButton(2,0,480,30,14,60);//recherche break; case WM_PAINT: hdc = BeginPaint(hwnd,&ps); //affichage barre de titre Title_bar(hdc); //affichage des trois boutons strcpy(textBna,"_"); IDb=0; Button(hdc);//resize strcpy(textBna,"x"); IDb=1; Button(hdc);//close strcpy(textBna,"recherche"); IDb=2; Button(hdc);//recherche //affichage des progressbar pro=8; XP=300; YP=100; progress(hdc);//test d'un debut de progressbar pro=79; XP=300; YP=115; progress(hdc); pro=98; XP=300; YP=130; progress(hdc); pro=100; XP=300; YP=145; progress(hdc); pro=50; XP=300; YP=160; progress(hdc); pro=0; XP=300; YP=175; progress(hdc); EndPaint(hwnd,&ps); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_KEYDOWN: if(wParam == VK_ESCAPE) PostMessage(hwnd,WM_CLOSE,0,0); //touche echappe ferme la fenetre break; case WM_MOUSEMOVE: //deplacement souris c=(short)LOWORD(lParam);//recuperation de x d=(short)HIWORD(lParam);//recuperation de y for(i=0;i<3;i++)//je boucle sur mes 3boutons et vérifis les coordonnées souris avec les leurs { if(((c >= etatBn[i][1]) && (c <= etatBn[i][3])) && ((d >= etatBn[i][2]) && (d <= etatBn[i][4]))) { if(etatBn[i][0]==0)//si etat du bouton est null je lui attribu l'etat 1 { etatBn[i][0]=1;//attribution de l'etat IDb=i; upcontrol(hwnd);//charge l'id du bouton puis je redessine le bouton } } else if(etatBn[i][0]!=0) //si l'etat du bouton n'est pas nul je lui attribu cet état { etatBn[i][0]=0;//attribution de l'etat IDb=i; upcontrol(hwnd);//charge l'id du bouton puis je redessine le bouton } } break; case WM_LBUTTONDOWN://bouton souris gauche enfoncé e=(short)LOWORD(lParam);//recuperation de x f=(short)HIWORD(lParam);//recuperation de y for(u=0;u<3;u++)//je boucle sur mes 3boutons et vérifis les coordonnées souris avec les leurs { if(((e >= etatBn[u][1]) && (e <= etatBn[u][3])) && ((f >= etatBn[u][2]) && (f <= etatBn[u][4]))) { if(etatBn[u][0]==1)//si etat du bouton est de 1 je lui attribu l'etat 2 { etatBn[u][0]=2;//attribution de l'etat IDb=u; upcontrol(hwnd);//charge l'id du bouton puis je redessine le bouton } } } break; case WM_LBUTTONUP: g=(short)LOWORD(lParam);//recuperation de x h=(short)HIWORD(lParam);//recuperation de y for(o=0;o<3;o++)//je boucle sur mes 3boutons et vérifis les coordonnées souris avec les leurs { if(((g >= etatBn[o][1]) && (g <= etatBn[o][3])) && ((h >= etatBn[o][2]) && (h <= etatBn[o][4]))) { if(etatBn[o][0]==2)//si etat du bouton est de 2 j'attibu leur fonction { if(o==0) { ShowWindow(hwnd,SW_SHOWMINIMIZED); etatBn[o][0]=0; }//bouton reduire minimise la fenetre et je lui attribu l'etat 0 pour qu'à son retour le bouton ne soit pas en etat 1 (survol souris) else if(o==1) { PostMessage(hwnd,WM_CLOSE,0,0); }//bouton fermer appel WM_CLOSE else if(o!=0) { etatBn[o][0]=1; }//si le bouton n'est pas "reduire" (id:0) je met le bouton trouver à l'état 1 IDb=o; upcontrol(hwnd);//charge l'id du bouton puis je redessine le bouton } } } break; //WM_CLOSE détruit déjà la fenête par défaut : inutile de le mettre. //case WM_CLOSE: // DestroyWindow(hwnd); // break; //oublié default: /* for messages that we don't deal with */ lrRetour = DefWindowProc(hwnd, message, wParam, lParam); } return lrRetour; }