Author name Qi Gao Original source of the article Original introduction Recently, a person has been responsible for the development task of a hydropower station in a place in Guizhou. In implementing the interface monitoring system module, I have to use many switches and other things that represent the current operating status of the equipment. After searching on the Internet, I found that there is no good stuff in this area. Since it does not involve control, I used the method of deriving the CSTATIC class to map the WM_PAINT message to implement the function. I will post this class and pictures now, hoping to help. Industrial control and those who are studying this area are helpful.
The text control is implemented in the form of background maps, that is, different map operations are performed under different conditions to dynamically represent the operating status of the device. Since this time it represents the status of the switch, I used two pictures for replacement display. If there are many situations, it can be expanded and used. The picture is drawn by me using WINDOWS drawing board. If anyone draws a more beautiful switch style, please let me know. (^_^, my level is limited)
The program is very simple, the approximate code is as follows:
void CSwitchStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRectrect;
GetClientRect(&rect);
CDC *mdc=new CDC;
mdc->CreateCompatibleDC(&dc);
HBITMAP hbitmap;
switch(this->m_SystemMetric)
{
case STYLE_ONE:
if(!this->bSwitchOn)
hbitmap=::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_SWITCHOFF));
else
hbitmap=::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_SWITCHON));
break;
case STYLE_TWO:
if(!this->bSwitchOn)
hbitmap=::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_SWITCHOFF1));
else
hbitmap=::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_SWITCHON1));
break;
default:
if(!this->bSwitchOn)
hbitmap=::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_SWITCHOFF));
else
hbitmap=::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_SWITCHON));
break;
}
mdc->SelectObject(hbitmap);
dc.BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),mdc,0,0,SRCCOPY);
ReleaseDC(mdc);
::DeleteObject(hbitmap);
// Do not call CStatic::OnPaint() for painting messages
}
The calling method is as follows:
I set up two switch operating modes and two switch status styles in the program. The program provides a dynamic setting method:
The default is SwitchOff. If you want to change the state, you can use the following code:
SetSwitchState(TRUE);
If we want to change the switch display style, we can use the following code:
SetSwitchStyle(STYLE_TWO);//The second style
SetSwitchStyle(STYLE_ONE);//The first style
Expand