home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / wpj_mag / wpjv1n4.zip / HELLO.ZIP / HELLO.C next >
C/C++ Source or Header  |  1993-03-15  |  12KB  |  401 lines

  1. /*============================================================================
  2.   Hello - Windows Programmer's Journal Volume 1 Number 3
  3.  
  4.   File      : Hello.C
  5.  
  6.   Prototype : 
  7.  
  8.   Call      : 
  9.  
  10.   Library   : 
  11.  
  12.   Example   : 
  13.  
  14.   7 February 1993 - dlcampbell
  15.   1993 Dave Campbell WynApse
  16.  
  17.   from:
  18.        Hello.C
  19.     By Pete Davis and Mike Wallace
  20.     Windows Programmer's Journal
  21.     Volume 1 Number 2
  22. ----------------------------------------------------------------------------*/
  23. #include <windows.h>
  24. #include "hello.h"
  25.  
  26. char   szAppName[] = "Hello";
  27. HANDLE hInst;
  28. HWND   hWndMain;
  29. int    InitSettings;
  30. static HMENU hMainMenu, hAlternateMenu;
  31.  
  32. /*----------------------------------------------------------------------------
  33.   Function Prototypes
  34. ----------------------------------------------------------------------------*/
  35. BOOL FAR PASCAL AboutDlgProc(HWND, WORD, WORD, LONG);
  36.  
  37. BOOL FAR PASCAL HelloDlgProc(HWND, WORD, WORD, LONG);
  38. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  39. BOOL            InitInstance(HANDLE);
  40.  
  41. /* The main procedure. This is called by Windows when your program is run. */
  42. /* hInstance is the handle for the current instance of the program         */
  43. /* hPrevInst is the handle for the last instance of the program to run     */
  44. /* CmdLine is a pointer to a string of whatever command line params came in*/
  45. /* nCmd is the specifies the applications appearance.                      */
  46.  
  47. int FAR PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInst, LPSTR CmdLine,
  48.                                                                      int nCmd)
  49. {
  50. MSG   msg;                                /* Message */
  51. HWND  hWnd;                       /* Our Window Handle */
  52. HMENU hMenu;
  53.  
  54. if (!hPrevInst)                         /* If no instance already exists, */
  55.    {
  56.     if (!InitInstance(hInstance))       /* try initializing instance */
  57.         return FALSE;                     /* No dice, it failed */
  58.     }
  59.  
  60. hInst = hInstance;
  61.     
  62. hWndMain = CreateWindow(szAppName,              /* Window Class */
  63.                               "Hello World Program",  /* Window caption */
  64.                               WS_OVERLAPPEDWINDOW,    /* Overlapped style */
  65.                               CW_USEDEFAULT,          /* Use defaults for the */
  66.                               CW_USEDEFAULT,          /* X & Y Positions and  */
  67.                               CW_USEDEFAULT,          /* X & Y sizes */
  68.                               CW_USEDEFAULT,          
  69.                               NULL,                   /* Parent Window */
  70.                               NULL,
  71.                               hInstance,              /* Instance */
  72.                               NULL);                  /* Creation Params */
  73.  
  74. if (hWndMain == NULL)
  75.    return 0;
  76.  
  77. hMenu = GetSystemMenu(hWndMain, FALSE);
  78. hAlternateMenu = LoadMenu(hInstance, "Hello2");
  79.  
  80. AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  81. AppendMenu(hMenu, MF_STRING,    IDM_HELPABOUT, "About...");
  82. AppendMenu(hMenu, MF_STRING,    IDM_SETUP, "Setup...");
  83.  
  84. ShowWindow(hWndMain, nCmd);
  85. UpdateWindow(hWndMain);
  86.     
  87. while (GetMessage(&msg, NULL, 0, 0))
  88.    {
  89.     TranslateMessage(&msg);
  90.     DispatchMessage(&msg);
  91.    }
  92.     
  93. return msg.wParam;
  94. }                                       /* int FAR PASCAL WinMain */
  95.  
  96. BOOL InitInstance (HANDLE hInstance)
  97. {
  98. WNDCLASS wc;                            /* Window class structure */
  99.  
  100. wc.lpszMenuName  = szAppName;
  101. wc.lpszClassName = szAppName;
  102. wc.hbrBackground = GetStockObject(WHITE_BRUSH);  /* White bkgrnd */
  103. wc.hInstance     = hInstance;
  104. wc.style         = CS_VREDRAW | CS_HREDRAW;
  105. wc.lpfnWndProc   = WndProc;              /* Name of proc to handle window */
  106. wc.hCursor       = LoadCursor(NULL, IDC_ARROW);  /* Arrow Cursor */
  107. wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);  /* Generic Icon */
  108. wc.cbClsExtra    = 0;                /* no extras */
  109. wc.cbWndExtra    = 0;                /* No Extras */
  110.  
  111. return (RegisterClass(&wc)); /* Let's register that class */
  112. }                                       /* BOOL InitInstance */
  113.  
  114. /* This is our main windows procedure. It receives messages in message, then,
  115. depending on what the message is, we react accordingly
  116. */
  117.  
  118. long FAR PASCAL WndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  119. {
  120. HDC            hdc;
  121. PAINTSTRUCT    ps;
  122. RECT           rect;
  123. HMENU          hMenu;
  124. static FARPROC lpfnHelloDlgProc, lpfnAboutDlgProc;
  125. char           OutMsg[25];
  126.  
  127. strcpy(OutMsg, "");
  128.  
  129. switch (message)
  130.    {
  131.    case WM_CREATE :
  132.       hMainMenu = GetMenu(hWnd);
  133.       InitSettings = GetPrivateProfileInt("Hello", "Setup", 1, "Hello.ini");
  134.       InitSettings = InitSettings ? IDM_HELLO : IDM_GOODBYE;
  135.       hdc = GetDC(hWnd);
  136.          InvalidateRect(hWnd, NULL, TRUE);
  137.       ReleaseDC(hWnd, hdc);
  138.  
  139. /*----------------------------------------------------------------------------
  140.   fall through to WM_PAINT...
  141. ----------------------------------------------------------------------------*/
  142.    case WM_PAINT :
  143.       hdc = BeginPaint(hWnd,&ps);        /* returns pointer to hdc */
  144.       GetClientRect(hWnd, &rect);
  145. /*
  146.   -1 tells the DrawText function to calculate length of string based on
  147.   NULL-termination
  148. */
  149.       DrawText(hdc, (InitSettings == IDM_HELLO) ? "Hello Windows!" : "Goodbye Windows!", -1,
  150.                                    &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  151.       EndPaint(hWnd,&ps);
  152.       return 0;
  153.  
  154.    case WM_SYSCOMMAND :
  155.       switch (wParam)
  156.          {
  157.          case IDM_SETUP :
  158.             lpfnHelloDlgProc = MakeProcInstance(HelloDlgProc, hInst);
  159.             DialogBox(hInst, "Hello", hWnd, lpfnHelloDlgProc);
  160.             FreeProcInstance(lpfnHelloDlgProc);
  161.             return 0;
  162.  
  163.          case IDM_HELPABOUT :
  164.             lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc, hInst);
  165.             DialogBox(hInst, "About", hWnd, lpfnAboutDlgProc);
  166.             FreeProcInstance(lpfnAboutDlgProc);
  167.             return 0;
  168.          }
  169.  
  170.       break;
  171.  
  172.    case WM_COMMAND :
  173.       hMenu = GetMenu(hWnd);
  174.       switch (wParam)
  175.          {
  176.       case IDM_FILENEW :
  177.          strcpy(OutMsg, "IDM_FILENEW");
  178.          break;
  179.  
  180.       case IDM_FILEOPEN :
  181.          strcpy(OutMsg, "IDM_FILEOPEN");
  182.          break;
  183.  
  184.       case IDM_FILESAVE :
  185.          strcpy(OutMsg, "IDM_FILESAVE");
  186.          break;
  187.  
  188.       case IDM_FILESAVEAS :
  189.          strcpy(OutMsg, "IDM_FILESAVEAS");
  190.          break;
  191.  
  192.       case IDM_FILEPRINT :
  193.          strcpy(OutMsg, "IDM_FILEPRINT");
  194.          break;
  195.  
  196.       case IDM_FILEPRINTPREV :
  197.          strcpy(OutMsg, "IDM_FILEPRINTPREV");
  198.          break;
  199.  
  200.       case IDM_FILEPRINTSETUP :
  201.          strcpy(OutMsg, "IDM_FILEPRINTSETUP");
  202.          break;
  203.  
  204.       case IDM_FILEEXIT :
  205.          strcpy(OutMsg, "IDM_FILEEXIT");
  206.          break;
  207.  
  208.  
  209.       case IDM_EDITUNDO :
  210.          strcpy(OutMsg, "IDM_EDITUNDO");
  211.          break;
  212.  
  213.       case IDM_EDITREPEAT :
  214.          strcpy(OutMsg, "IDM_EDITREPEAT");
  215.          break;
  216.  
  217.       case IDM_EDITCUT :
  218.          strcpy(OutMsg, "IDM_EDITCUT");
  219.          break;
  220.  
  221.       case IDM_EDITCOPY :
  222.          strcpy(OutMsg, "IDM_EDITCOPY");
  223.          break;
  224.  
  225.       case IDM_EDITPASTE :
  226.          strcpy(OutMsg, "IDM_EDITPASTE");
  227.          break;
  228.  
  229.       case IDM_EDITCLEAR :
  230.          strcpy(OutMsg, "IDM_EDITCLEAR");
  231.          break;
  232.  
  233.       case IDM_EDITPAST :
  234.          strcpy(OutMsg, "IDM_EDITPAST");
  235.          break;
  236.  
  237.  
  238.       case IDM_SAMPLEDLG :
  239.          strcpy(OutMsg, "IDM_SAMPLEDLG");
  240.          break;
  241.  
  242.  
  243.       case IDM_HELPINDX :
  244.          strcpy(OutMsg, "IDM_HELPINDX");
  245.          break;
  246.  
  247.       case IDM_HELPKBD :
  248.          strcpy(OutMsg, "IDM_HELPKBD");
  249.          break;
  250.  
  251.       case IDM_HELPCMDS :
  252.          strcpy(OutMsg, "IDM_HELPCMDS");
  253.          break;
  254.  
  255.       case IDM_HELPPROCS :
  256.          strcpy(OutMsg, "IDM_HELPPROCS");
  257.          break;
  258.  
  259.       case IDM_HELPUSING :
  260.          strcpy(OutMsg, "IDM_HELPUSING");
  261.          break;
  262.  
  263.       case IDM_HELPABOUT :
  264.          lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc, hInst);
  265.          DialogBox(hInst, "About", hWnd, lpfnAboutDlgProc);
  266.          FreeProcInstance(lpfnAboutDlgProc);
  267.          return 0;
  268.  
  269.       case IDM_ALTERNATE :
  270.          SetMenu(hWnd, hAlternateMenu);
  271.          return 0;
  272.  
  273.       case IDM_ALTONE : 
  274.          CheckMenuItem(hMenu, IDM_ALTONE, MF_CHECKED);
  275.          CheckMenuItem(hMenu, IDM_ALTTWO, MF_UNCHECKED);
  276.          return 0;
  277.  
  278.       case IDM_ALTTWO : 
  279.          CheckMenuItem(hMenu, IDM_ALTTWO, MF_CHECKED);
  280.          CheckMenuItem(hMenu, IDM_ALTONE, MF_UNCHECKED);
  281.          return 0;
  282.  
  283.       case IDM_ALTGRAY : 
  284.          EnableMenuItem(hMenu, IDM_ALTGRAY, MF_GRAYED);
  285.          EnableMenuItem(hMenu, IDM_ALTUNGRAY, MF_ENABLED);
  286.          return 0;
  287.  
  288.       case IDM_ALTUNGRAY : 
  289.          EnableMenuItem(hMenu, IDM_ALTUNGRAY, MF_GRAYED);
  290.          EnableMenuItem(hMenu, IDM_ALTGRAY, MF_ENABLED);
  291.          return 0;
  292.  
  293.       case IDM_ALTRESTORE : 
  294.          SetMenu(hWnd, hMainMenu);
  295.          return 0;
  296.  
  297.       case WM_DESTROY :
  298.           PostQuitMessage(0);
  299.           return 0;
  300.           }
  301.  
  302.    if (strlen(OutMsg) != 0)
  303.       {
  304.       MessageBox(hWnd, OutMsg, szAppName, MB_ICONEXCLAMATION | MB_OK);
  305.       return 0;
  306.       }
  307.  
  308.    }
  309. return DefWindowProc(hWnd, message, wParam, lParam);
  310. }                                       /* long FAR PASCAL WndProc */
  311.  
  312. /*============================================================================
  313.   HelloDlgProc -- 
  314.  
  315.   File      : Test.C
  316.  
  317.   Prototype : BOOL FAR PASCAL HelloDlgProc(HWND, WORD, WORD, LONG);
  318.  
  319.   Call      : HelloDlgProc(hDlg, message, wParam, lParam);
  320.  
  321.   Library   : 
  322.  
  323.   19 January 1993 - dlcampbell
  324.   (c) 1993 WynApse
  325. ----------------------------------------------------------------------------*/
  326. BOOL FAR PASCAL HelloDlgProc (HWND hDlg, WORD message, WORD wParam,
  327.                                                                   LONG lParam)
  328. {
  329. switch (message)
  330.    {
  331.    case WM_INITDIALOG :
  332.       CheckRadioButton(hDlg, IDM_HELLO, IDM_GOODBYE, InitSettings);
  333.       return TRUE;
  334.  
  335.    case WM_COMMAND :
  336.       switch (wParam)
  337.          {
  338.          case IDM_HELLO : 
  339.                WritePrivateProfileString("Hello", "Setup", "1", "Hello.ini");
  340.             InitSettings = wParam;
  341.             break;
  342.  
  343.          case IDM_GOODBYE : 
  344.                WritePrivateProfileString("Hello", "Setup", "0", "Hello.ini");
  345.             InitSettings = wParam;
  346.             break;
  347.  
  348.          case IDOK :
  349.             EndDialog(hDlg, wParam);
  350.             return TRUE;
  351.  
  352.          }
  353.       break;
  354.  
  355.    }
  356. return FALSE;
  357. }                                       /* HelloDlgProc */
  358. /*============================================================================
  359.   AboutDlgProc
  360.  
  361.   File      : ExitWin.C
  362.  
  363.   Prototype : 
  364.  
  365.   Call      : 
  366.  
  367.   Library   : 
  368.  
  369.   8 March 1993 - dlcampbell
  370.   (c) 1993 WynApse
  371. ----------------------------------------------------------------------------*/
  372. BOOL FAR PASCAL AboutDlgProc (HWND hDlg, WORD message, WORD wParam,
  373.                                                                   LONG lParam)
  374. {
  375. char szBuffer[100];
  376.  
  377. switch (message)
  378.    {
  379.    case WM_INITDIALOG :
  380.       wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
  381.       SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
  382.       return TRUE;
  383.  
  384.    case WM_COMMAND :
  385.       switch (wParam)
  386.          {
  387.          case IDOK :
  388.          case IDCANCEL :
  389.             if (HIWORD(lParam) == BN_CLICKED)
  390.                EndDialog(hDlg, wParam);
  391.             return TRUE;
  392.  
  393.          default :
  394.             return TRUE;
  395.          }
  396.  
  397.    default :
  398.       return FALSE;
  399.    }
  400. }                                       /* AboutDlgProc */
  401.