#include <windows.h>
#include <wingdi.h>
#include <iostream>
#include "resource.h"
COLORREF color; //variabile del colore, inizializzata a NERO
CHOOSECOLOR Ccolor;
HBRUSH hb; //pennello del colore voluto
int ed[3]; //vettore coordinate colori
int cont = 0; //contatore delle volte che viene ricolorato la static
HWND hwaux; //handle ausiliaria
HDC hdcaux; //handle ausiliaria
DWORD var1;
DWORD var2;
char var3[60];
BOOL CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,
LPSTR lpszArgument,int nFunsterStil){
HWND hwnd;
MSG messages;
/*Disegniamo la finestra di dialogo*/
DialogBoxParam(0,"ColoreDlg",0,WindowProcedure,0);
while (GetMessage (&messages, NULL, 0, 0)){
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
BOOL CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
switch (message) {
case WM_CLOSE:
//distruzione del pennello
DeleteObject((HGDIOBJ)hb);
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_INITDIALOG:
SendMessage(GetDlgItem(hwnd,1101),EM_LIMITTEXT,3,0);
SendMessage(GetDlgItem(hwnd,1102),EM_LIMITTEXT,3,0);
SendMessage(GetDlgItem(hwnd,1103),EM_LIMITTEXT,3,0);
//azzera array colore all'avvio
int i;
for(i=0;i<3;i++){
ed[i] = 0;
};
//inizializzazione del colore in NERO
color = RGB(ed[0],ed[1],ed[2]);
//############################################################
Ccolor.lStructSize = (DWORD)1000;
Ccolor.hwndOwner = hwnd;
//Ccolor.hInstance = ;
//Ccolor.rgbResult = ;
Ccolor.lpCustColors = &color;
//Ccolor.Flag = CC_ANYCOLOR||CC_FULLOPEN;
Ccolor.lCustData = (LPARAM) var1;
//Ccolor.lpfnHook = ;
Ccolor.lpTemplateName = TEXT("Modifica colori");
//############################################################
break;
case WM_CTLCOLORSTATIC://se il messaggio č di tipo colora static
if(GetDlgCtrlID ((HWND)lParam)==1010){
//MessageBox(0,"Devo impostare il colore.","",MB_OK);
if(cont==0){
//immagazziniamo l'informazione dei vari handle solo all'inizio
hwaux = (HWND) lParam;
hdcaux = (HDC) wParam;
}
else{
//distruzione del pennello
if(!DeleteObject((HGDIOBJ)hb))MessageBox(0,"Impossibile eliminare il pennello.","Errore",MB_OK);
};
//creazione del pennello nuovo
hb = CreateSolidBrush(color);
if(hb==NULL)MessageBox(0,"Impossibile creare il pennello.","Errore",MB_OK);
if(SetBkColor(hdcaux,color)==CLR_INVALID)MessageBox(0,"Impossibile impostare sfondo.","Errore",MB_OK);
cont++;
return (BOOL) hb;
}
break;
case WM_COMMAND:
/*ogni volta che si modifica un EditControl il dialogo invia un messaggio
WM_COMMAND con EN_UPDATE in HIWORD*/
if(HIWORD(wParam)==EN_UPDATE){
switch (LOWORD(wParam)) {
case 1101://valore dell'edit control del Rosso modificato
ed[0]=GetDlgItemInt(hwnd,1101,0,0);//prendi il colore dall EditControl
color = RGB(ed[0],ed[1],ed[2]);//imposta il colore
SendMessage(hwnd,WM_CTLCOLORSTATIC,(WPARAM)hdcaux,(LPARAM)hwaux);
/*char check[20];
sprintf(check,"R:%d, G:%d, B:%d",ed[0],ed[1],ed[2]);
MessageBox(0,check,"mandato messaggio",MB_OK);*/
break;
case 1102://valore dell'edit control del Verde modificato
ed[1]=GetDlgItemInt(hwnd,1102,0,0);
color = RGB(ed[0],ed[1],ed[2]);
SendMessage(hwnd,WM_CTLCOLORSTATIC,(WPARAM)hdcaux,(LPARAM)hwaux);
break;
case 1103://valore dell'edit control del Blu modificato
ed[2]=GetDlgItemInt(hwnd,1103,0,0);
color = RGB(ed[0],ed[1],ed[2]);
SendMessage(hwnd,WM_CTLCOLORSTATIC,(WPARAM)hdcaux,(LPARAM)hwaux);
break;
}
}
else if(HIWORD(wParam)==BN_CLICKED&&LOWORD(wParam)==1000){//BOTTONE1
if(!ChooseColor(&Ccolor)) {
var2 = CommDlgExtendedError();
sprintf(var3,"Errore: %d", var2);
MessageBox(0,var3,"Errore",MB_OK);
switch (var2){
case CDERR_DIALOGFAILURE:
MessageBox(0,"1","Err",MB_OK);
break;
case CDERR_INITIALIZATION:
MessageBox(0,"2","Err",MB_OK);
break;
case CDERR_STRUCTSIZE:
MessageBox(0,"3 Struct Size","Err",MB_OK);
break;
case CDERR_REGISTERMSGFAIL:
MessageBox(0,"4","Err",MB_OK);
break;
case CDERR_NOTEMPLATE:
MessageBox(0,"5","Err",MB_OK);
break;
case CDERR_NOHOOK:
MessageBox(0,"6","Err",MB_OK);
break;
}
}
}
else if(HIWORD(wParam)==BN_CLICKED&&LOWORD(wParam)==1001){//BOTTONE2
}
break;
}
return 0;
}