home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / ddeml / demo / demo.c next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  9.6 KB  |  366 lines

  1. /****************************** Module Header ******************************\
  2. * Module Name:  demo.c - Demo application
  3. *
  4. * Created:
  5. *
  6. * Copyright (c) 1987  Microsoft Corporation
  7. *
  8. \***************************************************************************/
  9.  
  10. #include "demo.h"
  11. #include <stdlib.h>
  12.  
  13. /************* GLOBAL VARIABLES         */
  14.  
  15. char szDemoClass[] = "Demo";
  16.  
  17. HAB  hab;
  18. HMQ  hmqDemo;
  19. HWND hwndDemo;
  20. HWND hwndDemoFrame;
  21. HCONV hconv = NULL;
  22. HSZ hszTitle, hszTopicChase, hszItemPos;
  23. USHORT fmtSWP;
  24. SWP SWPTarget = { 0 };
  25. PFNWP RealFrameProc;
  26. BOOL flee = FALSE;
  27. USHORT cServers = 0;
  28. USHORT cxScreen, cyScreen;
  29.  
  30. #define TIMEOUT 100
  31. #define TIMERSPEED 1000
  32.  
  33. /**************************************/
  34.  
  35. VOID CommandMsg(USHORT cmd)
  36. {
  37.     UNUSED cmd;
  38. }
  39.  
  40. BOOL DemoInit()
  41. {
  42.     hab = WinInitialize(0);
  43.  
  44.     hmqDemo = WinCreateMsgQueue(hab, 0);
  45.  
  46.     cxScreen = (USHORT)WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN);
  47.     cyScreen = (USHORT)WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);
  48.     srand(2);
  49.     
  50.     if (!WinRegisterClass(hab, szDemoClass, (PFNWP)DemoWndProc,
  51.             CS_SIZEREDRAW, 0))
  52.          return(FALSE);
  53.  
  54.     /*
  55.      * Initialize the ddeml
  56.      */
  57.     if (DdeInitialize((PFNCALLBACK)callback, 0L, 0L))
  58.         return(FALSE);
  59.  
  60.     /*
  61.      * Now create HSZs for each of our DDE strings.
  62.      */
  63.     fmtSWP = WinAddAtom(WinQuerySystemAtomTable(), "SWP FORMAT");
  64.     hszTitle = DdeGetHsz("Demo", 0, 0);
  65.     hszTopicChase = DdeGetHsz("Chaser", 0, 0);
  66.     hszItemPos = DdeGetHsz("Position", 0, 0);
  67.  
  68.     /*
  69.      * let others know we are here - available as a server and turn on
  70.      * filtering so we don't get bothered with any initiates for any
  71.      * other app names.
  72.      */
  73.     DdeAppNameServer(hszTitle, ANS_REGISTER | ANS_FILTERON);
  74.         
  75.     return(TRUE);
  76. }
  77.  
  78. int cdecl main(int argc, char** argv)
  79. {
  80.     ULONG fcf;
  81.     QMSG qmsg;
  82.  
  83.     UNUSED argc;
  84.     UNUSED argv;
  85.  
  86.     if (!DemoInit()) {
  87.         WinAlarm(HWND_DESKTOP, WA_ERROR);
  88.         return(0);
  89.     }
  90.  
  91.     fcf = FCF_STANDARD;
  92.  
  93.     hwndDemoFrame = WinCreateStdWindow(
  94.             HWND_DESKTOP,
  95.             WS_VISIBLE,
  96.             &fcf,
  97.             szDemoClass,
  98.             "",
  99.             WS_VISIBLE, 
  100.             NULL,
  101.             IDR_DEMO,
  102.             &hwndDemo);
  103.  
  104.     WinSetFocus(HWND_DESKTOP, hwndDemo);
  105.  
  106.     while (WinGetMsg(hab, (PQMSG)&qmsg, NULL, 0, 0)) {
  107.         WinDispatchMsg(hab, (PQMSG)&qmsg);
  108.     }
  109.  
  110.     WinDestroyWindow(hwndDemoFrame);
  111.  
  112.     WinDestroyMsgQueue(hmqDemo);
  113.     WinTerminate(hab);
  114.  
  115.     return(0);
  116. }
  117.  
  118. /********** Demo Window Procedure **************/
  119.  
  120. MRESULT FAR PASCAL DemoWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  121. {
  122.     HPS   hps;
  123.     RECTL rclPaint;
  124.     SWP swp;
  125.     SHORT speed;
  126.  
  127.     switch (msg) {
  128.     case WM_CREATE:
  129.         /* Set up this global first thing in case we need it elsewhere */
  130.         hwndDemo = hwnd;
  131.         hwndDemoFrame = WinQueryWindow(hwnd, QW_PARENT, FALSE);
  132.         RealFrameProc = WinSubclassWindow(hwndDemoFrame, DemoFrameWndProc);
  133.         WinSetWindowPos(hwndDemoFrame, NULL, 0, 0, 0, 0, SWP_MINIMIZE | SWP_SHOW);
  134.         /*
  135.          * start the timer so we will keep looking for another app like ours
  136.          * to chase.
  137.          */
  138.         WinStartTimer(hab, hwndDemo, 1, TIMERSPEED);
  139.         return(MRFROMSHORT(FALSE));
  140.         break;
  141.  
  142.     case WM_TIMER:
  143.         /*
  144.          * We use a timer to keep us moving.
  145.          */
  146.         if (!hconv) {
  147.             HDMGDATA hData;
  148.             PHSZHAPP phszhapp;
  149.             /*
  150.              * no active conversation, try to make one.
  151.              */
  152.             WinStopTimer(hab, hwndDemo, 1);
  153.             /*
  154.              * find out if any others like us are out there
  155.              */
  156.             hData = DdeAppNameServer(hszTitle, ANS_QUERYALLBUTME);
  157.             if (!hData) {
  158.                 /*
  159.                  * wait till others arrive.
  160.                  */
  161.                 return(0);
  162.             }
  163.             /*
  164.              * extract the first hApp from the hData so we can connect to it
  165.              */
  166.             phszhapp = (PHSZHAPP)DdeAccessData(hData);
  167.             if (phszhapp->hsz == 0) {
  168.                 DdeFreeData(hData);
  169.                 return(0);
  170.             }
  171.             /*
  172.              * perform directed connection to our target
  173.              */
  174.             hconv = DdeConnect(hszTitle, hszTopicChase, NULL, phszhapp->hApp);
  175.             /*
  176.              * free the hData now that we are done using it.
  177.              */
  178.             DdeFreeData(hData);
  179.             WinStartTimer(hab, hwndDemo, 1, TIMERSPEED);
  180.             if (!hconv) {
  181.                 /*
  182.                  * cant make one, try again later.
  183.                  */
  184.                 return(0);
  185.             }
  186.             /*
  187.              * Get the target's position into SWPTarget.
  188.              */
  189.             if (hData = DdeClientXfer(NULL, 0L, hconv, hszItemPos, fmtSWP,
  190.                     XTYP_REQUEST, TIMEOUT, NULL)) {
  191.                 DdeCopyBlock(DdeAccessData(hData), (PBYTE)&SWPTarget,
  192.                         sizeof(SWP));
  193.                 DdeFreeData(hData);
  194.             }
  195.             /*
  196.              * set up an advise loop so our moving target keeps us informed
  197.              * of where he is.
  198.              */
  199.             DdeClientXfer(NULL, 0L, hconv, hszItemPos, fmtSWP,
  200.                     XTYP_ADVSTART, TIMEOUT, NULL);
  201.         }
  202.         
  203.         if (WinIsWindow(hab, SWPTarget.hwnd)) {
  204.             /*
  205.              * target data must be valid, move toward it.
  206.              */
  207.             speed = 1;
  208.             WinQueryWindowPos(hwndDemoFrame, &swp);
  209.             if (swp.x > SWPTarget.x) 
  210.                 swp.x -= speed;
  211.             if (swp.x < SWPTarget.x)
  212.                 swp.x += speed;
  213.             if (swp.y > SWPTarget.y) 
  214.                 swp.y -= speed;
  215.             if (swp.y < SWPTarget.y)
  216.                 swp.y += speed;
  217.             swp.fs = SWP_MOVE | SWP_NOADJUST;
  218.             WinSetMultWindowPos(hab, &swp, 1);
  219.             if ((swp.x == SWPTarget.x) && (swp.y == SWPTarget.y) && (!flee)) {
  220.                 /*
  221.                  * he's cought stop chasing him and go find another.
  222.                  */
  223.                 WinAlarm(HWND_DESKTOP, WA_NOTE);
  224.                 DdeDisconnect(hconv);
  225.                 hconv = NULL;
  226.                 /*
  227.                  * move to a random position
  228.                  */
  229.                 WinSetWindowPos(hwndDemoFrame, HWND_TOP, rand() % cxScreen,
  230.                         rand() % cyScreen, 0, 0,
  231.                         SWP_MOVE | SWP_ZORDER | SWP_NOADJUST);
  232.             }
  233.         } else if (hconv) {
  234.             /*
  235.              * Target is invalid, disconnect and try a reconnect later.
  236.              */
  237.             DdeDisconnect(hconv);
  238.             hconv = NULL;
  239.         }
  240.         break;
  241.  
  242.     case WM_PAINT:
  243.         hps = WinBeginPaint(hwnd, (HPS)NULL, &rclPaint);
  244.         DemoPaint(hwnd, hps, &rclPaint);
  245.         WinEndPaint(hps);
  246.         break;
  247.  
  248.     case WM_COMMAND:
  249.         CommandMsg(LOUSHORT(mp1));
  250.         break;
  251.  
  252.     default:
  253.         return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  254.         break;
  255.     }
  256.     return(0L);
  257. }
  258.  
  259.  
  260.  
  261. MRESULT FAR PASCAL DemoFrameWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  262. {
  263.     switch (msg) {
  264.     case WM_MOVE:
  265.         DdePostAdvise(hszTopicChase, hszItemPos);
  266.         /* fall through */
  267.     default:
  268.         return(RealFrameProc(hwnd, msg, mp1, mp2));
  269.         break;
  270.     }
  271.     return(0L);
  272. }
  273.  
  274.  
  275. VOID DemoPaint(HWND hwnd, HPS hps, RECTL* prcl)
  276. {
  277.     RECTL rcl;
  278.  
  279.     UNUSED prcl;
  280.     
  281.     /* get window interior rect */
  282.     WinQueryWindowRect(hwnd, &rcl);
  283.  
  284.     /* print "Hello World" centered horizontally and vertically */
  285.     WinDrawText(hps, -1, "Hello World", &rcl, SYSCLR_WINDOWTEXT,
  286.             SYSCLR_WINDOW, DT_CENTER | DT_VCENTER | DT_ERASERECT);
  287.  
  288.     /* draw interior border */
  289.     WinDrawBorder(hps, &rcl, 6, 6, SYSCLR_WINDOWTEXT, SYSCLR_WINDOW,
  290.             DB_STANDARD);
  291. }
  292.  
  293.  
  294.  
  295. HDMGDATA EXPENTRY callback(
  296. HCONV hConv,
  297. HSZ hszTopic,
  298. HSZ hszItem,
  299. USHORT usFmt,
  300. USHORT usType,
  301. HDMGDATA hDmgData)
  302. {
  303.     SWP swp;
  304.  
  305.     UNUSED hConv;
  306.  
  307.     if (usType == XTYP_REGISTER && hszItem == hszTitle && !hconv) {
  308.         /*
  309.          * someone else came onboard, if we are looking for a target,
  310.          * restart our clock.
  311.          */
  312.         WinStartTimer(hab, hwndDemo, 1, TIMERSPEED);
  313.     }
  314.     
  315.     /*
  316.      * we only care about stuff on our topic.
  317.      */    
  318.     if (hszTopic != hszTopicChase)
  319.         return(0);
  320.  
  321.     switch (usType) {
  322.         
  323.     case XTYP_ADVSTART:
  324.         /*
  325.          * Always allow advises on our item
  326.          */
  327.         return(hszItem == hszItemPos);
  328.         break;
  329.  
  330.     case XTYP_ADVDATA:
  331.         /*
  332.          * Always accept advise data on our target's latest position.
  333.          */
  334.         if (hszItem == hszItemPos) 
  335.             DdeGetData(hDmgData, (PBYTE)&SWPTarget, sizeof(SWP), 0L);
  336.         DdeFreeData(hDmgData);
  337.         return(0);
  338.         break;
  339.                 
  340.     case XTYP_INIT:
  341.         /*
  342.          * always allow others to initiate with us on our topic.
  343.          */
  344.         return(hszItem == hszTitle && hszTopic == hszTopicChase);
  345.         break;
  346.  
  347.     case XTYP_REQUEST:
  348.     case XTYP_ADVREQ:
  349.         /*
  350.          * Respond to data requests as to our whereabouts item and format are
  351.          * ok.
  352.          */
  353.         if (hszItem != hszItemPos || usFmt != fmtSWP) 
  354.             return(0);
  355.         WinQueryWindowPos(hwndDemoFrame, &swp);
  356.         return(DdePutData((PBYTE)&swp, sizeof(SWP), 0L, hszItemPos, fmtSWP, 0));
  357.         break;
  358.  
  359.     default:
  360.         return(0);
  361.     }
  362. }
  363.  
  364.  
  365.  
  366.