home *** CD-ROM | disk | FTP | other *** search
/ Building OCXs / Building_OCXs_Que_1995.iso / code / ch09 / commctl.cpp < prev    next >
C/C++ Source or Header  |  1994-12-14  |  8KB  |  277 lines

  1. // commctl.cpp : Implementation of the CCommCtrl OLE control class.
  2.  
  3. #include "stdafx.h"
  4. #include "comm.h"
  5. #include "commctl.h"
  6. #include "commppg.h"
  7.  
  8.  
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char BASED_CODE THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. #define DEF_PHONE "13031234567"
  15.  
  16. IMPLEMENT_DYNCREATE(CCommCtrl, COleControl)
  17.  
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Message map
  21.  
  22. BEGIN_MESSAGE_MAP(CCommCtrl, COleControl)
  23.     //{{AFX_MSG_MAP(CCommCtrl)
  24.     ON_WM_LBUTTONDOWN()
  25.     ON_WM_RBUTTONDOWN()
  26.     //}}AFX_MSG_MAP
  27.     ON_OLEVERB(AFX_IDS_VERB_EDIT, OnEdit)
  28.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  29. END_MESSAGE_MAP()
  30.  
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // Dispatch map
  34.  
  35. BEGIN_DISPATCH_MAP(CCommCtrl, COleControl)
  36.     //{{AFX_DISPATCH_MAP(CCommCtrl)
  37.     DISP_PROPERTY_NOTIFY(CCommCtrl, "PhoneNumber", m_phoneNumber, OnPhoneNumberChanged, VT_BSTR)
  38.     //}}AFX_DISPATCH_MAP
  39.     DISP_FUNCTION_ID(CCommCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  40. END_DISPATCH_MAP()
  41.  
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // Event map
  45.  
  46. BEGIN_EVENT_MAP(CCommCtrl, COleControl)
  47.     //{{AFX_EVENT_MAP(CCommCtrl)
  48.     //}}AFX_EVENT_MAP
  49. END_EVENT_MAP()
  50.  
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // Property pages
  54.  
  55. // TODO: Add more property pages as needed.  Remember to increase the count!
  56. BEGIN_PROPPAGEIDS(CCommCtrl, 1)
  57.     PROPPAGEID(CCommPropPage::guid)
  58. END_PROPPAGEIDS(CCommCtrl)
  59.  
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // Initialize class factory and guid
  63.  
  64. IMPLEMENT_OLECREATE_EX(CCommCtrl, "COMM.CommCtrl.1",
  65.     0x5b83b080, 0x14f0, 0x11ce, 0x96, 0xf, 0x52, 0x41, 0x53, 0x48, 0x0, 0x5)
  66.  
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // Type library ID and version
  70.  
  71. IMPLEMENT_OLETYPELIB(CCommCtrl, _tlid, _wVerMajor, _wVerMinor)
  72.  
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // Interface IDs
  76.  
  77. const IID BASED_CODE IID_DComm =
  78.         { 0x5b83b081, 0x14f0, 0x11ce, { 0x96, 0xf, 0x52, 0x41, 0x53, 0x48, 0x0, 0x5 } };
  79. const IID BASED_CODE IID_DCommEvents =
  80.         { 0x5b83b082, 0x14f0, 0x11ce, { 0x96, 0xf, 0x52, 0x41, 0x53, 0x48, 0x0, 0x5 } };
  81.  
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // Control type information
  85.  
  86. static const DWORD BASED_CODE _dwCommOleMisc =
  87.     OLEMISC_ACTIVATEWHENVISIBLE |
  88.     OLEMISC_SETCLIENTSITEFIRST |
  89.     OLEMISC_INSIDEOUT |
  90.     OLEMISC_CANTLINKINSIDE |
  91.     OLEMISC_RECOMPOSEONRESIZE;
  92.  
  93. IMPLEMENT_OLECTLTYPE(CCommCtrl, IDS_COMM, _dwCommOleMisc)
  94.  
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CCommCtrl::CCommCtrlFactory::UpdateRegistry -
  98. // Adds or removes system registry entries for CCommCtrl
  99.  
  100. BOOL CCommCtrl::CCommCtrlFactory::UpdateRegistry(BOOL bRegister)
  101. {
  102.     if (bRegister)
  103.         return AfxOleRegisterControlClass(
  104.             AfxGetInstanceHandle(),
  105.             m_clsid,
  106.             m_lpszProgID,
  107.             IDS_COMM,
  108.             IDB_COMM,
  109.             TRUE,                       //  Insertable
  110.             _dwCommOleMisc,
  111.             _tlid,
  112.             _wVerMajor,
  113.             _wVerMinor);
  114.     else
  115.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  116. }
  117.  
  118.  
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CCommCtrl::CCommCtrl - Constructor
  121.  
  122. CCommCtrl::CCommCtrl()
  123. {
  124.     InitializeIIDs(&IID_DComm, &IID_DCommEvents);
  125.  
  126.     // TODO: Initialize your control's instance data here.
  127. }
  128.  
  129.  
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CCommCtrl::~CCommCtrl - Destructor
  132.  
  133. CCommCtrl::~CCommCtrl()
  134. {
  135.     // TODO: Cleanup your control's instance data here.
  136. }
  137.  
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140. // CCommCtrl::OnDraw - Drawing function
  141.  
  142. void CCommCtrl::OnDraw(
  143.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  144. {
  145.     // TODO: Replace the following code with your own drawing code.
  146.     CBitmap bitmap;
  147.     BITMAP  bmp;
  148.     CPictureHolder picHolder;
  149.     CRect rcSrcBounds;
  150.  
  151.     // Load clock bitmap
  152.     bitmap.LoadBitmap(IDB_BITMAP1);
  153.     bitmap.GetObject(sizeof(BITMAP), &bmp);
  154.     rcSrcBounds.right = bmp.bmWidth;
  155.     rcSrcBounds.bottom = bmp.bmHeight;
  156.  
  157.     // Create picture and render
  158.     picHolder.CreateFromBitmap((HBITMAP)bitmap.m_hObject, NULL, FALSE);
  159.     picHolder.Render(pdc, rcBounds, rcSrcBounds);
  160.  
  161. }
  162.  
  163.  
  164. /////////////////////////////////////////////////////////////////////////////
  165. // CCommCtrl::DoPropExchange - Persistence support
  166.  
  167. void CCommCtrl::DoPropExchange(CPropExchange* pPX)
  168. {
  169.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  170.     COleControl::DoPropExchange(pPX);
  171.     // TODO: Call PX_ functions for each persistent custom property.
  172.     PX_String(pPX,_T("PhoneNumber"),m_phoneNumber,DEF_PHONE);
  173. }
  174.  
  175.  
  176. /////////////////////////////////////////////////////////////////////////////
  177. // CCommCtrl::OnResetState - Reset control to default state
  178.  
  179. void CCommCtrl::OnResetState()
  180. {
  181.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  182.  
  183.     // TODO: Reset any other control state here.
  184. }
  185.  
  186.  
  187. /////////////////////////////////////////////////////////////////////////////
  188. // CCommCtrl::AboutBox - Display an "About" box to the user
  189.  
  190. void CCommCtrl::AboutBox()
  191. {
  192.     CDialog dlgAbout(IDD_ABOUTBOX_COMM);
  193.     dlgAbout.DoModal();
  194. }
  195.  
  196.  
  197. /////////////////////////////////////////////////////////////////////////////
  198. // CCommCtrl message handlers
  199.  
  200. //Phone number has changed in the property page
  201. void CCommCtrl::OnPhoneNumberChanged() 
  202. {
  203.     // TODO: Add notification handler code
  204.     SetModifiedFlag(TRUE);
  205. }
  206.  
  207. //Dial number
  208. void CCommCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
  209. {                              
  210.     // TODO: Add your message handler code here and/or call default
  211.     LPDCB    lpDevConBlk;
  212.     DWORD    dwLstErr;
  213.     HANDLE    hCommDev;
  214.     int     iloop;     
  215.     CString    cCommStr;
  216.  
  217.     //Build a device control block
  218.     if(!BuildCommDCB("COM2: baud=1200 parity=n data=8 stop=1",lpDevConBlk))
  219.         dwLstErr = GetLastError();
  220.  
  221.     //Open a handle for the communications port
  222.     hCommDev = CreateFile("COM2:",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,
  223.         FILE_ATTRIBUTE_NORMAL,NULL);
  224.  
  225.     //Setup the communications port, bypass if an error occurs
  226.     if(!SetCommState(hCommDev,lpDevConBlk))
  227.         MessageBox("Cannot Setup Comm Device");
  228.     
  229.     //Build a comm string to submit to modem
  230.     cCommStr = "atdt" + m_phoneNumber;
  231.  
  232.     //Send each character from the property page phone number
  233.     for(iloop=0;iloop<cCommStr.GetLength();iloop++)
  234.     {
  235.         TransmitCommChar(hCommDev,cCommStr[iloop]);
  236.         Sleep(10);
  237.     }
  238.  
  239.     //Send a carriage return line-feed to notify the modem that the # is complete
  240.     TransmitCommChar(hCommDev,0x0D);
  241.     Sleep(10);
  242.     TransmitCommChar(hCommDev,0x0A);
  243.  
  244.     //Wait for 5 seconds
  245.     Sleep(5000);
  246.  
  247.     cCommStr = "ath";
  248.  
  249.     //Send each character from the property page phone number
  250.     for(iloop=0;iloop<cCommStr.GetLength();iloop++)
  251.     {
  252.         TransmitCommChar(hCommDev,cCommStr[iloop]);
  253.         Sleep(10);
  254.     }
  255.  
  256.     //Send a carriage return line-feed to notify the modem that the command is complete
  257.     TransmitCommChar(hCommDev,0x0D);
  258.     Sleep(10);
  259.     TransmitCommChar(hCommDev,0x0A);
  260.  
  261.     //Close the device attached to this handle
  262.     CloseHandle(hCommDev);
  263.     
  264.     COleControl::OnLButtonDown(nFlags, point);
  265. }
  266.  
  267. //Display about box
  268. void CCommCtrl::OnRButtonDown(UINT nFlags, CPoint point) 
  269. {
  270.     // TODO: Add your message handler code here and/or call default
  271.     CDialog dlgAbout(IDD_ABOUTBOX_COMM);
  272.     dlgAbout.DoModal();
  273.         
  274.     COleControl::OnRButtonDown(nFlags, point);
  275. }
  276.  
  277.