Learn to create STATIC class for alarm function with Jiang Chuan author Qi Gao
Introduction Alarm control is often seen in various industrial control systems. There are many methods for alarming, which can be marked by text or represented by graphics. The principle is nothing more than constructing a CDC class object and then using the various basic provided Just draw graphics using the primitive attributes. Here we will make a program that can display alarm text. When an alarm occurs, we can flash the background to achieve the function.
We generally use derived methods to implement this type of processing in the text.
First use the VC wizard to create a dialog-based project.
Then we place a STATIC control on the dialog box interface and set the position.
The basic preparations for deriving a CStatic-based class named CStaticRectangle for our STATIC control are complete. Our next task is to add content to the new derived class to achieve the desired alarm screen effect.
According to what I said in my last article, using the double buffering method, we need to add the three files MemDC.h, AutoFont.h, and AutoFont.cpp to the project. The AutoFont class is a better encapsulation of the CFont class on the Internet today. Users do not need to consider complex creation API functions when creating CFont objects. They only need to make a few simple parameter settings to complete the function, so we use this class here to implement font control of alarm text.
Define object
//The definition of color can be increased or decreased according to the actual situation.
#define COLOR_RED RGB(255,0,0)
#define COLOR_GREEN RGB(0,255,0)
protected:
CBitmap *m_pBitmapOldBackground;//These three are used for double buffer drawing
CBitmap m_bitmapBackground;
CDC m_dcBackground;
CRect m_rectCtrl; // control area
short nMillSec; //Timer
CAutoFont *m_pFnt; //Construct alarm text font
public:
COLORREF m_BkColor;//Background color
COLORREF m_TextColor;//Text color
CString strText; //Displayed alarm text
BOOL bGleam;//Whether it flashes
Initialize variables in the constructor:
CStaticRectangle::CStaticRectangle()
{
m_BkColor = COLOR_RED;
m_TextColor = RGB(255,128,64);
nMillSec = 10;
bGleam = FALSE;
m_pFnt = new CAutoFont("official script");
m_pFnt->SetHeight(28);
m_pFnt->SetWidth(12);
m_pFnt->SetBold(TRUE);
}
Memory is released in the destructor
CStaticRectangle::~CStaticRectangle()
{
if(m_pFnt)
delete m_pFnt;
}
To draw the STATIC alarm function, we only need to map the WM_PAINT message, and then redraw the interface and display the text.
void CStaticRectangle::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Get the control area
GetClientRect (&m_rectCtrl);
CMemDC memDC(&dc, &m_rectCtrl);
if(m_dcBackground.GetSafeHdc()== NULL|| (m_bitmapBackground.m_hObject == NULL))
{
m_dcBackground.CreateCompatibleDC(&dc);
m_bitmapBackground.CreateCompatibleBitmap(&dc, m_rectCtrl.Width(), m_rectCtrl.Height()) ;
m_pBitmapOldBackground = m_dcBackground.SelectObject(&m_bitmapBackground);
}
DrawRectangleBackground(&m_dcBackground, m_rectCtrl);
memDC.BitBlt(0, 0, m_rectCtrl.Width(), m_rectCtrl.Height(),
&m_dcBackground, 0, 0, SRCCOPY);
// Do not call CStatic::OnPaint() for painting messages
}
Next, we control the display of relevant information. What we need to pay attention to here is the display of our text. Here we need to calculate the position of the text display.
We can get the CSize of the text through pDC->GetTextExtent();, and then use the rect of the control to get the display position of the text.
The method used here is to display it in the center.
void CStaticRectangle::DrawRectangleBackground(CDC *pDC, CRect &rect)
{
CBrush brushFill, *pBrushOld;
int nXSize,nYSize;
CSize szText;
CFont *fOldFont;
nXSize = rect.Width();
nYSize = rect.Height();
brushFill.DeleteObject();
brushFill.CreateSolidBrush(m_BkColor);
pBrushOld = pDC->SelectObject(&brushFill);
pDC->Rectangle(rect);
pDC->SetBkColor(m_BkColor);
pDC->SetTextColor(m_TextColor);
fOldFont = (CFont *)pDC->SelectObject(m_pFnt);
szText = pDC->GetTextExtent(strText);
nXSize = (nXSize - szText.cx)/2;
nYSize = (nYSize - szText.cy)/2;
pDC->TextOut(nXSize,nYSize,strText);
pDC->SelectObject(pBrushOld);
pDC->SelectObject(&fOldFont);
brushFill.DeleteObject();
}
If we want to implement the alarm function, we need to use the timer function to change the background color regularly and then refresh the display.
At this point, the basic work has been completed. The next thing to do is to provide an interface that users can dynamically modify.
void CStaticRectangle::SetRectangleText(CString strVal)
{
this->strText = strVal;
}
void CStaticRectangle::SetBackColor(UINT nStyle)//Set the background color
{
this->m_BkColor = nStyle;
}
void CStaticRectangle::SetBkGleam(BOOL bShow)
{
if(this->bGleam)
{
if(!bShow)
KillTimer(nMillSec);
}
else
{
if(bShow)
SetTimer(nMillSec,750,NULL);
}
this->bGleam = bShow;
}
Well, now that our functional class has been completed, the next thing to do is to connect it with CSTATIC on the interface.
Create a STATIC control associated variable (Control type) through CLASSWIZARD, then modify the derived type to our CStaticRectangle, and set the flashing effect, and we can see the effect.
Expand