|
|
1.1 ! root 1: /*** ! 2: *spoly2.cpp ! 3: * ! 4: * Copyright (C) 1992, Microsoft Corporation. All Rights Reserved. ! 5: * Information Contained Herein Is Proprietary and Confidential. ! 6: * ! 7: *Purpose: ! 8: * This module is the main entry point for the sample IDispatch polygon ! 9: * server, spoly2.exe. ! 10: * ! 11: * This program is intended to demonstrate an implementation of the IDispatch ! 12: * interface. Spoly is a very simple app, that implements two simple objects, ! 13: * CPoly and CPoint and exposes their properties and methods for programatic ! 14: * and cross-process access via IDispatch. ! 15: * ! 16: *Implementation Notes: ! 17: * ! 18: *****************************************************************************/ ! 19: ! 20: #include <stdio.h> ! 21: #include <string.h> ! 22: ! 23: #include <windows.h> ! 24: #include <ole2.h> ! 25: #include <dispatch.h> ! 26: ! 27: #include "common.h" ! 28: ! 29: #include "statbar.h" ! 30: ! 31: #include "spoly.h" ! 32: #include "cpoint.h" ! 33: #include "cpoly.h" ! 34: ! 35: ! 36: HANDLE g_hinst = 0; ! 37: ! 38: HWND g_hwndFrame = 0; ! 39: HWND g_hwndClient = 0; ! 40: ! 41: char g_szFrameWndClass[] = "FrameWClass"; ! 42: ! 43: DWORD g_dwPolyCF = 0L; ! 44: DWORD g_dwPointCF = 0L; ! 45: ! 46: IClassFactory FAR* g_ppolyCF = NULL; ! 47: IClassFactory FAR* g_ppointCF = NULL; ! 48: ! 49: BOOL g_fExitOnLastRelease = FALSE; ! 50: ! 51: CStatBar FAR* g_psb = NULL; ! 52: ! 53: ! 54: HRESULT InitOle(void); ! 55: void UninitOle(void); ! 56: BOOL InitApplication(HANDLE); ! 57: BOOL InitInstance(HANDLE, int); ! 58: ! 59: extern "C" int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int); ! 60: extern "C" long FAR PASCAL FrameWndProc(HWND, UINT, WPARAM, LPARAM); ! 61: ! 62: ! 63: extern "C" int PASCAL ! 64: WinMain( ! 65: HANDLE hinst, ! 66: HANDLE hPrevInstance, ! 67: LPSTR lpCmdLine, ! 68: int nCmdShow) ! 69: { ! 70: MSG msg; ! 71: int retval; ! 72: HRESULT hresult; ! 73: ! 74: if(!hPrevInstance) ! 75: if(!InitApplication(hinst)) ! 76: return FALSE; ! 77: ! 78: if((hresult = InitOle()) != NOERROR) ! 79: return FALSE; ! 80: ! 81: if(!InitInstance(hinst, nCmdShow)){ ! 82: retval = FALSE; ! 83: goto LExit; ! 84: } ! 85: ! 86: if(STRSTR(lpCmdLine, "-Embedding")) ! 87: g_fExitOnLastRelease = TRUE; ! 88: ! 89: while(GetMessage(&msg, NULL, NULL, NULL)) { ! 90: TranslateMessage(&msg); ! 91: DispatchMessage(&msg); ! 92: } ! 93: ! 94: CPoly::PolyTerm(); ! 95: ! 96: retval = msg.wParam; ! 97: ! 98: LExit:; ! 99: UninitOle(); ! 100: ! 101: return retval; ! 102: } ! 103: ! 104: ! 105: BOOL ! 106: InitApplication(HANDLE hinst) ! 107: { ! 108: WNDCLASS wc; ! 109: ! 110: wc.style = CS_HREDRAW | CS_VREDRAW; ! 111: wc.lpfnWndProc = FrameWndProc; ! 112: wc.cbClsExtra = 0; ! 113: wc.cbWndExtra = 0; ! 114: wc.hInstance = hinst; ! 115: wc.hIcon = LoadIcon(hinst, "SPOLY"); ! 116: wc.hCursor = LoadCursor(NULL, IDC_ARROW); ! 117: wc.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE+1); ! 118: wc.lpszMenuName = "SPolyMenu"; ! 119: wc.lpszClassName = g_szFrameWndClass; ! 120: ! 121: if(!RegisterClass(&wc)) ! 122: return FALSE; ! 123: ! 124: return TRUE; ! 125: } ! 126: ! 127: ! 128: BOOL ! 129: InitInstance(HANDLE hinst, int nCmdShow) ! 130: { ! 131: g_hinst = hinst; ! 132: ! 133: // Create a main frame window ! 134: // ! 135: g_hwndFrame = CreateWindow( ! 136: g_szFrameWndClass, ! 137: "IDispatch Polygon Server #2", ! 138: WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, ! 139: CW_USEDEFAULT, ! 140: CW_USEDEFAULT, ! 141: CW_USEDEFAULT, ! 142: CW_USEDEFAULT, ! 143: NULL, ! 144: NULL, ! 145: hinst, ! 146: NULL); ! 147: if(!g_hwndFrame) ! 148: return FALSE; ! 149: ! 150: g_hwndClient = GetWindow(g_hwndFrame, GW_CHILD); ! 151: if(!g_hwndClient) ! 152: return FALSE; ! 153: ! 154: // create the status bar ! 155: // ! 156: g_psb = CStatBar::Create(g_hinst, g_hwndFrame); ! 157: if(!g_psb) ! 158: return FALSE; ! 159: ! 160: // initialize and show the status bar ! 161: // ! 162: g_psb->SetHeight(GetSystemMetrics(SM_CYCAPTION) - 1); ! 163: g_psb->SetFont(GetStockObject(SYSTEM_FONT)); ! 164: g_psb->SetText(""); ! 165: g_psb->Show(); ! 166: ! 167: ShowWindow(g_hwndFrame, nCmdShow); ! 168: ! 169: UpdateWindow(g_hwndFrame); ! 170: ! 171: return TRUE; ! 172: } ! 173: ! 174: ! 175: /*** ! 176: *HRESULT InitOle(void) ! 177: *Purpose: ! 178: * Initialize Ole, and register our class factories. ! 179: * ! 180: *Entry: ! 181: * None ! 182: * ! 183: *Exit: ! 184: * None ! 185: * ! 186: ***********************************************************************/ ! 187: HRESULT ! 188: InitOle() ! 189: { ! 190: HRESULT hresult; ! 191: ! 192: if((hresult = OleInitialize(NULL)) != NOERROR) ! 193: goto LError0; ! 194: ! 195: // Register the CPoint Class Factory ! 196: // ! 197: if((g_ppointCF = CPointCF::Create()) == NULL){ ! 198: hresult = ResultFromScode(E_OUTOFMEMORY); ! 199: goto LError1; ! 200: } ! 201: ! 202: hresult = CoRegisterClassObject( ! 203: CLSID_CPoint2, ! 204: g_ppointCF, ! 205: CLSCTX_LOCAL_SERVER, ! 206: REGCLS_MULTIPLEUSE, ! 207: &g_dwPointCF); ! 208: if(hresult != NOERROR) ! 209: goto LError1; ! 210: ! 211: // Register the CPoly Class Factory. ! 212: // ! 213: if((g_ppolyCF = CPolyCF::Create()) == NULL){ ! 214: hresult = ResultFromScode(E_OUTOFMEMORY); ! 215: goto LError1; ! 216: } ! 217: ! 218: hresult = CoRegisterClassObject( ! 219: CLSID_CPoly2, ! 220: g_ppolyCF, ! 221: CLSCTX_LOCAL_SERVER, ! 222: REGCLS_MULTIPLEUSE, ! 223: &g_dwPolyCF); ! 224: if(hresult != NOERROR) ! 225: goto LError1; ! 226: ! 227: g_ppolyCF->Release(); ! 228: ! 229: g_ppointCF->Release(); ! 230: ! 231: return NOERROR; ! 232: ! 233: ! 234: LError1:; ! 235: if(g_ppolyCF != NULL) ! 236: g_ppolyCF->Release(); ! 237: ! 238: if(g_ppointCF != NULL) ! 239: g_ppointCF->Release(); ! 240: ! 241: UninitOle(); ! 242: ! 243: LError0:; ! 244: return hresult; ! 245: } ! 246: ! 247: ! 248: void ! 249: UninitOle() ! 250: { ! 251: // Tell Ole to release our class factories. ! 252: // ! 253: if(g_dwPointCF != 0L) ! 254: CoRevokeClassObject(g_dwPointCF); ! 255: ! 256: if(g_dwPolyCF != 0L) ! 257: CoRevokeClassObject(g_dwPolyCF); ! 258: ! 259: OleUninitialize(); ! 260: } ! 261: ! 262: ! 263: void ! 264: FrameWndOnCreate(HWND hwnd) ! 265: { ! 266: CLIENTCREATESTRUCT ccs; ! 267: ! 268: ccs.hWindowMenu = NULL; ! 269: ccs.idFirstChild = IDM_FIRSTCHILD; ! 270: ! 271: g_hwndClient = CreateWindow( ! 272: "MDICLIENT", ! 273: 0, ! 274: WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE, ! 275: 0, 0, 0, 0, ! 276: hwnd, ! 277: (HMENU) 1, ! 278: g_hinst, ! 279: &ccs); ! 280: } ! 281: ! 282: ! 283: void ! 284: FrameWndOnSize(HWND hwnd) ! 285: { ! 286: RECT rc; ! 287: int height; ! 288: ! 289: // Get the client rectangle for the frame window ! 290: GetClientRect(hwnd, &rc); ! 291: ! 292: height = g_psb->GetHeight(); ! 293: ! 294: // adjust the client win to make room for the status bar. ! 295: // ! 296: MoveWindow( ! 297: g_hwndClient, ! 298: rc.left, ! 299: rc.top, ! 300: rc.right - rc.left, ! 301: rc.bottom - rc.top - height, ! 302: TRUE); ! 303: ! 304: // move the status bar to the bottom of the newly positioned window. ! 305: // ! 306: g_psb->SetX(rc.left); ! 307: g_psb->SetY(rc.bottom - height), ! 308: g_psb->SetWidth(rc.right - rc.left); ! 309: g_psb->Move(); ! 310: } ! 311: ! 312: ! 313: extern "C" long FAR PASCAL ! 314: FrameWndProc( ! 315: HWND hwnd, ! 316: UINT message, ! 317: WPARAM wParam, ! 318: LPARAM lParam) ! 319: { ! 320: switch(message){ ! 321: case WM_COMMAND: ! 322: switch(wParam){ ! 323: case IDM_DUMP: ! 324: CPoly::PolyDump(); ! 325: return 0; ! 326: ! 327: case IDM_CLEAR: ! 328: InvalidateRect(g_hwndClient, NULL, TRUE); ! 329: return 0; ! 330: } ! 331: break; ! 332: ! 333: case WM_CREATE: ! 334: FrameWndOnCreate(hwnd); ! 335: break; ! 336: ! 337: case WM_SIZE: ! 338: FrameWndOnSize(hwnd); ! 339: return 1; ! 340: ! 341: case WM_PAINT: ! 342: CPoly::PolyDraw(); ! 343: break; ! 344: ! 345: case WM_CLOSE: ! 346: DestroyWindow(hwnd); ! 347: return 0; ! 348: ! 349: case WM_DESTROY: ! 350: PostQuitMessage(0); ! 351: return 0; ! 352: } ! 353: return DefFrameProc(hwnd, g_hwndClient, message, wParam, lParam); ! 354: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.