Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Paint XY -Sistemi & Grafici da File- - main.cpp

main.cpp

Caricato da: Dany
Scarica il programma completo

  1. /***************************************************
  2. ***********PAINT XY -SISTEMI & GRAFICI DA FILE-*****
  3. ***********AUTORE:  DANY****************************
  4. ***********DATA:    17/07/12************************
  5. ***********ORE:   20:23*****************************
  6. ***********VERSIONE:  1.1***************************/
  7.  
  8. //Dichiarazione Header
  9. #include <windows.h>
  10. #include <Windowsx.h>
  11. #include <commctrl.h>
  12. #include <cmath>
  13. #include <ctime>
  14. #include <stack>  
  15. #include <fstream>
  16. #include <cstring>
  17. //Dichiarazione librerie:
  18. #include "funzioni.h"
  19. //Dichiarazioni per visual style (grafica win 7)
  20. #pragma comment( lib, "comctl32.lib")
  21. #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
  22. //Fine Dichiarazioni...                                                                                            //prima della p--> \
  23. //Namespace:
  24. using namespace std;
  25. //Dichiarazione variabili globali:
  26. //Class Name delle finestre
  27. static char g_szClassName[] = "Main",name[]="File",name2[]="System";
  28. static HINSTANCE g_hInst = NULL;
  29. //Timer per la WM_Timer nella mainWindows
  30. const UINT idTimer1 = 1;
  31. UINT nTimerDelay = 1;
  32. //Bitmap
  33. HBITMAP hbmBall, hbmMask,griglia,maskgri;
  34. BITMAP bm;
  35. //Coordinate Punto funzione
  36. int ballX, ballY;
  37. //Variabile che indica se è stato cliccato il tasto disegna nella finestra dei file
  38. int agg=0;  
  39. //Font dei Caratteri:
  40. RECT rect;
  41. HFONT hfont;
  42. LOGFONT lf;
  43. //Progress Bar in file Window
  44. HWND prog;
  45. //Progress Bar in system windows
  46. HWND prog2;
  47. //stringhe che in function window contiene le funzioni in notazione polacca inversa.
  48. string rpn="",rpn2="",rpn3="",rpn4="";
  49. //Variabile che indica se il pulsante disegna in function window è stato premuto.
  50. int funpress=0;
  51. //Point che contiene le posizioni dei punti che collegherà  la linea
  52. POINT pp[2],p[2];
  53. //Minimi e massimi delle equazioni dei sistemi
  54. float Mx=0,My=0;
  55. //Precisione
  56. int prec=0;
  57. //Se checkbox è checkata in filewindow
  58. int ck,ckx;
  59.  
  60.  
  61. //Dichiarazione della WndProc Della MainWindow
  62. LRESULT CALLBACK WndProc(HWND hwnd ,UINT Message, WPARAM wParam, LPARAM lParam);
  63. //Disegna il punto utilizzato nelle rappresentazioni di cruve e funzioni.
  64. void DrawBall(HDC hdc)
  65. {
  66.    HDC hdcMemory;
  67.    hdcMemory = CreateCompatibleDC(hdc);
  68.  
  69.    SelectObject(hdcMemory, hbmMask);
  70.    BitBlt(hdc, ballX, ballY, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCAND);
  71.  
  72.    SelectObject(hdcMemory, hbmBall);
  73.    BitBlt(hdc, ballX, ballY, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCPAINT);
  74.  
  75.    DeleteDC(hdcMemory);
  76. }
  77. //Disegna la griglia di sfondo
  78. void DrawGriglia(HDC hdc)
  79. {
  80.      HDC hdcMemory=0;
  81.      hdcMemory=CreateCompatibleDC(hdc);
  82.      
  83.          SelectObject(hdcMemory, maskgri);
  84.          BitBlt(hdc, 0, 0, 1000, 1000, hdcMemory, 0, 0, SRCAND);
  85.          
  86.          SelectObject(hdcMemory, griglia);
  87.      BitBlt(hdc, 0, 0, 1000, 1000, hdcMemory, 0, 0, SRCPAINT);
  88.      
  89.      DeleteDC(hdcMemory);
  90. }
  91.  
  92. /*************************************************************************************************************/
  93. //Dichiarazione variabili in WndProc
  94. //Variabili disegno funzione
  95. double i,x,y,j;
  96. //Variabili Menu:
  97. HMENU file,hfile,info,hinfo,modifica,hmodifica;
  98. int MENU_FILE_EXIT=0;  //Valore ritorno se si clicca su "Esci"
  99. int MENU_FILE_CURV=1;  //Valore ritorno se si clicca su "Disegna Equazione"
  100. int MENU_FILE_FUNZ=2;  //Valore ritorno se si clicca su "Disegna da file"
  101. int MENU_FILE_ABOU=3;  //Valore ritorno se si clicca su "Informazioni su "Paint XY"
  102. int MENU_FILE_HELP=4;  //Valore ritorno se si clicca su "HELP"
  103. int MENU_FILE_CLEA=6;  //Valore ritorno se si clicca su "Pulisci"
  104.  
  105. /*Variabili posizione mouse*/
  106. HWND lix,ley;
  107. float  cox=0,coy=0;
  108. char cux[7]="",cooy[7]="";
  109. /*result:*/
  110. float result,result2,result3,result4;
  111. //Scrive punti di inersezione
  112. char  pax[6],pay[6];
  113.  
  114. /*WNDPROC DELLA MAIN*/
  115. LRESULT CALLBACK WndProc(HWND hwnd ,UINT Message, WPARAM wParam, LPARAM lParam)
  116. {  
  117.         InitCommonControls ();   //Applicazione visual style
  118.        
  119.    switch(Message)
  120.    {
  121.       case WM_CREATE:
  122.                   //Nella finestra principale:
  123.                   //Inizializzazione delle immagini BMP
  124.           hbmBall = LoadBitmap(g_hInst, "BALLBMP");
  125.           griglia = LoadBitmap(g_hInst, "gri");
  126.           hbmMask = LoadBitmap(g_hInst, "MASKBMP");
  127.                   maskgri = LoadBitmap(g_hInst, "maskgri");
  128.           if(!hbmBall || !hbmMask)
  129.           {
  130.                                 MessageBox(hwnd, "Load of resources failed.", "Error",MB_OK | MB_ICONEXCLAMATION);
  131.                                 return -1;
  132.           }
  133.           GetObject(hbmBall, sizeof(bm), &bm);
  134.                   //Timer:
  135.           SetTimer(hwnd, idTimer1, nTimerDelay, NULL);
  136.                   //Menu:
  137.                   //Dichiarazione Menu:
  138.                   file=CreateMenu();
  139.                   hfile=CreatePopupMenu();
  140.                   info=CreateMenu();
  141.                   hinfo=CreatePopupMenu();
  142.                   modifica=CreateMenu();
  143.                   hmodifica=CreatePopupMenu();
  144.                   //Creazione Menu:
  145.                   /*File*/
  146.           AppendMenu(file,MF_STRING|MF_POPUP,(UINT_PTR) hfile,TEXT("File"));
  147.           AppendMenu(hfile,MF_STRING,MENU_FILE_EXIT,TEXT("Esci"));
  148.                   /*Modifica*/
  149.                   AppendMenu(file,MF_STRING|MF_POPUP,(UINT_PTR) hmodifica,TEXT("Disegna"));
  150.           AppendMenu(hmodifica,MF_STRING,MENU_FILE_CURV,TEXT("Grafico da File"));
  151.                   AppendMenu(hmodifica,MF_STRING,MENU_FILE_FUNZ,TEXT("Sistema"));
  152.                   AppendMenu(hmodifica,MF_STRING,MENU_FILE_CLEA,TEXT("Pulisci"));
  153.                   /*Info*/
  154.                   AppendMenu(file,MF_STRING|MF_POPUP,(UINT_PTR) hinfo,TEXT("Info"));
  155.                   AppendMenu(hinfo,MF_STRING,MENU_FILE_ABOU,TEXT("Informazioni su Paint XY"));
  156.                   AppendMenu(hinfo,MF_STRING,MENU_FILE_HELP,TEXT("Help"));
  157.                   //Rende visibile i menu
  158.                   SetMenu(hwnd,file);  
  159.                   //Piccolo edit che scrive la posizione del mouse nella finestra:
  160.                   lix=CreateWindow("edit","",WS_CHILD|WS_VISIBLE|ES_READONLY|WS_BORDER,5,5,50,20,hwnd,(HMENU)100,0,0);
  161.                   ley=CreateWindow("edit","",WS_CHILD|WS_VISIBLE|ES_READONLY|WS_BORDER,60,5,50,20,hwnd,(HMENU)100,0,0);
  162.                   /****SETTO I CARATTERI DA UTILIZZARE: FONT, DIMENSIONI ECC...**/
  163.                   GetObject (GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  164.                   hfont = CreateFont (20,0,0,0, 500, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
  165.           CLIP_DEFAULT_PRECIS, 5, VARIABLE_PITCH, TEXT ("Monotype Corsiva"));
  166.                   SendMessage (lix, WM_SETFONT, (WPARAM) hfont, true);
  167.                   SendMessage (ley, WM_SETFONT, (WPARAM) hfont, true);
  168.  
  169.           break;
  170.       case WM_TIMER:
  171.          if(hbmBall && hbmMask)
  172.          {
  173.             HDC hdcWindow;
  174.             hdcWindow = GetDC(hwnd);
  175.                         /*SETTO LA PENNA E LA CASUALITà DEL COLORE*/
  176.                         srand(time(NULL));
  177.                         HPEN penna=CreatePen(PS_SOLID,3, RGB(rand()%255,100-rand()%255,100+rand()%255));
  178.                         SelectObject(hdcWindow,penna);
  179.  
  180.                     //Disegno Equazioni:***************************************************************************************************************
  181.                         if (funpress>0)                   //Se è stato cliccato il pulsante disegna nella funciton windows:
  182.                         {
  183.                 /*FUNZIONAMENTO IN BREVE:
  184.                 *Il grafico si compone da destra verso sinistra con linee che si alternano:
  185.                 *una va dall'alto in basso, la successiva dal basso all'alto in modo che
  186.                 *dato che i punti si collegano in successione, visaranno meno linee trasversali possibile*/
  187.                                 int alt=0;
  188.                                 funpress=0;  
  189.                                 //Il ciclo va dall'opposto del massimo di x a quest'ultimo con
  190.                 //un incremento del doppio di questo fratto la precisione scelta.
  191.                                 for (i=-Mx;i<=Mx;i=i+(2*Mx)/prec)
  192.                                 {
  193.                                         x=i;
  194.                                         //Aggiorno la progress bar
  195.                                         SendMessage(prog,PBM_SETPOS,1+(i+Mx)*100/(2*Mx),0);
  196.                                         alt++;
  197.                     //'Alt' si incrementa ogni ciclo, permette di alternare il verso in cui il
  198.                     //programma trova i punti (infatti i 2 ccli sono inversi)
  199.                     //in modo che, dato che il programma collega i punti in successione,
  200.                     //ridurre al minimo le linee trasversali.
  201.                                         if (alt%2==0)
  202.                                         for (j=-My;j<=My;j=j+(My*2)/prec)
  203.                                         {
  204.                                                
  205.                                                 y=j;
  206.                                                 //Risolvo le equazioni in notazione rpn
  207.                                                 result=SolveRPN(rpn,x,y);
  208.                                                 result2=SolveRPN(rpn2,x,y);
  209.                                                 result3=SolveRPN(rpn3,x,y);
  210.                                                 result4=SolveRPN(rpn4,x,y);
  211.                                                 //Se la differenza tra i due membri è minore della
  212.                                                 //millesima parte del valore massimo, allora disegna il punto(o linea)
  213.                                                 //Questo è più efficace di 'result==result2' poichè può capitare
  214.                                                 //che a causa dell'incremento nel ciclo il valore della prima
  215.                                                 //espressione non sarà mai uguale a quella della seconda, e perciò
  216.                         //metto un margine di errore.
  217.                                                 if (fabs(result-result2)<=Mx/1000)
  218.                                                 {
  219.                             //Converto i valori in coordinate pixel in proporzione
  220.                             //al massimo delle rispettive varabili
  221.                                                         ballX=conx(x,Mx);
  222.                                                         ballY=cony(y,My);
  223.                                                         //memorizzo il punto nell'array
  224.                                                         pp[1].x=ballX;
  225.                                                         pp[1].y=ballY;
  226.                                                         /*Il programma traccia una linea che collega i due punti nell'array 'pp'
  227.                                                         * SE:
  228.                                  -LE COO. DEL PUNTO PRECEDENTEMENTE (PP[0])TROVATO DA COLLEGARE A QUESTO
  229.                                      APPENA TROVATO SONO DIVERSE DA 0 (COSì NON SI COLLEGA ALL'INIZIO DELLA
  230.                                      FINESTRA, POICHè L'ARRAY è INIZIALIZZATO CON PP[0]=0.
  231.                                  -LA DISTANZA TRA I DUE PUNTI è MINORE DI 220 COSì DA EVITARE IL
  232.                                      COLLEGAMENTO ERRONEO DI PUNTI DISTANTI COME IN "Y=1/X". INFATTI
  233.                                      SENZA DI QUESTA, VI SAREBBE UNA LINEA CHE COLLEGA ERRONEAMENTE
  234.                                      IL PUNTO PIù IN BASSO CON QUELLO PIù IN ALTO (DETTO TERRA TERRA)
  235.                                  -E' ATTIVATA LA SPUNTA A "COLLEGA CON LINEE"     */
  236.                                                         if ((pp[0].x>0)&&(pp[0].y>0)&&(dist(pp[1],pp[0])<220)&&(ckx==BST_CHECKED))
  237.                                                                 Polyline(hdcWindow,pp ,2);
  238.                                                         //Se la spunta non è attivata, allora disegna solo un punto
  239.                                                         if (ckx!=BST_CHECKED)
  240.                                                            DrawBall(hdcWindow);
  241.                                             //pp[0] riassume il valore attuale delle coordinate e si ripete il ciclo
  242.                                                         pp[0].x=ballX;
  243.                                                         pp[0].y=ballY;
  244.                                                 }
  245.                                                 //CIò SI RIPETE UGUALMENTE PER L'ALTRA EQUAZIONE*/
  246.                                                 if (fabs(result3-result4)<=Mx/1000)
  247.                                                 {
  248.                                                         ballX=conx(x,Mx);
  249.                                                         ballY=cony(y,My);
  250.                                                         p[1].x=ballX;
  251.                                                         p[1].y=ballY;
  252.                                                         if ((p[0].x>0)&&(p[0].y>0)&&(dist(p[1],p[0])<220)&&(ckx==BST_CHECKED))
  253.                                                                 Polyline(hdcWindow,p ,2);
  254.                                                         if (ckx!=BST_CHECKED)
  255.                                                            DrawBall(hdcWindow);
  256.                             p[0].x=ballX;
  257.                                                         p[0].y=ballY;
  258.                                                 }
  259.                                                 /*SE PER LA COPPIA X-Y VENGONO VERIFICATE TUTTE LE EQUAZIONI, SIAMO DI FRONTE A UN PUNTO DI
  260.                                                  *INTERSEZIONE*/
  261.                                                 if ((fabs(result-result2)<=Mx/1000)&&(fabs(result3-result4)<=Mx/1000))
  262.                                                 {
  263.                                                         sprintf(pax, "%6.2f", x);   //Converto da float a *char
  264.                                                         sprintf(pay, "%6.2f", y);       //Converto da float a *char
  265.                                                         TextOut(hdcWindow,conx(x,Mx),cony(y,My),pax,sizeof(pax));
  266.                                                         TextOut(hdcWindow,conx(x,Mx)+40,cony(y,My),pay,sizeof(pay));
  267.                                                 }
  268.                                         }
  269.                                         else
  270.                                         //CICLO INVERSO:
  271.                                         for (j=My;j>=-My;j=j-(My*2)/prec)
  272.                                         {
  273.                                                 //FUNZIONA UGUALE A QUELLO PRECEDENTE
  274.                                                 y=j;
  275.                                                 result=SolveRPN(rpn,x,y);
  276.                                                 result2=SolveRPN(rpn2,x,y);
  277.                                                 result3=SolveRPN(rpn3,x,y);
  278.                                                 result4=SolveRPN(rpn4,x,y);
  279.                                                 if (fabs(result-result2)<=Mx/1000)
  280.                                                 {
  281.                                                         ballX=conx(x,Mx);
  282.                                                         ballY=cony(y,My);
  283.                                                         pp[1].x=ballX;
  284.                                                         pp[1].y=ballY;
  285.                                                         if ((pp[0].x>0)&&(pp[0].y>0)&&(dist(pp[1],pp[0])<220&&(ckx==BST_CHECKED)))
  286.                                                                 Polyline(hdcWindow,pp ,2);
  287.                                                         if (ckx!=BST_CHECKED)
  288.                                                            DrawBall(hdcWindow);
  289.                             pp[0].x=ballX;
  290.                                                         pp[0].y=ballY;
  291.                                                 }
  292.                                                 if (fabs(result3-result4)<=Mx/1000)
  293.                                                 {
  294.                                                         ballX=conx(x,Mx);
  295.                                                         ballY=cony(y,My);
  296.                                                         p[1].x=ballX;
  297.                                                         p[1].y=ballY;
  298.                                                         if ((p[0].x>0)&&(p[0].y>0)&&(dist(p[1],p[0])<220)&&(ckx==BST_CHECKED))
  299.                                                                 Polyline(hdcWindow,p ,2);
  300.                                                         if (ckx!=BST_CHECKED)
  301.                                                             DrawBall(hdcWindow);
  302.                             p[0].x=ballX;
  303.                                                         p[0].y=ballY;
  304.                                                 }
  305.                                                 if ((fabs(result-result2)<=Mx/1000)&&(fabs(result3-result4)<=Mx/1000))
  306.                                                 {
  307.                                                         sprintf(pax, "%6.2f", x);   //Converto da float a *char
  308.                                                         sprintf(pay, "%6.2f", y);       //Converto da float a *char
  309.                                                         TextOut(hdcWindow,conx(x,Mx),cony(y,My),pax,sizeof(pax));
  310.                                                         TextOut(hdcWindow,conx(x,Mx)+40,cony(y,My),pay,sizeof(pay));
  311.                                                 }
  312.                                         }
  313.                                 }
  314.                                 //Una volta disegnate le equazioni, i due "POINT" vengono azzerati, per evitare
  315.                                 //Che il successivo sistema si colleghi a quello vecchio.
  316.                                 pp[0].x=0;
  317.                                 pp[0].y=0;
  318.                                 p[0].x=0;
  319.                                 p[0].y=0;
  320.                         }
  321.                         /*DISEGNO GRAFICO DA FILE **************************************************************/
  322.                         if (agg>0)
  323.                         {
  324.                   fstream ll,kk;
  325.                   agg=0;
  326.                   kk.open("X.txt",ios::in);
  327.                   //Controllo l'esistenza del file
  328.                   if(kk==NULL)
  329.                   {
  330.                       MessageBox(hwnd,"'X.txt' è stato rinominato o eliminato.Controllare.",
  331.                       "Errore!",MB_ICONERROR);
  332.                       return 1;
  333.                   }
  334.                   Mx=0;
  335.                   //Ciclo che individua il massimo dei valori
  336.                   while (!kk.eof())
  337.                   {
  338.                         kk >>x;
  339.                         if (fabs(x)>Mx)
  340.                            Mx=fabs(x);
  341.                   }
  342.                   kk.close();
  343.                  
  344.                   ll.open("Y.txt",ios::in);
  345.                   //Controllo l'esistenza del file
  346.                   if(ll==NULL)
  347.                   {
  348.                       MessageBox(hwnd,"'Y.txt' è stato rinominato o eliminato.Controllare.",
  349.                       "Errore!",MB_ICONERROR);
  350.                       return 1;
  351.                   }
  352.                   My=0;
  353.                   //Ciclo che individua il massimo dei valori
  354.                   while (!ll.eof())
  355.                   {
  356.                         ll >>y;
  357.                         if (fabs(y)>My)
  358.                            My=fabs(y);
  359.                   }
  360.                   ll.close();
  361.                  
  362.                  
  363.                   fstream ff,gg;
  364.                   ff.open("X.txt",ios::in);
  365.                   gg.open("Y.txt",ios::in);
  366.                   //Mentre i file "X.txt" e "Y.txt" non finiscono
  367.                   //Legge i valori x e y.
  368.                   while ((!ff.eof())&&(!gg.eof()))
  369.                   {
  370.                         ff >>x;
  371.                         gg >>y;
  372.                         ballX=conx(x,Mx);
  373.                         ballY=cony(y,My);
  374.                         //Se è stato cliccato su "collega con linee"
  375.                         //lo collega con una linea usando lo stesso metodo per
  376.                         //i sistemi di equazioni.
  377.                         if (ck==BST_CHECKED)
  378.                         {
  379.                             pp[1].x=ballX;
  380.                                                         pp[1].y=ballY;
  381.                                                         if ((pp[0].x>0)&&(pp[0].y>0))
  382.                                                                 Polyline(hdcWindow,pp ,2);
  383.                             pp[0].x=ballX;
  384.                                                         pp[0].y=ballY;
  385.                         }
  386.                         //Altrimenti  colloca solo i punti.
  387.                         if (ck!=BST_CHECKED)
  388.                             DrawBall(hdcWindow);
  389.                   }
  390.                  
  391.                   ff.close();
  392.                   gg.close();
  393.             }
  394.             pp[0].x=0;
  395.             pp[0].y=0;
  396.                         ReleaseDC(hwnd, hdcWindow);
  397.                   }    
  398.                   break;
  399.       case WM_COMMAND: /***************************************MENU*****************************************************/
  400.                    if (LOWORD(wParam)==MENU_FILE_EXIT)
  401.                    {
  402.                            DestroyWindow(hwnd);    //Chiudo la finestra
  403.                    }
  404.            if (LOWORD(wParam)==MENU_FILE_CURV)
  405.                    {
  406.                            //Crea e visualizza la finestra delle curve
  407.                            MSG messages2;
  408.                HWND hwnd2 = CreateWindowEx (0,name,"Grafico da File",WS_SYSMENU|WS_MINIMIZEBOX|WS_VISIBLE,400,400,355,300,hwnd, NULL, g_hInst,NULL);
  409.                ShowWindow (hwnd2, 1);
  410.                while (GetMessage (&messages2, NULL, 0, 0))
  411.                            {
  412.                        TranslateMessage(&messages2);
  413.                        DispatchMessage(&messages2);
  414.                            }
  415.                    }
  416.                    if (LOWORD(wParam)==MENU_FILE_ABOU)
  417.                    {
  418.                            MessageBox(hwnd,"Paint XY -SISTEMI & FILE- Versione 1.1","By Dany",MB_OK|MB_ICONINFORMATION);
  419.                    }
  420.                    if (LOWORD(wParam)==MENU_FILE_CLEA)
  421.                    {
  422.                
  423.                    ShowWindow(hwnd,0);
  424.                            ShowWindow(hwnd,1);
  425.                            Mx=0;My=0;
  426.                    }
  427.                    if (LOWORD(wParam)==MENU_FILE_FUNZ)
  428.                    {
  429.                            //Crea e visualizza la finestra
  430.                            MSG messages5;
  431.                HWND hwnd5 = CreateWindowEx (0,name2,"Sistema:",WS_SYSMENU|WS_MINIMIZEBOX|WS_VISIBLE,400,400,400,350,hwnd, NULL, g_hInst,NULL);
  432.                            ShowWindow (hwnd5, 1);
  433.                            while (GetMessage (&messages5, NULL, 0, 0))
  434.                            {
  435.                        TranslateMessage(&messages5);
  436.                        DispatchMessage(&messages5);
  437.                            }
  438.                    }
  439.                    if (LOWORD(wParam)==MENU_FILE_HELP)
  440.                    {
  441.                system("Guida.txt");
  442.            }
  443.            break;
  444.       case WM_PAINT:
  445.             PAINTSTRUCT ps;
  446.             HDC hdcWindow;
  447.             hdcWindow = BeginPaint(hwnd, &ps);
  448.             DrawGriglia(hdcWindow);        //Ridisegno la griglia se la finestra viene ridotta a icona/coperta
  449.                        
  450.             EndPaint(hwnd, &ps);
  451.                  break;
  452.                  case WM_MOUSEMOVE:
  453.                          /*Coordinate mouse nella finestra
  454.                          *In pratica Grazie a GET_X_LPARAM (lParam); e a GET_Y_LPARAM (lParam);
  455.                          *Ottengo le coordinate in pixel dentro la finestra, per poi convertirle
  456.                          *In coordinate reali.*/
  457.                          cox = GET_X_LPARAM (lParam);
  458.                          coy = GET_Y_LPARAM (lParam);
  459.  
  460.                          cox=(cox-500)/(500/Mx);
  461.                          coy=(500-coy)/(500/My);
  462.  
  463.                          sprintf(cux, "%6.2f", cox);   //Converto da float a *char
  464.                          sprintf(cooy, "%6.2f", coy);   //Converto da float a *char
  465.                          SetWindowText(lix,cux);   //Imposto nell'edit le coordinate x
  466.                          SetWindowText(ley,cooy);   //Imposto nell'edit le coordinate y
  467.                          break;
  468.                 case WM_KEYDOWN:
  469.                          if (LOWORD(wParam)==VK_DELETE)   //Pulizia istantanea se si preme  CANC
  470.                          {
  471.                            ShowWindow(hwnd,0);
  472.                            ShowWindow(hwnd,1);
  473.                            Mx=0;My=0;
  474.                          }
  475.                          break;
  476.       case WM_CLOSE:
  477.                   if(MessageBox(hwnd,"Vuoi davvero uscire?","Messaggio",MB_YESNO|MB_ICONQUESTION)==IDYES)
  478.          DestroyWindow(hwnd);
  479.          break;
  480.       case WM_DESTROY:
  481.          KillTimer(hwnd, idTimer1);
  482.          DeleteObject(hbmBall);
  483.          DeleteObject(hbmMask);
  484.          PostQuitMessage(0);
  485.          break;
  486.       default:
  487.          return DefWindowProc(hwnd, Message, wParam, lParam);
  488.    }
  489.    return 0;
  490. }
  491. //WndProc della finestra per rappresentare Grafici da file
  492. //HWND
  493. HWND rodo;
  494. HWND avvia,line,xtxt,ytxt;
  495. //WndProc della finestra "grafici da file"
  496. LRESULT CALLBACK WindowProcedure2 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  497. {              
  498.         InitCommonControls ();      //Applicazione visual style
  499.         switch (message)                  
  500.         {
  501.                         case WM_PAINT:
  502.                                 PAINTSTRUCT ps;
  503.                                 HDC hdcWindow;
  504.                                 hdcWindow = BeginPaint(hwnd, &ps);
  505.                                 /****SETTO I CARATTERI DA UTILIZZARE: FONT, DIMENSIONI ECC...**/
  506.                                 hfont = CreateFont (22,0,0,0, 500, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
  507.                 CLIP_DEFAULT_PRECIS, 5, VARIABLE_PITCH, TEXT ("Monotype Corsiva"));
  508.                             SelectObject (hdcWindow, hfont);
  509.                             SetRect (& rect, 100,100,700,200);
  510.                 SetTextColor (hdcWindow, RGB (0,0,0));
  511.                 TextOut(hdcWindow,0,0,"Crea un Grafico con i valori nei file 'X.txt' e 'Y.txt'",55);
  512.                                 EndPaint(hwnd,&ps);
  513.                                 break;
  514.             case WM_CREATE:  
  515.                 //Bottoni
  516.                                 avvia=CreateWindow("Button","AVVIA!",WS_VISIBLE|WS_CHILD,77,100,200,50,hwnd,(HMENU)1,NULL,NULL);
  517.                                 xtxt=CreateWindow("Button","X.txt",WS_VISIBLE|WS_CHILD,102,180,60,60,hwnd,(HMENU)2,NULL,NULL);
  518.                                 ytxt=CreateWindow("Button","Y.txt",WS_VISIBLE|WS_CHILD,202,180,60,60,hwnd,(HMENU)3,NULL,NULL);
  519.                 //Checkbox
  520.                 line=CreateWindow("Button","Collega con linee",WS_VISIBLE|WS_CHILD|BS_AUTOCHECKBOX,117,50,200,25,hwnd,NULL,NULL,NULL);
  521.                 /****SETTO I CARATTERI DA UTILIZZARE: FONT, DIMENSIONI ECC...**/
  522.                                 GetObject (GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  523.                                 hfont = CreateFont (20,0,0,0, 500, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
  524.                 CLIP_DEFAULT_PRECIS, 5, VARIABLE_PITCH, TEXT ("Monotype Corsiva"));
  525.                 SendMessage (avvia, WM_SETFONT, (WPARAM) hfont, true);
  526.                 SendMessage (line, WM_SETFONT, (WPARAM) hfont, true);
  527.                 SendMessage (xtxt, WM_SETFONT, (WPARAM) hfont, true);
  528.                 SendMessage (ytxt, WM_SETFONT, (WPARAM) hfont, true);
  529.                 break;
  530.                         case WM_COMMAND:
  531.                  if (LOWORD(wParam)==1)
  532.                  {
  533.                     //Controlla lo stato della checkbox
  534.                     ck=SendMessage(line, BM_GETCHECK, 0,0);
  535.                     agg++;           //è stato premuto il tasto AVVIA!
  536.                  }
  537.                  if (LOWORD(wParam)==2)          //apre x.txt
  538.                     system ("start X.txt");
  539.                  if (LOWORD(wParam)==3)
  540.                     system ("start Y.txt");      //apre y.txt
  541.                    
  542.                                 break;
  543.             case WM_DESTROY:
  544.                 PostQuitMessage (0);    
  545.                 break;
  546.             default:                  
  547.                 return DefWindowProc (hwnd, message, wParam, lParam);
  548.         }
  549.         return 0;
  550. }
  551. //Function Window
  552. //Variabile del ciclo
  553. int h;
  554. //HWND
  555. HWND edit,edit2,edit3,edit4,button,sbox;
  556. HWND maxx,maxy,acc;
  557.  
  558. HBITMAP maskgraf,graf;
  559. //Disegna la parentesi graffa
  560. void DrawGraf(HDC hdc)
  561. {
  562.      HDC hdcMemory=0;
  563.      hdcMemory=CreateCompatibleDC(hdc);
  564.          SelectObject(hdcMemory, maskgraf);
  565.          BitBlt(hdc, 15, 30, 25, 60, hdcMemory, 0, 0, SRCAND);
  566.          SelectObject(hdcMemory, graf);
  567.      BitBlt(hdc, 15, 30, 25, 60, hdcMemory, 0, 0, SRCPAINT);
  568.      DeleteDC(hdcMemory);
  569. }
  570. //Window Procedure della System Window
  571. LRESULT CALLBACK WindowProcedure3 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  572. {      
  573.         InitCommonControls ();     //Applicazione visual style
  574.         switch(message)
  575.         {
  576.                 case WM_CREATE:
  577.             //Inizializzo la graffa
  578.                 graf = LoadBitmap(g_hInst, "graf");
  579.                         maskgraf = LoadBitmap(g_hInst, "maskgra");
  580.                         /*CHECKBOX*/
  581.                         sbox=CreateWindow("Button","Collega con linee",WS_VISIBLE|WS_CHILD|BS_AUTOCHECKBOX,170,165,150,25,hwnd,NULL,NULL,NULL);
  582.                         /*BARRA*/
  583.                         prog=CreateWindowEx(NULL, PROGRESS_CLASS, NULL, WS_CHILD|WS_VISIBLE, 20,288, 350, 30, hwnd, NULL, NULL, 0);
  584.                         /*EDIT*/
  585.                     edit=CreateWindow("edit","<primo membro>",WS_VISIBLE|WS_CHILD|WS_BORDER|EM_GETLINE,55,30,150,20,hwnd,0,0,0);
  586.                         edit2=CreateWindow("edit","<secondo membro>",WS_VISIBLE|WS_CHILD|WS_BORDER|EM_GETLINE,219,30,150,20,hwnd,0,0,0);
  587.                         edit3=CreateWindow("edit","<primo membro>",WS_VISIBLE|WS_CHILD|WS_BORDER|EM_GETLINE,55,70,150,20,hwnd,0,0,0);
  588.                         edit4=CreateWindow("edit","<secondo membro>",WS_VISIBLE|WS_CHILD|WS_BORDER|EM_GETLINE,219,70,150,20,hwnd,0,0,0);
  589.                         /*BUTTON*/
  590.                         button=CreateWindow("Button","CALCOLA!",WS_VISIBLE|WS_CHILD,4,220,385,50,hwnd,(HMENU)2,NULL,NULL);
  591.                         /*MIN E MAX X E Y E PRECISIONE*/
  592.                         maxx=CreateWindow("edit","",WS_VISIBLE|WS_CHILD|WS_BORDER,95,130,60,20,hwnd,NULL,NULL,NULL);
  593.                         maxy=CreateWindow("edit","",WS_VISIBLE|WS_CHILD|WS_BORDER,240,130,60,20,hwnd,NULL,NULL,NULL);
  594.                         acc=CreateWindow("edit","300",WS_VISIBLE|WS_CHILD|WS_BORDER,95,165,50,20,hwnd,NULL,NULL,NULL);
  595.                         /****SETTO I CARATTERI DA UTILIZZARE: FONT, DIMENSIONI ECC...**/
  596.                         GetObject (GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  597.                         hfont = CreateFont (20,0,0,0, 500, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
  598.             CLIP_DEFAULT_PRECIS, 5, VARIABLE_PITCH, TEXT ("Monotype Corsiva"));
  599.                         SendMessage (edit, WM_SETFONT, (WPARAM) hfont, true);
  600.                         SendMessage (edit2, WM_SETFONT, (WPARAM) hfont, true);
  601.                         SendMessage (button, WM_SETFONT, (WPARAM) hfont, true);
  602.                         SendMessage (edit3, WM_SETFONT, (WPARAM) hfont, true);
  603.                         SendMessage (edit4, WM_SETFONT, (WPARAM) hfont, true);
  604.                         SendMessage (maxx, WM_SETFONT, (WPARAM) hfont, true);
  605.                         SendMessage (maxy, WM_SETFONT, (WPARAM) hfont, true);
  606.                         SendMessage (acc, WM_SETFONT, (WPARAM) hfont, true);
  607.                         SendMessage (sbox, WM_SETFONT, (WPARAM) hfont, true);
  608.                 break;
  609.                 case WM_PAINT:
  610.                         PAINTSTRUCT ps;
  611.                         HDC hdcWindow;
  612.                         hdcWindow = BeginPaint(hwnd, &ps);
  613.                         /****SETTO I CARATTERI DA UTILIZZARE: FONT, DIMENSIONI ECC...**/
  614.                         hfont = CreateFont (20,0,0,0, 500, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
  615.             CLIP_DEFAULT_PRECIS, 5, VARIABLE_PITCH, TEXT ("Monotype Corsiva"));
  616.                         SelectObject (hdcWindow, hfont);
  617.                         SetRect (& rect, 100,100,700,200);
  618.             SetTextColor (hdcWindow, RGB (0,0,0));
  619.                         DrawGraf(hdcWindow);
  620.                         TextOut(hdcWindow,1,1,"Comporre il sistema desiderato.",31);
  621.                         TextOut(hdcWindow,206,30,"=",1);
  622.                         TextOut(hdcWindow,206,70,"=",1);
  623.                         TextOut(hdcWindow,25,130,"max x =",7);
  624.                         TextOut(hdcWindow,190,130,"max y =",7);
  625.                         TextOut(hdcWindow,25,165,"precisione",10);
  626.                         EndPaint(hwnd,&ps);
  627.                         break;
  628.                 case WM_COMMAND:
  629.                         if (LOWORD(wParam)==2)
  630.                         {
  631.                                 funpress++;                //Indica che è stato premuto il tasto CALCOLA!
  632.                                 //Inizializzo gli array che contengono le espressioni
  633.                                 char in[100]="",in2[100]="",in3[100]="",in4[100]="";
  634.                                 //Prendo il testo negli edit e lo metto negli array
  635.                                 GetWindowText(edit,in,100);
  636.                                 GetWindowText(edit2,in2,100);
  637.                                 GetWindowText(edit3,in3,100);
  638.                                 GetWindowText(edit4,in4,100);
  639.                                 //Mi trovo la notazione polacca inversa (RPN)
  640.                                 rpn=FoundRPN(in);
  641.                                 rpn2=FoundRPN(in2);
  642.                                 rpn3=FoundRPN(in3);
  643.                                 rpn4=FoundRPN(in4);
  644.                                 //leggo i massimi e i minimi e la precisione:
  645.                 //ne inizializzo gli array
  646.                                 char minix[6]="",maxix[6]="",miniy[6]="",maxiy[6]="",accu[5]="";
  647.                                 //Li catturo dagli edit
  648.                                 GetWindowText(acc,accu,5);
  649.                                 GetWindowText(maxx,maxix,6);
  650.                                 GetWindowText(maxy,maxiy,6);
  651.                                 //Converto le string in float.
  652.                                 Mx=atof(maxix);
  653.                                 My=atof(maxiy);
  654.                                 prec=atoi(accu);
  655.                                 //Controllo lo stato della checkbox
  656.                                 ckx=SendMessage(sbox, BM_GETCHECK, 0,0);
  657.                         }
  658.                         break;
  659.                 case WM_DESTROY:
  660.                                 PostQuitMessage(0);
  661.                                 break;
  662.                 default:
  663.                         return DefWindowProc(hwnd,message,wParam,lParam);
  664.         }
  665.         return 0;
  666. }
  667.  
  668. //Creazione finestra principale/Dichiarazione di hinstance per le altre finestre.
  669. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
  670. {
  671.    InitCommonControls ();      //Applicazione visual style
  672.    WNDCLASSEX WndClass,WndClass2,WndClass3;
  673.    HWND hwnd;
  674.    MSG Msg;
  675.    
  676.    g_hInst = hInstance;
  677.    //Main Window
  678.    WndClass.cbSize        = sizeof(WNDCLASSEX);
  679.    WndClass.style         = 0;
  680.    WndClass.lpfnWndProc   = WndProc;
  681.    WndClass.cbClsExtra    = 0;
  682.    WndClass.cbWndExtra    = 0;
  683.    WndClass.hInstance     = g_hInst;
  684.    WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  685.    WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  686.    WndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
  687.    WndClass.lpszMenuName  = NULL;
  688.    WndClass.lpszClassName = g_szClassName;
  689.    WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  690.    //Da File Window
  691.    WndClass2.cbSize        = sizeof(WNDCLASSEX);
  692.    WndClass2.style         = 0;
  693.    WndClass2.lpfnWndProc   = WindowProcedure2;
  694.    WndClass2.cbClsExtra    = 0;
  695.    WndClass2.cbWndExtra    = 0;
  696.    WndClass2.hInstance     = hInstance;
  697.    WndClass2.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  698.    WndClass2.hCursor       = LoadCursor(NULL, IDC_ARROW);
  699.    WndClass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
  700.    WndClass2.lpszMenuName  = NULL;
  701.    WndClass2.lpszClassName = name;
  702.    WndClass2.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  703.    //SystemWindow
  704.    WndClass3.cbSize        = sizeof(WNDCLASSEX);
  705.    WndClass3.style         = 0;
  706.    WndClass3.lpfnWndProc   = WindowProcedure3;
  707.    WndClass3.cbClsExtra    = 0;
  708.    WndClass3.cbWndExtra    = 0;
  709.    WndClass3.hInstance     = hInstance;
  710.    WndClass3.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  711.    WndClass3.hCursor       = LoadCursor(NULL, IDC_ARROW);
  712.    WndClass3.hbrBackground = (HBRUSH)(15);
  713.    WndClass3.lpszMenuName  = NULL;
  714.    WndClass3.lpszClassName = name2;
  715.    WndClass3.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  716.  
  717.    //Main Window
  718.    if(!RegisterClassEx(&WndClass))
  719.    {
  720.       MessageBox(0, "Window1 Registration Failed!", "Error!",
  721.          MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
  722.       return 0;
  723.    }
  724.    //Da file Window
  725.     if(!RegisterClassEx(&WndClass2))
  726.    {
  727.       MessageBox(0, "Window2 Registration Failed!", "Error!",
  728.          MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
  729.       return 0;
  730.    }
  731.    //System Window
  732.     if(!RegisterClassEx(&WndClass3))
  733.    {
  734.       MessageBox(0, "Window3 Registration Failed!", "Error!",
  735.          MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
  736.       return 0;
  737.    }
  738.    //CREAZIONE:
  739.    //Main Window
  740.    
  741.    hwnd = CreateWindowEx(
  742.    WS_EX_CLIENTEDGE,
  743.    g_szClassName,
  744.    "Paint XY- Sistemi -",
  745.    WS_SYSMENU|WS_MINIMIZEBOX|WS_VISIBLE,
  746.    175, 0, 1011, 1000,
  747.    NULL, NULL, g_hInst, NULL);
  748.      
  749.     //incaso di errore
  750.    if (hwnd == NULL)
  751.    {
  752.       MessageBox(0, "Window Creation Failed!", "Error!",
  753.          MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
  754.       return 0;
  755.    }
  756.    //Visualizza Main Window
  757.    ShowWindow(hwnd, nCmdShow);
  758.    UpdateWindow(hwnd);
  759.    //Messaggio da Main Window
  760.    while(GetMessage(&Msg, NULL, 0, 0))
  761.    {
  762.       TranslateMessage(&Msg);
  763.       DispatchMessage(&Msg);
  764.    }
  765.    return Msg.wParam;
  766. }