大家好,我目前就职于一家工业制造公司,我的email是wuxfei@gmail.com,工作近3年时间了。在设备制造时经常需要在操作界面显示设备状态。为了能在工程项目中快速添加状态显示灯,我对CStatic做了子类化。主要提供了这三个接口:
void SetWindowText(LPCTSTR lpszString);//设置文字信息
void StartFlash(COLORREF color);//开始闪烁
void StopFlash(COLORREF color);//停止闪烁
通过调用这三个接口函数即可快速在软件界面先生设备状态。若未调用SetWindowText设置文字信息,将使用默认文字信息(对话框资源)。
通过开启线程来实现闪烁,GetTickCount作为计时工具。为了主程序退出时能正常关闭子线程,使用了事件。
使用效果如下:
设备零件有故障时:
设备零件正常工作:
优点:能快速重用,不依赖额外资源文件。
缺点:单色的,不是很漂亮。
本人能力有限,时间仓促,若有问题请指正。
头文件如下:
#if !defined(AFX_LAMPSTATIC_H__D049CF00_2318_4950_9F0D_BA68968EE981__INCLUDED_)
#define AFX_LAMPSTATIC_H__D049CF00_2318_4950_9F0D_BA68968EE981__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// LampStatic.h : header file
//
#ifdef _UTILITYEXT
#define UTILITY_EXT_CLASS __declspec(dllexport)
#else
#define UTILITY_EXT_CLASS __declspec(dllimport)
#endif
/
// CLampStatic window
#define RED_COLOR RGB(255,0,0)
#define GREEN_COLOR RGB(0,255,0)
class UTILITY_EXT_CLASS CLampStatic : public CStatic
{
// Construction
public:
CLampStatic();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLampStatic)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CLampStatic();
// Generated message map functions
protected:
//{{AFX_MSG(CLampStatic)
afx_msg void OnPaint();
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
void SetWindowText(LPCTSTR lpszString);//设置文字信息
void StartFlash(COLORREF color);//开始闪烁
void StopFlash(COLORREF color);//停止闪烁
private:
void FlashThread(void);
private:
HANDLE m_FlashStopEvent;//闪烁线程停止
bool m_bStopFlash; //停止闪烁
COLORREF m_color;
friend UINT FlashThread(LPVOID lp);
};
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_LAMPSTATIC_H__D049CF00_2318_4950_9F0D_BA68968EE981__INCLUDED_)
实现如下:
// LampStatic.cpp : implementation file
//
#include "stdafx.h"
#include "LampStatic.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CLampStatic
CLampStatic::CLampStatic()
{
m_color = GREEN_COLOR;
m_FlashStopEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
m_bStopFlash = true;
}
CLampStatic::~CLampStatic()
{
StopFlash(m_color);
}
UINT FlashThread(LPVOID lp)
{
((CLampStatic*)lp)->FlashThread();
return S_OK;
}
BEGIN_MESSAGE_MAP(CLampStatic, CStatic)
//{{AFX_MSG_MAP(CLampStatic)
ON_WM_PAINT()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CLampStatic message handlers
void CLampStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
CString strText;
GetWindowText(strText);
CPen pen(PS_SOLID, 1, RGB(255,255,255));
CBrush brush(m_color);
dc.SetBkMode(TRANSPARENT);
dc.SelectObject(&brush);
dc.SelectObject(&pen);
dc.SelectObject(this->GetFont());
CRect rect, rect2;
GetClientRect(&rect);
GetClientRect(&rect2);
rect.right = rect.bottom = (rect.right>rect.bottom) ? rect.bottom: rect.right;
rect = rect.MulDiv(2, 3);
//显示文字
rect2.OffsetRect(0, rect.bottom);
dc.DrawText(strText, rect2, DT_CENTER);
//绘制灯
rect.OffsetRect(rect2.CenterPoint().x-rect.Width()/2, 0);
dc.Ellipse(rect);
}
void CLampStatic::FlashThread()
{
ResetEvent(m_FlashStopEvent);
COLORREF colorBak = m_color;
const int SHOW=300, HIDE=200;
DWORD dwStart=GetTickCount(), dwNow;
while (!m_bStopFlash)
{
dwNow = GetTickCount();
if (dwNow-dwStart<HIDE)
{
m_color = RGB(239,235,222);
Invalidate();
}
if (dwNow-dwStart>HIDE && dwNow-dwStart<SHOW+HIDE)
{
m_color = colorBak;
Invalidate();
}
if (dwNow-dwStart>SHOW+HIDE)
{
dwStart = GetTickCount();
}
Sleep(30);
}
SetEvent(m_FlashStopEvent);
}
void CLampStatic::StartFlash(COLORREF color)
{
StopFlash(m_color);
m_color = color;
m_bStopFlash = false;
AfxBeginThread(::FlashThread, this);
}
void CLampStatic::StopFlash(COLORREF color)
{
m_color = color;
m_bStopFlash = true;
WaitForSingleObject(m_FlashStopEvent, 3000);
Sleep(100);
TRACE(_T("StopFlash"));
}
void CLampStatic::OnDestroy()
{
StopFlash(m_color);
CStatic::OnDestroy();
}
void CLampStatic::SetWindowText(LPCTSTR lpszString)
{
CStatic::SetWindowText(lpszString);
if (GetParent())
{
GetParent()->RedrawWindow();
}
}