📄 digitalctl.cpp
字号:
// DigitalCtl.cpp : Implementation of the CDigitalCtrl ActiveX Control class.
#include "stdafx.h"
#include "Digital.h"
#include "DigitalCtl.h"
#include "DigitalPpg.h"
#include "ChannelInfo.h"
#include "ChannelInfoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CDigitalCtrl, COleControl)
/////////////////////////////////////////////////////////////////////////////
// Message map
BEGIN_MESSAGE_MAP(CDigitalCtrl, COleControl)
//{{AFX_MSG_MAP(CDigitalCtrl)
ON_WM_TIMER()
ON_WM_CLOSE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_PROPERTY, OnProperty)
ON_COMMAND(IDM_CHANNEL, OnChannel)
ON_COMMAND(IDM_DAYSTAT, OnDaystat)
ON_COMMAND(IDM_HOURSTAT, OnHourstat)
ON_COMMAND(IDM_STATUS, OnStatus)
ON_COMMAND(IDM_REGULATE, OnRegulate)
ON_COMMAND(IDM_CONTROL, OnControl)
ON_COMMAND(IDM_ALARMSETTING, OnAlarmsetting)
ON_COMMAND(IDM_CHANNELINFO, OnChannelinfo)
ON_COMMAND(IDM_CHANNELSETTING, OnChannelsetting)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_COMMAND(IDM_CONFIRM, OnConfirm)
ON_COMMAND(IDM_HISTORYDATA, OnHistorydata)
//}}AFX_MSG_MAP
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(CDigitalCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CDigitalCtrl)
DISP_PROPERTY_NOTIFY(CDigitalCtrl, "nBit", m_nBit, OnNBitChanged, VT_I2)
DISP_FUNCTION(CDigitalCtrl, "Init", Init, VT_EMPTY, VTS_BSTR)
DISP_FUNCTION(CDigitalCtrl, "GetParameter", GetParameter, VT_BSTR, VTS_NONE)
DISP_FUNCTION(CDigitalCtrl, "SetFloatValue", SetFloatValue, VT_EMPTY, VTS_DATE VTS_BSTR VTS_R4)
DISP_FUNCTION(CDigitalCtrl, "SetFloatAlarm", SetFloatAlarm, VT_EMPTY, VTS_DATE VTS_BSTR VTS_I2 VTS_R4)
DISP_FUNCTION(CDigitalCtrl, "SetAlarmConfirm", SetAlarmConfirm, VT_EMPTY, VTS_DATE VTS_BSTR VTS_BSTR)
DISP_FUNCTION(CDigitalCtrl, "SetID", SetID, VT_EMPTY, VTS_I2)
DISP_FUNCTION(CDigitalCtrl, "SetEditMode", SetEditMode, VT_EMPTY, VTS_BOOL)
DISP_FUNCTION(CDigitalCtrl, "SetChannel", SetChannel, VT_EMPTY, VTS_PVARIANT)
DISP_FUNCTION(CDigitalCtrl, "HaveLink", HaveLink, VT_EMPTY, VTS_BOOL)
DISP_FUNCTION(CDigitalCtrl, "SetError", SetError, VT_EMPTY, VTS_DATE VTS_BSTR VTS_I2 VTS_BSTR)
//}}AFX_DISPATCH_MAP
DISP_FUNCTION_ID(CDigitalCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()
/////////////////////////////////////////////////////////////////////////////
// Event map
BEGIN_EVENT_MAP(CDigitalCtrl, COleControl)
//{{AFX_EVENT_MAP(CDigitalCtrl)
EVENT_CUSTOM("LBDown", FireLBDown, VTS_I2 VTS_I2)
EVENT_CUSTOM("DBClick", FireDBClick, VTS_I2)
EVENT_CUSTOM("ChannelReq", FireChannelReq, VTS_I2 VTS_BSTR)
EVENT_CUSTOM("ChannelInfo", FireChannelInfo, VTS_I2 VTS_BSTR VTS_I2)
EVENT_CUSTOM("ChannelDayStat", FireChannelDayStat, VTS_I2 VTS_BSTR)
EVENT_CUSTOM("ChannelHourStat", FireChannelHourStat, VTS_I2 VTS_BSTR)
EVENT_CUSTOM("Control", FireControl, VTS_I2 VTS_BSTR)
EVENT_CUSTOM("Regulate", FireRegulate, VTS_I2 VTS_BSTR)
EVENT_CUSTOM("Confirm", FireConfirm, VTS_I2 VTS_BSTR)
EVENT_CUSTOM("ChannelHistoryData", FireChannelHistoryData, VTS_I2 VTS_BSTR)
//}}AFX_EVENT_MAP
END_EVENT_MAP()
/////////////////////////////////////////////////////////////////////////////
// Property pages
// TODO: Add more property pages as needed. Remember to increase the count!
BEGIN_PROPPAGEIDS(CDigitalCtrl, 1)
PROPPAGEID(CDigitalPropPage::guid)
END_PROPPAGEIDS(CDigitalCtrl)
/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CDigitalCtrl, "DIGITAL.DigitalCtrl.1",
0xc35119f3, 0x6ff1, 0x4be4, 0xba, 0x75, 0x25, 0xf2, 0x7a, 0xa1, 0xde, 0x99)
/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
IMPLEMENT_OLETYPELIB(CDigitalCtrl, _tlid, _wVerMajor, _wVerMinor)
/////////////////////////////////////////////////////////////////////////////
// Interface IDs
const IID BASED_CODE IID_DDigital =
{ 0x54f8a86e, 0x8cc4, 0x41a0, { 0x95, 0x40, 0xe7, 0x47, 0x4c, 0x62, 0x82, 0x7e } };
const IID BASED_CODE IID_DDigitalEvents =
{ 0x2a95172, 0x8d02, 0x43ed, { 0x86, 0x24, 0x26, 0xa6, 0x7e, 0x64, 0xd, 0xd4 } };
/////////////////////////////////////////////////////////////////////////////
// Control type information
static const DWORD BASED_CODE _dwDigitalOleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
IMPLEMENT_OLECTLTYPE(CDigitalCtrl, IDS_DIGITAL, _dwDigitalOleMisc)
/////////////////////////////////////////////////////////////////////////////
// CDigitalCtrl::CDigitalCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CDigitalCtrl
BOOL CDigitalCtrl::CDigitalCtrlFactory::UpdateRegistry(BOOL bRegister)
{
// TODO: Verify that your control follows apartment-model threading rules.
// Refer to MFC TechNote 64 for more information.
// If your control does not conform to the apartment-model rules, then
// you must modify the code below, changing the 6th parameter from
// afxRegApartmentThreading to 0.
if (bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_DIGITAL,
IDB_DIGITAL,
afxRegApartmentThreading,
_dwDigitalOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
/////////////////////////////////////////////////////////////////////////////
// CDigitalCtrl::CDigitalCtrl - Constructor
CDigitalCtrl::CDigitalCtrl()
{
InitializeIIDs(&IID_DDigital, &IID_DDigitalEvents);
for(int i = 0; i< 12; i++)
{
HBITMAP temp = (HBITMAP)::CreateMappedBitmap(AfxGetApp()->m_hInstance, i+IDB_BITMAP1, 0, NULL, 0);
m_hBitmap[i] = (HBITMAP)::CopyImage( temp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG | LR_COPYDELETEORG);
}
HBITMAP temp = (HBITMAP)::CreateMappedBitmap(AfxGetApp()->m_hInstance,IDB_BITMAP13, 0, NULL, 0);
m_hBitmap[i] = (HBITMAP)::CopyImage( temp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG | LR_COPYDELETEORG);
m_nBit = 8;
m_flag = 3;
::GetObject(m_hBitmap[11], sizeof BITMAP, &m_bm);
SetColor(0);
m_nSetTime = 0;
m_f = -1000000;
m_HaveLink = false;
m_bError = false;
m_FlashTime = 1;
}
/////////////////////////////////////////////////////////////////////////////
// CDigitalCtrl::~CDigitalCtrl - Destructor
CDigitalCtrl::~CDigitalCtrl()
{
POSITION pos = m_mapChannelInfo.GetStartPosition();
CChannelInfo* pInfo;
CString channelName;
while (pos != NULL)
{
m_mapChannelInfo.GetNextAssoc( pos, channelName, (CObject*&)pInfo );
delete pInfo;
}
for(int i = 0; i< 13; i++)
{
DeleteObject(m_hBitmap[i]);
}
}
/////////////////////////////////////////////////////////////////////////////
// CDigitalCtrl::OnDraw - Drawing function
void CDigitalCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
m_h = rcBounds.Height();
m_w = rcBounds.Width()/m_nBit;
SetFloat(m_f);
}
/////////////////////////////////////////////////////////////////////////////
// CDigitalCtrl::DoPropExchange - Persistence support
void CDigitalCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
// TODO: Call PX_ functions for each persistent custom property.
}
/////////////////////////////////////////////////////////////////////////////
// CDigitalCtrl::OnResetState - Reset control to default state
void CDigitalCtrl::OnResetState()
{
COleControl::OnResetState(); // Resets defaults found in DoPropExchange
// TODO: Reset any other control state here.
}
/////////////////////////////////////////////////////////////////////////////
// CDigitalCtrl::AboutBox - Display an "About" box to the user
void CDigitalCtrl::AboutBox()
{
CDialog dlgAbout(IDD_ABOUTBOX_DIGITAL);
dlgAbout.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CDigitalCtrl message handlers
void CDigitalCtrl::Output(UINT digit, UINT pos)
{
// draw the bitmap
CClientDC dc(this);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap* pBitmap = CBitmap::FromHandle( m_hBitmap[digit] );
CBitmap* pOldBitmap = dcMem.SelectObject(pBitmap);
dc.StretchBlt(m_w*pos, 0, m_w, m_h,
&dcMem, 0, 0, m_bm.bmWidth, m_bm.bmHeight, SRCCOPY);
dcMem.SelectObject(pOldBitmap);
dcMem.DeleteDC();
}
void CDigitalCtrl::Init(LPCTSTR param)
{
CString strTemp,strTemp1,strTemp2;
strTemp2 = param;
int count = strTemp2.GetLength();
strTemp1 = strTemp2.Left(count-2);
strTemp = strTemp2.Right(1);
sscanf((LPCTSTR)strTemp,"%1d",&m_FlashTime);
// strTemp1 = param;
m_nBit = ::atoi((LPCTSTR)strTemp1);
InvalidateControl();
}
BSTR CDigitalCtrl::GetParameter()
{
CString strResult;
strResult.Format("%d",m_nBit);
return strResult.AllocSysString();
}
void CDigitalCtrl::SetFloat(float f)
{
m_f = f;
if(f == -1000000)
{
for(int i=0;i<m_nBit;i++)
{
Output(12,i);
}
return;
}
CString s;
try
{
s.Format(m_Format,f);
s.TrimLeft();
s.TrimRight();
}
catch(...)
{
s.Format("%f",f);
}
int bit = m_nBit - s.GetLength();
if(bit < 0)
bit = 0;
for(int i=0;i<m_nBit;i++)
{
char c;
if(i >= bit)
c = s[i-bit];
else
{
Output(11,i);
continue;
}
if(c == '.')
{
Output( 10,i);
TRACE("\n.");
}
else if(c == '-')
{
Output(12,i);
TRACE("\n-");
}
else
{
CString ss = c;
BYTE t = ::atoi(ss);
Output(t,i);
TRACE("\n%d",t);
}
}
}
void CDigitalCtrl::SetFloatValue(DATE Time, LPCTSTR Channel, float Value)
{
if(m_bError)
{
SetColor(0);
m_bError = false;
}
SetFloat(Value);
// InvalidateControl();
CChannelInfo* pInfo;
CString ChannelName;
POSITION pos;
pos = m_mapChannelInfo.GetStartPosition();
while (pos != NULL){ //only repeat once
m_mapChannelInfo.GetNextAssoc(pos,ChannelName,(CObject*&)pInfo );
pInfo->m_CurrentValue = Value;
pInfo->m_ValueTime = Time;
pInfo->m_ErrorString = "";
}
}
void CDigitalCtrl::SetColor(BYTE flag)
{
if(m_flag == flag)
return;
COLORREF NewColor;
if(flag == 4)
{
NewColor = RGB(0,0,255);
}
else if(flag == 3)
{
NewColor = RGB(255,0,0);
}
else if(flag == 2)
{
NewColor = RGB(255,0,255);
}
else if(flag == 1)
{
NewColor = RGB(255,255,0);
}
else
{
NewColor = RGB(0,255,0);
}
m_flag = flag;
for(int i = 0; i< 12; i++)
{
if(m_hBitmap[i])
::DeleteObject(m_hBitmap[i]);
COLORMAP mapColor;
mapColor.from = RGB(255,0,0);
mapColor.to = NewColor;
HBITMAP temp = (HBITMAP)::CreateMappedBitmap(AfxGetApp()->m_hInstance, i+IDB_BITMAP1, 0, &mapColor, 1);
m_hBitmap[i] = (HBITMAP)::CopyImage( temp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG | LR_COPYDELETEORG);
}
if(m_hBitmap[i])
::DeleteObject(m_hBitmap[i]);
COLORMAP mapColor;
mapColor.from = RGB(255,0,0);
mapColor.to = NewColor;
HBITMAP temp = (HBITMAP)::CreateMappedBitmap(AfxGetApp()->m_hInstance, IDB_BITMAP13, 0, &mapColor, 1);
m_hBitmap[i] = (HBITMAP)::CopyImage( temp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG | LR_COPYDELETEORG);
}
void CDigitalCtrl::SetFloatAlarm(DATE Time, LPCTSTR Channel, short Level, float Value)
{
SetColor(Level);
SetFloat(Value);
if(!m_nSetTime)
{
m_nSetTime = 2;
SetTimer(1,1000*m_FlashTime,NULL);
}
if(Level == 0)
m_nSetTime = 3;
CChannelInfo* pInfo;
CString ChannelName;
POSITION pos;
pos = m_mapChannelInfo.GetStartPosition();
while (pos != NULL){ //only repeat once
m_mapChannelInfo.GetNextAssoc(pos,ChannelName,(CObject*&)pInfo );
pInfo->m_CurrentValue = Value;
pInfo->m_AlarmTime = Time;
pInfo->m_ValueTime = Time;
pInfo->m_AlarmLevel = Level;
pInfo->m_ErrorString = "";
if(pInfo->m_AlarmLevel != Level)
{
pInfo->m_ConfirmOp = "";
pInfo->m_ConfirmTime = COleDateTime();
if( Level==0)
pInfo->m_AlarmTime = COleDateTime();
}
}
}
void CDigitalCtrl::SetAlarmConfirm(DATE Time, LPCTSTR Channel, LPCTSTR ConfirmOp)
{
if(m_nSetTime)
{
m_nSetTime = 3;
}
CChannelInfo* pInfo;
CString ChannelName;
POSITION pos;
pos = m_mapChannelInfo.GetStartPosition();
while (pos != NULL){ //only repeat once
m_mapChannelInfo.GetNextAssoc(pos,ChannelName,(CObject*&)pInfo );
pInfo->m_ConfirmOp = ConfirmOp;
pInfo->m_ConfirmTime = Time;
}
}
void CDigitalCtrl::OnTimer(UINT nIDEvent)
{
if(m_nSetTime == 2)
{
for(int i=0;i<m_nBit;i++)
{
Output(11,i);
}
m_nSetTime = 1;
}
else
{
SetFloat(m_f);
if(m_nSetTime == 3)
{
KillTimer(nIDEvent);
m_nSetTime = 0;
}
else
m_nSetTime = 2;
}
COleControl::OnTimer(nIDEvent);
}
void CDigitalCtrl::OnClose()
{
if(m_nSetTime)
{
KillTimer(1);
}
CWnd::OnClose();
}
void CDigitalCtrl::SetID(short id)
{
m_id=id;
}
void CDigitalCtrl::SetEditMode(BOOL bEdit)
{
m_bEditMode = bEdit;
}
void CDigitalCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// if(!m_bEditMode)
// return;
FireLBDown(m_id,0);
COleControl::OnLButtonDown(nFlags, point);
}
void CDigitalCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
if(m_bEditMode)
return;
FireDBClick(m_id);
COleControl::OnLButtonDblClk(nFlags, point);
}
void CDigitalCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
if(!m_bEditMode)
{
if (!menu.LoadMenu(IDR_RUNTIMEMENU))
return ;
CMenu* pSubMenu = menu.GetSubMenu(0);
if (!pSubMenu)
return ;
POSITION pos;
CChannelInfo* pInfo = NULL;
CString ChannelName;
pos = m_mapChannelInfo.GetStartPosition();
if(pos == NULL)
return;
while (pos != NULL){ //only repeat once
m_mapChannelInfo.GetNextAssoc(pos,ChannelName,(CObject*&)pInfo );
break; //just get the first element
}
if(pInfo != NULL) //check to see if 遥控或遥调菜单 should be added
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -