home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / oleaut / dispcalc / winmain.cpp < prev   
Encoding:
C/C++ Source or Header  |  1997-10-05  |  3.3 KB  |  167 lines

  1. /*** 
  2. *main.cpp
  3. *
  4. *  This is a part of the Microsoft Source Code Samples.
  5. *
  6. *  Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
  7. *
  8. *  This source code is only intended as a supplement to Microsoft Development
  9. *  Tools and/or WinHelp documentation.  See these sources for detailed
  10. *  information regarding the Microsoft samples programs.
  11. *
  12. *Purpose:
  13. *  This module is the main entry point of the sample IDispatch
  14. *  calculator, dispcalc.exe
  15. *
  16. *  This program is intended to demonstrate an implementation of
  17. *  the IDispatch interface.
  18. *
  19. *Implementation Notes:
  20. *
  21. *****************************************************************************/
  22.  
  23. #include "dispcalc.h"
  24.  
  25. TCHAR g_szAppName[] = TSTR("DispCalc");
  26.  
  27. BOOL InitApplication(HINSTANCE);
  28. BOOL InitInstance(HINSTANCE, int);
  29.  
  30.  
  31. extern "C" {
  32. long FAR PASCAL WndProc(HWND, UINT, WPARAM, LPARAM);
  33. int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
  34. }
  35.  
  36.  
  37. extern "C" int PASCAL
  38. WinMain(
  39.     HINSTANCE hinst,
  40.     HINSTANCE hinstPrev,
  41.     LPSTR lpCmdLine,
  42.     int nCmdShow)
  43. {
  44.     MSG msg;
  45.  
  46.     if(!hinstPrev)
  47.       if(!InitApplication(hinst))
  48.     return FALSE;
  49.  
  50.     if(InitOle() != NOERROR)
  51.       return FALSE;
  52.  
  53.     if(!InitInstance(hinst, nCmdShow)){
  54.       UninitOle();
  55.       return FALSE;
  56.     }
  57.  
  58.     while(GetMessage(&msg, NULL, NULL, NULL)) {
  59.       TranslateMessage(&msg);
  60.       DispatchMessage(&msg);
  61.     }
  62.  
  63.     UninitOle();
  64.  
  65.     return msg.wParam;
  66. }
  67.  
  68.  
  69. BOOL
  70. InitApplication(HINSTANCE hinst)
  71. {
  72.     WNDCLASS  wc;
  73.  
  74.     wc.style        = CS_HREDRAW | CS_VREDRAW;
  75.     wc.lpfnWndProc    = WndProc;
  76.     wc.cbClsExtra    = 0;
  77.     wc.cbWndExtra    = DLGWINDOWEXTRA;
  78.     wc.hInstance    = hinst;
  79.     wc.hIcon        = LoadIcon(hinst, g_szAppName);
  80.     wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  81.     wc.hbrBackground    = (HBRUSH)(COLOR_APPWORKSPACE+1);
  82.     wc.lpszMenuName    = NULL;
  83.     wc.lpszClassName    = g_szAppName;
  84.  
  85.     if(!RegisterClass(&wc))
  86.       return FALSE;
  87.  
  88.     return TRUE;
  89. }
  90.  
  91.  
  92. BOOL
  93. InitInstance(HINSTANCE hinst, int nCmdShow)
  94. {
  95.     g_pcalc->m_hwnd = CreateDialog(hinst, g_szAppName, 0, NULL);
  96.  
  97.     ShowWindow(g_pcalc->m_hwnd, nCmdShow);
  98.  
  99.     g_pcalc->m_arith.Display();
  100.  
  101.     return TRUE;
  102. }
  103.  
  104.  
  105. extern "C" long FAR PASCAL
  106. WndProc(
  107.     HWND hwnd,
  108.     UINT message,
  109.     WPARAM wParam,
  110.     LPARAM lParam)
  111. {
  112.     switch(message){
  113.     case WM_COMMAND:
  114.       switch(wParam){
  115.       case IDC_ZERO:
  116.       case IDC_ONE:
  117.       case IDC_TWO:
  118.       case IDC_THREE:
  119.       case IDC_FOUR:
  120.       case IDC_FIVE:
  121.       case IDC_SIX:
  122.       case IDC_SEVEN:
  123.       case IDC_EIGHT:
  124.       case IDC_NINE:
  125.       case IDC_PLUS:
  126.       case IDC_MINUS:
  127.       case IDC_MULT:
  128.       case IDC_DIV:
  129.       case IDC_CLEAR:
  130.       case IDC_EQUALS:
  131.     g_pcalc->m_arith.ButtonPush(wParam);
  132.     return 0;
  133.       }
  134.       break;
  135.  
  136.     case WM_DESTROY:
  137.       PostQuitMessage(0);
  138.       return 0;
  139.     }
  140.     return DefWindowProc(hwnd, message, wParam, lParam);
  141. }
  142.  
  143.  
  144. #if defined(WIN32)
  145.  
  146. extern "C" char FAR*
  147. ConvertStrWtoA(OLECHAR FAR* strIn, char FAR* buf, UINT size)
  148. {
  149.   int badConversion = FALSE;
  150.   
  151.   WideCharToMultiByte(CP_ACP, NULL, 
  152.                   strIn, -1, 
  153.               buf, size, 
  154.               NULL, &badConversion);
  155.   return buf;
  156. }
  157.  
  158. extern "C" char FAR*
  159. AnsiString(OLECHAR FAR* strIn)
  160. {
  161.   static char buf[256];
  162.   
  163.   return (ConvertStrWtoA(strIn, buf, 256));    
  164. }
  165.  
  166. #endif
  167.