home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / AUTOCALC.PAK / WINMAIN.CPP < prev   
Encoding:
C/C++ Source or Header  |  1997-05-06  |  4.7 KB  |  153 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents - (C) Copyright 1994, 1996 by Borland International
  3. // Automated calculator example demonstrating class hierarchy and collections
  4. // Automation localized for simultaneous English and German use and typelibs
  5. // The example can be built as an .EXE or as an inproc .DLL server
  6. //----------------------------------------------------------------------------
  7. #include <ocf/pch.h>
  8. #include <classlib/pointer.h>
  9. #include <ocf/ocreg.h>
  10. #include "autocalc.h"
  11.  
  12. BEGIN_REGISTRATION(AppReg)
  13.   REGDATA(appname,    APP_NAME)
  14. #if !defined(__DLL__) // building EXE application
  15.   REGDATA(clsid,      "{877B6200-7627-101B-B87C-0000C057CE4E}")
  16.   REGDATA(progid,     APP_NAME ".Application")
  17.   REGDATA(description,"@AppName")
  18.   REGDATA(cmdline,    "/Automation")
  19. #if defined(BI_PLAT_WIN32)
  20.   REGDATA(debugger,   "TD32")
  21. #else
  22.   REGDATA(debugger,   "TDW")
  23. #endif
  24.   REGDATA(debugclsid, "{877B6201-7627-101B-B87C-0000C057CE4E}")
  25.   REGDATA(debugprogid,  APP_NAME ".Debug")
  26.   REGDATA(debugdesc,"@AppName")
  27. #else                 // building DLL inproc server
  28.   REGDATA(clsid,      "{877B6280-7627-101B-B87C-0000C057CE4E}")
  29.   REGDATA(progid,     APP_NAME ".DLLServer")
  30.   REGDATA(description,"@AppDLLServer")
  31. #endif                // common to both EXE and DLL
  32.   REGDATA(typehelp,   "@typehelp")
  33.   REGDATA(version,    "1.2")
  34. END_REGISTRATION
  35.  
  36. BOOL InitApplication(HINSTANCE hInst);
  37. long FAR PASCAL WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  38.  
  39. TPointer<TRegistrar> Registrar;    // initialized at WinMain or LibMain
  40.  
  41. #if !defined(__DLL__)  // building EXE application
  42.  
  43. static const char* ErrorLookup(long errCode)
  44. {
  45.   static char buf[50];
  46.   wsprintf(buf, "Error message #%ld", errCode);
  47.   return buf;
  48. }
  49.  
  50. int PASCAL
  51. WinMain(HINSTANCE hInst, HINSTANCE prev, char far* cmdLine, int/*cmdShow*/)
  52. {
  53.   if(prev || InitApplication(hInst)) {
  54.     try {
  55.       string cmdLineString(cmdLine);
  56.       ::Registrar=new TRegistrar(AppReg,TOcAutoFactory<TCalc>(),
  57.                                  cmdLineString, hInst);
  58.       TAutoCommand::SetErrorMsgHook(ErrorLookup);
  59.       if (!::Registrar->IsOptionSet(amAnyRegOption))
  60.         ::Registrar->Run();
  61.       ::Registrar = 0;  // deletes registrar by replacing pointer
  62.       return 0;
  63.     }
  64.     catch (TXBase& x) {
  65.       ::MessageBox(0, x.why().c_str(), "OLE Exception", MB_OK);
  66.     }
  67.   }
  68.   return 1;
  69. }
  70.  
  71. #else  // building DLL inproc server
  72.  
  73. #if defined(BI_PLAT_WIN16)
  74. int PASCAL
  75. LibMain(HINSTANCE hInst, uint16 /*dataSeg*/, uint16 /*heapSize*/, char far* cmdLine)
  76. {
  77. #else
  78. int WINAPI
  79. DllEntryPoint(HINSTANCE hInst, uint32 reason, LPVOID)
  80. {
  81.   char* cmdLine = 0;
  82.   if (reason != DLL_PROCESS_ATTACH)
  83.     return 1;
  84. #endif
  85.   if (InitApplication(hInst)) {
  86.     try {
  87.       string cmdLineString(cmdLine);
  88.       ::Registrar=new TRegistrar(AppReg,TOcAutoFactory<TCalc>(),
  89.                                  cmdLineString, hInst);
  90.       return 1;
  91.     }
  92.     catch (TXBase& x) {
  93.       ::MessageBox(0, x.why().c_str(), "OLE Exception", MB_OK);
  94.     }
  95.   }
  96.   return 0;
  97. }
  98.  
  99. #endif  // the code following is common to both EXE and DLL builds
  100.  
  101. BOOL InitApplication(HINSTANCE hInst)
  102. {
  103.   WNDCLASS wc;
  104.   wc.style            = CS_HREDRAW | CS_VREDRAW;
  105.   wc.lpfnWndProc      = WndProc;
  106.   wc.cbClsExtra       = 0;
  107.   wc.cbWndExtra       = DLGWINDOWEXTRA + sizeof(long);
  108.   wc.hInstance        = hInst;
  109.   wc.hIcon            = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON));
  110.   wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  111.   wc.hbrBackground    = (HBRUSH)(COLOR_APPWORKSPACE+1);
  112.   wc.lpszMenuName     = 0;
  113.   wc.lpszClassName    = APP_NAME;
  114.   return ::RegisterClass(&wc) != 0;
  115. }
  116.  
  117. long PASCAL __export WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  118. {
  119.   TCalc* This = (TCalc*)::GetWindowLong(hwnd, DLGWINDOWEXTRA);
  120.   switch(msg) {
  121.   case WM_COMMAND:
  122.     if (wParam < IDC_FIRSTID || wParam > IDC_LASTID)
  123.       break;
  124.     This->ButtonPush(wParam);
  125.     return 0;
  126.  
  127.   case WM_ERASEBKGND:
  128.     if (!This->GetWindow().Background) {
  129.       This->GetWindow().Background = (long)::GetSysColor(COLOR_APPWORKSPACE);
  130.       break;
  131.     } else {
  132.       HBRUSH brush = ::CreateSolidBrush(This->GetWindow().Background);
  133.       ::UnrealizeObject(brush);
  134.       HBRUSH oldbrush = (HBRUSH)::SelectObject((HDC)wParam, brush);
  135.       ::PatBlt((HDC)wParam,0,0,10000,10000,PATCOPY);
  136.       ::SelectObject((HDC)wParam, oldbrush);
  137.       ::DeleteObject(brush);
  138.       return 1;
  139.     }
  140.  
  141.   case WM_DESTROY:
  142.     if (This) {
  143.       This->Closed();
  144.       ::Registrar->ReleaseAutoApp(TAutoObject<TCalc>(This));
  145.     }
  146.     if (This->Options & amExeMode)
  147.       PostQuitMessage(0);
  148.     return 0;
  149.   }
  150.   return ::DefWindowProc(hwnd, msg, wParam, lParam);
  151. }
  152.  
  153.