DIALOGS.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:10k
源码类别:

Windows编程

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // This is a part of the Microsoft Source Code Samples. 
  3. // Copyright (C) 1993-1997 Microsoft Corporation.
  4. // All rights reserved. 
  5. //  
  6. // This source code is only intended as a supplement to 
  7. // Microsoft Development Tools and/or WinHelp documentation.
  8. // See these sources for detailed information regarding the 
  9. // Microsoft samples programs.
  10. //-----------------------------------------------------------------------------
  11. /****************************************************************************
  12. *  dialogs.c -- sample program demonstrating Windows Sockets APIs.
  13. *
  14. *  Handles the dialog boxes for the WSOCK sample.
  15. *
  16. ****************************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h> /* atoi */
  19. #include <windows.h>
  20. #include "wsock.h"
  21. extern u_short portno; /* Which tcp port are we going to use? */
  22. extern char szBuff[80];
  23. /****************************************************************************
  24. *
  25. *    FUNCTION: GetTcpPort(HWND, UINT, UINT, LONG)
  26. *
  27. *    PURPOSE: dialog callback procedure.  Allows the user to enter the
  28. *             tcp port number, or a service name.
  29. *
  30. ****************************************************************************/
  31. LRESULT APIENTRY GetTcpPort(
  32.         HWND hDlg,                /* window handle of the dialog box */
  33.         UINT message,             /* type of message                 */
  34.         UINT wParam,              /* message-specific information    */
  35.         LONG lParam)
  36. {
  37.    switch (message) {
  38.       case WM_INITDIALOG:                     /* message: initialize dialog box */
  39.         SetFocus( GetDlgItem( hDlg, IDD_EDIT));
  40.         return (TRUE);
  41.       case WM_COMMAND: {                    /* message: received a command */
  42.         switch(LOWORD(wParam)) {
  43.             case IDOK:                      /* "OK" box selected?          */
  44.             {
  45.                GetDlgItemText( hDlg, IDD_EDIT, szBuff, 80);
  46.                if ((portno = atoi(szBuff)) == 0)
  47.                   EndDialog( hDlg, 2 );
  48.                else
  49.                   EndDialog( hDlg, 1 );
  50.             }
  51.             break;
  52.          case IDCANCEL: /* System menu close command? */
  53.             EndDialog(hDlg, 0);           /* Exits the dialog box        */
  54.             break;
  55.          default:
  56.            return FALSE;
  57.          }
  58.                 return (TRUE);
  59.       }
  60.     }   /* switch message */
  61.     return (FALSE);                           /* Didn't process a message    */
  62.         UNREFERENCED_PARAMETER(lParam);
  63. }
  64. /****************************************************************************
  65. *
  66. *    FUNCTION: GetHostName(HWND, UINT, UINT, LONG)
  67. *
  68. *    PURPOSE: dialog callback procedure.  Allows the user to enter the
  69. *             the host name.  Used for menu item "Check Host Name" and
  70. *             also for menu item "Connect".
  71. *
  72. ****************************************************************************/
  73. LRESULT APIENTRY GetHostName(
  74.         HWND hDlg,                /* window handle of the dialog box */
  75.         UINT message,             /* type of message                 */
  76.         UINT wParam,              /* message-specific information    */
  77.         LONG lParam)
  78. {
  79.     switch (message) {
  80.        case WM_INITDIALOG:                     /* message: initialize dialog box */
  81.          SetFocus( GetDlgItem( hDlg, IDD_EDIT));
  82.          return (TRUE);
  83.        case WM_COMMAND: {                     /* message: received a command */
  84.           switch(LOWORD(wParam)) {
  85.             case IDOK:          /* "OK" box selected?        */
  86.              {
  87.               GetDlgItemText( hDlg, IDD_EDIT, szBuff, 80);
  88.               EndDialog(hDlg, TRUE);          /* Exits the dialog box        */
  89.              }
  90.              break;
  91.              case IDCANCEL:     /* System menu close command? */
  92.               EndDialog(hDlg, FALSE);         /* Exits the dialog box        */
  93.               break;
  94.              default:
  95.               return FALSE;
  96.            }
  97.         return (TRUE);
  98.       }
  99.     }   /* switch message */
  100.     return (FALSE);                           /* Didn't process a message    */
  101.         UNREFERENCED_PARAMETER(lParam);
  102. }
  103. /****************************************************************************
  104. *
  105. *    FUNCTION: About(HWND, UINT, UINT, LONG)
  106. *
  107. *    PURPOSE: dialog callback procedure for "about" box.
  108. *
  109. ****************************************************************************/
  110. LRESULT APIENTRY About(
  111.         HWND hDlg,                /* window handle of the dialog box */
  112.         UINT message,             /* type of message                 */
  113.         UINT wParam,              /* message-specific information    */
  114.         LONG lParam)
  115. {
  116.      switch (message) {
  117.         case WM_COMMAND:                         /* message: received a command */
  118.           if (LOWORD(wParam) == IDOK             /* "OK" box selected?          */
  119.                 || LOWORD(wParam) == IDCANCEL) { /* System menu close command?  */
  120.                         EndDialog(hDlg, TRUE);   /* Exits the dialog box        */
  121.           return (TRUE);        /* WM_COMMAND */
  122.      }
  123.      break;
  124.    } /* End switch message */
  125.    return (FALSE);                            /* Didn't process a message    */
  126.         UNREFERENCED_PARAMETER(lParam);
  127. }
  128. /****************************************************************************
  129. *
  130. *    FUNCTION: DisplayHostEnt(HWND, UINT, UINT, LONG)
  131. *
  132. *    PURPOSE: dialog callback procedure.  Displays the information
  133. *             returned by gethostbyname() (HOSTENT structure).
  134. *
  135. ****************************************************************************/
  136. LRESULT APIENTRY DisplayHostEnt(
  137.         HWND hDlg,                /* window handle of the dialog box */
  138.         UINT message,             /* type of message                 */
  139.         UINT wParam,              /* message-specific information    */
  140.         LONG lParam)
  141. {
  142.    DWORD ret;
  143.    switch (message) {
  144.         /*
  145.         *   Initialize dialog box
  146.         */
  147.         case WM_INITDIALOG:
  148.                 {
  149.                 int count = 0;
  150.                 SetWindowText( GetDlgItem( hDlg, IDD_HOSTNAME),  (LPCTSTR)phe->h_name);
  151.                 while (phe->h_aliases[count] != NULL) {
  152.                         SendDlgItemMessage(hDlg, IDD_LBALIAS, LB_ADDSTRING, 0, (LPARAM)(phe->h_aliases[count]));
  153.                         count++;
  154.                         }
  155.                 count = 0;
  156.                 /*
  157.                 *   Enumerate all the hosts IP addresses.
  158.                 */
  159.                 while (phe->h_addr_list[count] != NULL) {
  160.                     sprintf( szBuff, "%u.%u.%u.%u",
  161.                     (unsigned char) phe->h_addr_list[count][0],
  162.                     (unsigned char) phe->h_addr_list[count][1],
  163.                     (unsigned char) phe->h_addr_list[count][2],
  164.                     (unsigned char) phe->h_addr_list[count][3]);
  165.                     count++;
  166.                     /*
  167.                     *   Fill the dialog box..
  168.                     */
  169.                     if ((ret = SendDlgItemMessage(hDlg, IDD_LBADDR, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBuff)) == LB_ERR)
  170.                        MessageBox(hDlg, szBuff, "Couldn't add address..", MB_OK);
  171.                     }
  172.                 } /* while( more IP addresses ) */
  173.            return (TRUE);       /* WM_INITDIALOG */
  174.         /*
  175.         *   Received a command message
  176.         */
  177.         case WM_COMMAND:
  178.                 if (LOWORD(wParam) == IDOK) {
  179.                    EndDialog(hDlg, TRUE);             /* Exits the dialog box        */
  180.                    return TRUE;
  181.                 }
  182.                 return FALSE;   /* WM_COMMAND */
  183.     }   /* switch message */
  184.     return (FALSE);                                   /* Didn't process a message    */
  185.         UNREFERENCED_PARAMETER(lParam);
  186. } /* DisplayHostEnt */
  187. /****************************************************************************
  188. *
  189. *    FUNCTION: GetSendString(HWND, UINT, UINT, LONG)
  190. *
  191. *    PURPOSE: dialog callback procedure.  Allows the user to enter a
  192. *             string to be sent to the connected remote host.
  193. *
  194. ****************************************************************************/
  195. LRESULT APIENTRY GetSendString(
  196.         HWND hDlg,                /* window handle of the dialog box */
  197.         UINT message,             /* type of message                 */
  198.         UINT wParam,              /* message-specific information    */
  199.         LONG lParam)
  200. {
  201.     switch (message) {
  202.     case WM_INITDIALOG:                     /* message: initialize dialog box */
  203.         SetFocus( GetDlgItem( hDlg, IDD_EDIT));
  204.         return (TRUE);
  205.     case WM_COMMAND: {                     /* message: received a command */
  206.           switch(LOWORD(wParam)) {
  207.             case IDOK:          /* "OK" box selected?        */
  208.              {
  209.               /*
  210.               *   Store string in szBuff (global buffer)
  211.               */
  212.               GetDlgItemText( hDlg, IDD_EDIT, szBuff, 80);
  213.               EndDialog(hDlg, TRUE);          /* Exits the dialog box        */
  214.              }
  215.              break;
  216.              case IDCANCEL:     /* System menu close command? */
  217.               EndDialog(hDlg, FALSE);         /* Exits the dialog box        */
  218.               break;
  219.              default:
  220.               return FALSE;
  221.            }
  222.         return (TRUE);
  223.       }
  224.     }   /* switch message */
  225.     return (FALSE);                           /* Didn't process a message    */
  226.         UNREFERENCED_PARAMETER(lParam);
  227. } /* GetString */