1. 程式人生 > >VC6.0之MFC 單文件操作

VC6.0之MFC 單文件操作

CUSERMessage::CUSERMessage()
{
this->m_MessageReadPos = -1;
this->m_MessageWritePos = -1;
this->m_MessageSize = -1;
this->m_MessageCurSize = -1;
}


CUSERMessage::~CUSERMessage()
{
if (NULL != this->m_MessageQueue)
{
free(this->m_MessageQueue);
}
}


void CUSERMessage::SetUSERMessageQueueSize(UINT size)
{
if (0 == size)
{
AfxMessageBox("非法引數");
return;
}


this->m_MessageQueue = (_USERMessage*)malloc(size * sizeof(_USERMessage));


if (NULL == this->m_MessageQueue)
{
AfxMessageBox("訊息佇列分配失敗");
return;
}


this->m_MessageReadPos = 0;
this->m_MessageWritePos = 0;
this->m_MessageCurSize = 0;
this->m_MessageSize = size;
}


INT CUSERMessage::SetUSERMessageQueue(USERMessage msg)
{
if (-1 == this->m_MessageSize)
{
return -1;
}


if (this->m_MessageSize > this->m_MessageCurSize)
{
this->m_MessageCurSize++;


memcpy(&this->m_MessageQueue[this->m_MessageWritePos++], &msg, sizeof(_USERMessage));


if (this->m_MessageSize <= this->m_MessageWritePos)
{
this->m_MessageWritePos = 0;
}
}
else
{
return -2;
}


return 0;
}


INT CUSERMessage::GetUSERMessage(USERMessage *pMsg)
{
if (-1 == this->m_MessageSize)
{
return -1;
}


if (0 < this->m_MessageCurSize)
{
this->m_MessageCurSize--;


memcpy(pMsg, &this->m_MessageQueue[this->m_MessageReadPos++], sizeof(_USERMessage));


if (this->m_MessageSize <= this->m_MessageReadPos)
{
this->m_MessageReadPos = 0;
}
}
else
{
return -2;
}


return 0;
}