home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 32 Periodic / 32-Periodic.zip / revue1b.zip / EXEMPLE1.CPP < prev    next >
Text File  |  1993-11-28  |  6KB  |  251 lines

  1.  #define INCL_PM
  2.  #define INCL_DOS)
  3.  #define INCL_WINDIALOGS
  4.  #define INCL_GPI
  5.  #define INCL_GPIPRIMITIVES
  6.  /*********************************
  7.     changer le répertoire suivant
  8.     change the next directory
  9.  *********************************/
  10.  #include "c:\data\programme\exemple1\exemple1.h"
  11.  #include <math.h>
  12.  #include <stdio.h>
  13.  #include <stdlib.h>
  14.  #include <mem.h>
  15.  #include <os2.h>
  16.  
  17. HWND hwndTrace;
  18. int hypote=0;
  19. PFNWP GenericDlgProc;
  20. double x = 0;
  21. double y = 0;
  22. double resultat = 0;
  23.  
  24.  
  25. MRESULT EXPENTRY WinPrincipal( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  26. MRESULT EXPENTRY WinTrace( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  27. MRESULT EXPENTRY WinCalcul ( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  28. hypotenuse(HWND hwnd, int hypote);
  29.  
  30.  
  31. hypotenuse(HWND hwnd, int hypote)
  32. {
  33.   HWND hEntree;
  34.   char Buffer[50];
  35.  
  36.   memset(Buffer, 0, sizeof(Buffer));
  37.   hEntree = WinWindowFromID(hwnd, ID_CALCUL_Xin);
  38.   WinQueryWindowText(hEntree, sizeof(Buffer), Buffer);
  39.   x = atof(Buffer);
  40.  
  41.   memset(Buffer, 0, sizeof(Buffer));
  42.   hEntree = WinWindowFromID(hwnd, ID_CALCUL_Yin);
  43.   WinQueryWindowText(hEntree, sizeof(Buffer), Buffer);
  44.   y = atof(Buffer);
  45.  
  46.   switch (hypote)
  47.   {
  48.     case 0:
  49.       resultat = hypot(x, y);
  50.       break;
  51.  
  52.     case 1:
  53.       resultat = pow(x, y);
  54.       break;
  55.  
  56.     default:
  57.       break;
  58.   }
  59.       sprintf(Buffer,"%f", resultat);
  60.       hEntree = WinWindowFromID(hwnd, ID_CALCUL_OUT);
  61.       WinSetWindowText(hEntree, Buffer);
  62.   return (0);
  63. }
  64.  
  65.  
  66.  MRESULT EXPENTRY WinPrincipal( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  67.  {
  68.      switch( msg )
  69.      {
  70.     case WM_CREATE: /* crée la fenêtre */
  71.     break;
  72.  
  73.     case WM_COMMAND:
  74.     switch (SHORT1FROMMP(mp1))
  75.         {
  76.         case ID_MENU_HYP:
  77.           x = 0; y = 0;
  78.           WinSetWindowPos(hwndTrace,
  79.             0, 328,8, 160,208,
  80.             SWP_SIZE | SWP_MOVE | SWP_SHOW);
  81.  
  82.           hypote=0;
  83.           WinDlgBox(HWND_DESKTOP, hwnd, WinCalcul,
  84.               NULLHANDLE, ID_HYPOTE, NULL);
  85.         return (MRESULT) TRUE;
  86.  
  87.         case ID_MENU_EXP:
  88.           WinSetWindowPos(hwndTrace,
  89.             0, 0,0, 160,208, SWP_HIDE);
  90.           hypote=1;
  91.           WinDlgBox(HWND_DESKTOP, hwnd, WinCalcul,
  92.               NULLHANDLE, ID_HYPOTE, NULL);
  93.         return (MRESULT) TRUE;
  94.  
  95.         case ID_TERMINE:
  96.           if(WinMessageBox(HWND_DESKTOP, hwnd,
  97.              "Voulez-vous vraiment quitter cette application?",
  98.              "Juste pour être certain...", 0,
  99.              MB_YESNO | MB_QUERY)
  100.            == MBID_YES) WinPostMsg(hwnd,WM_CLOSE,MPVOID,MPVOID); 
  101.         break;
  102.  
  103.         case ID_ABOUT:
  104.           WinDlgBox(HWND_DESKTOP, hwnd, GenericDlgProc,
  105.             0L, IDD_ABOUT, NULL);
  106.         return (MRESULT) TRUE;
  107.  
  108.         default:
  109.         return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  110.         }
  111.  
  112.     case WM_ERASEBACKGROUND:
  113.        return(MRESULT)(TRUE);
  114.  
  115.     default:
  116.        return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  117.      }
  118.    return (0);
  119.  }
  120.  
  121.  MRESULT EXPENTRY WinTrace( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  122.  {
  123.      POINTL aptl[4];
  124.      RECTL rcl;
  125.      HPS hps;
  126.  
  127.      WinInvalidateRegion( hwnd, (HRGN)NULL, FALSE);
  128.      switch( msg )
  129.      {
  130.     case WM_CREATE: /* crée la fenêtre */
  131.     break;
  132.  
  133.     case WM_ERASEBACKGROUND:
  134.     return(MRESULT)(TRUE);
  135.  
  136.     case WM_PAINT:
  137.         if (hypote == 0)
  138.           {
  139.             if ((fabs(y) >= fabs(x)) && (y != 0))
  140.               {
  141.              x = fabs(130 * (x / y));
  142.              y = 130;
  143.               }
  144.             else
  145.               if ((fabs(x) > fabs(y)) && (x != 0))
  146.                {
  147.              y = fabs(130 * (y / x));
  148.              x = 130;
  149.                }
  150.             hps=WinBeginPaint(hwnd, (HPS)NULL, &rcl);
  151.             aptl[0].x=10; aptl[0].y=12;
  152.             aptl[1].x=(x+10); aptl[1].y=12;
  153.             aptl[2].x=(x+10); aptl[2].y=(y+10);
  154.             aptl[3].x=10; aptl[3].y=12;
  155.             GpiMove(hps, aptl);
  156.             GpiPolyLine(hps,sizeof(aptl) / sizeof(POINTL),aptl);
  157.             WinEndPaint(hps);
  158.           }
  159.  
  160.     break;
  161.     default : return WinDefWindowProc( hwnd, msg, mp1, mp2);
  162.      }
  163.      return(0);
  164.  }
  165.  
  166. MRESULT EXPENTRY WinCalcul ( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  167. {
  168.   HWND hEntree;
  169.   double x, y, resultat;
  170.   char Buffer[50];
  171.  
  172.   switch( msg )
  173.   {
  174.     case WM_INITDLG:
  175.       hEntree = WinWindowFromID(hwnd, ID_CALCUL_STITRE);
  176.       if (hypote == 0)
  177.         WinSetWindowText(hEntree, "hypoténuse");
  178.       else
  179.         WinSetWindowText(hEntree, "Exposant");
  180.     return (MRESULT) FALSE;
  181.  
  182.     case WM_COMMAND:
  183.       switch(SHORT1FROMMP(mp1))
  184.       {
  185.         case ID_CALCUL_OK:
  186.         WinInvalidateRegion( hwndTrace, (HRGN)NULL, FALSE);
  187.         hypotenuse(hwnd, hypote);
  188.         return (MRESULT) TRUE;
  189.  
  190.         case ID_CALCUL_AIDE:
  191.         {
  192.           for (int i=1;i < 8; ++i) DosBeep(i*100,20*i);
  193.         }
  194.         return (MRESULT) TRUE;
  195.  
  196.         case ID_CALCUL_TERMINE:
  197.            WinSetWindowPos(hwndTrace,
  198.             0, 0,0, 160,208, SWP_HIDE);
  199.            WinPostMsg(hwnd,WM_CLOSE,MPVOID,MPVOID);
  200.         break;
  201.  
  202.         default: break;
  203.       }
  204.     default:
  205.     break;
  206.   }
  207.        return (MRESULT) WinDefDlgProc(hwnd, msg, mp1, mp2);
  208. }
  209.  
  210.  int main(void)
  211.  {
  212.      HAB hab;
  213.      HMQ  hmq;
  214.      HWND hwndClient;
  215.      HWND hwndFrame;
  216.      QMSG qmsg;
  217.      ULONG flCreate;
  218.      char titre1[] = "Calcul";
  219.  
  220.      hab = WinInitialize(0);
  221.      if (!hab) return 1;
  222.  
  223.      hmq = WinCreateMsgQueue(hab, 0);
  224.      if (!hmq) return 1;
  225.  
  226.      WinRegisterClass( hab, (PSZ)"Principale", WinPrincipal, CS_SIZEREDRAW, 0);
  227.  
  228.      WinRegisterClass( hab, (PSZ)"Secondaire", WinTrace, CS_SIZEREDRAW, 0);
  229.       flCreate = FCF_DLGBORDER | FCF_SYSMENU | FCF_SHELLPOSITION |
  230.          FCF_TITLEBAR | FCF_MENU;
  231.  
  232.      hwndFrame = WinCreateStdWindow(HWND_DESKTOP, 0, &flCreate,
  233.     "Principale", titre1, 0L, NULLHANDLE, 100, &hwndClient);
  234.  
  235.      WinSetWindowPos(hwndFrame, HWND_TOP, 0,0, 500,300, SWP_SIZE | SWP_SHOW);
  236.  
  237.      flCreate = FCF_DLGBORDER;
  238.  
  239.      hwndTrace = WinCreateStdWindow(hwndFrame, 0, &flCreate, "Secondaire",
  240.     0, 0L, NULLHANDLE, 0L, &hwndTrace);
  241.  
  242.      while( WinGetMsg( hab, &qmsg, 0L, 0, 0 ) )
  243.        WinDispatchMsg( hab, &qmsg );
  244.  
  245.      WinDestroyWindow(hwndFrame);
  246.      WinDestroyWindow(hwndTrace);
  247.      WinDestroyMsgQueue( hmq );
  248.      WinTerminate( hab );
  249.      return 0;
  250.  }
  251.