home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Building OCXs
/
Building_OCXs_Que_1995.iso
/
code
/
ch09
/
commctl.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1994-12-14
|
8KB
|
277 lines
// commctl.cpp : Implementation of the CCommCtrl OLE control class.
#include "stdafx.h"
#include "comm.h"
#include "commctl.h"
#include "commppg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#define DEF_PHONE "13031234567"
IMPLEMENT_DYNCREATE(CCommCtrl, COleControl)
/////////////////////////////////////////////////////////////////////////////
// Message map
BEGIN_MESSAGE_MAP(CCommCtrl, COleControl)
//{{AFX_MSG_MAP(CCommCtrl)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
//}}AFX_MSG_MAP
ON_OLEVERB(AFX_IDS_VERB_EDIT, OnEdit)
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(CCommCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CCommCtrl)
DISP_PROPERTY_NOTIFY(CCommCtrl, "PhoneNumber", m_phoneNumber, OnPhoneNumberChanged, VT_BSTR)
//}}AFX_DISPATCH_MAP
DISP_FUNCTION_ID(CCommCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()
/////////////////////////////////////////////////////////////////////////////
// Event map
BEGIN_EVENT_MAP(CCommCtrl, COleControl)
//{{AFX_EVENT_MAP(CCommCtrl)
//}}AFX_EVENT_MAP
END_EVENT_MAP()
/////////////////////////////////////////////////////////////////////////////
// Property pages
// TODO: Add more property pages as needed. Remember to increase the count!
BEGIN_PROPPAGEIDS(CCommCtrl, 1)
PROPPAGEID(CCommPropPage::guid)
END_PROPPAGEIDS(CCommCtrl)
/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CCommCtrl, "COMM.CommCtrl.1",
0x5b83b080, 0x14f0, 0x11ce, 0x96, 0xf, 0x52, 0x41, 0x53, 0x48, 0x0, 0x5)
/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
IMPLEMENT_OLETYPELIB(CCommCtrl, _tlid, _wVerMajor, _wVerMinor)
/////////////////////////////////////////////////////////////////////////////
// Interface IDs
const IID BASED_CODE IID_DComm =
{ 0x5b83b081, 0x14f0, 0x11ce, { 0x96, 0xf, 0x52, 0x41, 0x53, 0x48, 0x0, 0x5 } };
const IID BASED_CODE IID_DCommEvents =
{ 0x5b83b082, 0x14f0, 0x11ce, { 0x96, 0xf, 0x52, 0x41, 0x53, 0x48, 0x0, 0x5 } };
/////////////////////////////////////////////////////////////////////////////
// Control type information
static const DWORD BASED_CODE _dwCommOleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
IMPLEMENT_OLECTLTYPE(CCommCtrl, IDS_COMM, _dwCommOleMisc)
/////////////////////////////////////////////////////////////////////////////
// CCommCtrl::CCommCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CCommCtrl
BOOL CCommCtrl::CCommCtrlFactory::UpdateRegistry(BOOL bRegister)
{
if (bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_COMM,
IDB_COMM,
TRUE, // Insertable
_dwCommOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
/////////////////////////////////////////////////////////////////////////////
// CCommCtrl::CCommCtrl - Constructor
CCommCtrl::CCommCtrl()
{
InitializeIIDs(&IID_DComm, &IID_DCommEvents);
// TODO: Initialize your control's instance data here.
}
/////////////////////////////////////////////////////////////////////////////
// CCommCtrl::~CCommCtrl - Destructor
CCommCtrl::~CCommCtrl()
{
// TODO: Cleanup your control's instance data here.
}
/////////////////////////////////////////////////////////////////////////////
// CCommCtrl::OnDraw - Drawing function
void CCommCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
// TODO: Replace the following code with your own drawing code.
CBitmap bitmap;
BITMAP bmp;
CPictureHolder picHolder;
CRect rcSrcBounds;
// Load clock bitmap
bitmap.LoadBitmap(IDB_BITMAP1);
bitmap.GetObject(sizeof(BITMAP), &bmp);
rcSrcBounds.right = bmp.bmWidth;
rcSrcBounds.bottom = bmp.bmHeight;
// Create picture and render
picHolder.CreateFromBitmap((HBITMAP)bitmap.m_hObject, NULL, FALSE);
picHolder.Render(pdc, rcBounds, rcSrcBounds);
}
/////////////////////////////////////////////////////////////////////////////
// CCommCtrl::DoPropExchange - Persistence support
void CCommCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
// TODO: Call PX_ functions for each persistent custom property.
PX_String(pPX,_T("PhoneNumber"),m_phoneNumber,DEF_PHONE);
}
/////////////////////////////////////////////////////////////////////////////
// CCommCtrl::OnResetState - Reset control to default state
void CCommCtrl::OnResetState()
{
COleControl::OnResetState(); // Resets defaults found in DoPropExchange
// TODO: Reset any other control state here.
}
/////////////////////////////////////////////////////////////////////////////
// CCommCtrl::AboutBox - Display an "About" box to the user
void CCommCtrl::AboutBox()
{
CDialog dlgAbout(IDD_ABOUTBOX_COMM);
dlgAbout.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CCommCtrl message handlers
//Phone number has changed in the property page
void CCommCtrl::OnPhoneNumberChanged()
{
// TODO: Add notification handler code
SetModifiedFlag(TRUE);
}
//Dial number
void CCommCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
LPDCB lpDevConBlk;
DWORD dwLstErr;
HANDLE hCommDev;
int iloop;
CString cCommStr;
//Build a device control block
if(!BuildCommDCB("COM2: baud=1200 parity=n data=8 stop=1",lpDevConBlk))
dwLstErr = GetLastError();
//Open a handle for the communications port
hCommDev = CreateFile("COM2:",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,NULL);
//Setup the communications port, bypass if an error occurs
if(!SetCommState(hCommDev,lpDevConBlk))
MessageBox("Cannot Setup Comm Device");
//Build a comm string to submit to modem
cCommStr = "atdt" + m_phoneNumber;
//Send each character from the property page phone number
for(iloop=0;iloop<cCommStr.GetLength();iloop++)
{
TransmitCommChar(hCommDev,cCommStr[iloop]);
Sleep(10);
}
//Send a carriage return line-feed to notify the modem that the # is complete
TransmitCommChar(hCommDev,0x0D);
Sleep(10);
TransmitCommChar(hCommDev,0x0A);
//Wait for 5 seconds
Sleep(5000);
cCommStr = "ath";
//Send each character from the property page phone number
for(iloop=0;iloop<cCommStr.GetLength();iloop++)
{
TransmitCommChar(hCommDev,cCommStr[iloop]);
Sleep(10);
}
//Send a carriage return line-feed to notify the modem that the command is complete
TransmitCommChar(hCommDev,0x0D);
Sleep(10);
TransmitCommChar(hCommDev,0x0A);
//Close the device attached to this handle
CloseHandle(hCommDev);
COleControl::OnLButtonDown(nFlags, point);
}
//Display about box
void CCommCtrl::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDialog dlgAbout(IDD_ABOUTBOX_COMM);
dlgAbout.DoModal();
COleControl::OnRButtonDown(nFlags, point);
}