我有个信号灯的图,1秒绿,1秒变红,可是因为不是在onpaint里面画的,所以最小化后,或者移出频幕再移回来就消失了。因为图是动态的,不知道怎样画在onpaint里面,所以就画在外面了,有没有方法可以既不再onpaint里面写绘图函数,又可以不被刷新掉的方法?
解决方案 »
- ¥100 $$$ 为什么用WS_EX_TOOLWINDOW 属性的窗口老往后跑? Z次序问题 $$$ ¥100
- 怎么用VS 2003编译单个的C++文件
- 如何在excel里插入一行!!请高手指点!!
- 如下面语句能把dataName数据中的内容赋到buf中吗?
- 求系统分析员考试论文
- 怎样把下拉列表框设置为只读属性??
- [ADO] 怪事,空表里难道已经预留了一条记录?帮我看看这个SQL语句对吗?
- 怎样得到金山词霸所提供的单词
- windows下,我要封装一个支持200套接字收发数据的模块,选哪个网络模型?
- 关于ADO连SQL Server的delete的问题
- 100分请教高手shift键加tab键同时按下键值是多少?
- 得到窗口指针
然后,在定时器里面,也要画,避免每次调用刷新!
在线程里控制变量然后Invalidate
设你要画不同的图片
1.在OnPaint 所在窗口建立一图片路径变量m_a;
2.在onpaint 根据m_a把图片画上
3.当你获得了其他图片后,把图片路径赋给变量m_a
然后让OnPaint所在的窗口重绘界面:
pMainWnd->Invalidate();
{
CPaintDC dc(this);
if (m_bRedLight)
{
// Draw Red light
}
else
{
// Draw green light
}
}void CYourWnd::OnTimer(...)
{
m_bRedLight = TRUE or FALSE;
Invalidate();
}
然后再实现SetState函数SetState(0)//设为绿色状态
SetState(1)//设为红色状态这样以后调用,就比较简单,而且可以复用。如果需要我可以发一个给你
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
led.cpp
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Led.cpp : implementation file
//#include "stdafx.h"
#include "DataProcess.h"
#include "Led.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CLedCLed::CLed()
{
m_LedBitmap.LoadBitmap(IDB_LEDS);
m_nLedColor = LED_COLOR_RED;
m_nLedMode = LED_OFF;
m_nLedShape = LED_ROUND;
}CLed::~CLed()
{
VERIFY(m_LedBitmap.DeleteObject());
}
BEGIN_MESSAGE_MAP(CLed, CStatic)
//{{AFX_MSG_MAP(CLed)
ON_WM_CREATE()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CLed message handlersint CLed::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
return 0;
}BOOL CLed::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
}void CLed::OnPaint()
{
CPaintDC dc(this); // device context for painting
DrawLed(&dc,m_nLedColor,m_nLedMode,m_nLedShape);
// TODO: Add your message handler code here
// Do not call CStatic::OnPaint() for painting messages
}void CLed::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent == PING_TIMER)
{
SetLed(m_nLedColor,CLed::LED_OFF,m_nLedShape);
KillTimer(nIDEvent);
m_bPingEnabled = FALSE;
}
CStatic::OnTimer(nIDEvent);
}///////////////////////////////////////////////////////////////////////////////
// Name: SetLed
// Description: This method will draw the LED to the specified DC.
//
// Entry:
// CDC *pDC - DC to draw to
//
// int iLedColor - Where color is defined by:
// LED_COLOR_RED
// LED_COLOR_GREEN
// LED_COLOR_YELLOW
// LED_COLOR_BLUE
//
// int iMode - where mode is defined by:
// LED_ON
// LED_OFF
// LED_DISABLED
//
// int iShape - where shape is defined by:
// LED_ROUND
// LED_SQUARE
///////////////////////////////////////////////////////////////////////////////
void CLed::DrawLed(CDC *pDC,int nLEDColor, int nMode, int nShape)
{
CRect rect;
GetClientRect(&rect); //
// Center led within an oversized window
//
if(rect.Width() >= LED_SIZE && rect.Height() >= LED_SIZE)
{
int nWidth = rect.Width();
int nHeight = rect.Height();
rect.left += (nWidth - LED_SIZE)/2;
rect.right -= (nWidth - LED_SIZE)/2;
rect.top += (nHeight - LED_SIZE)/2;
rect.bottom -= (nHeight - LED_SIZE)/2;
} //
// Prepare temporary DCs and bitmaps
//
CBitmap TransBitmap;
TransBitmap.CreateBitmap(LED_SIZE,LED_SIZE,1,1,NULL);
CBitmap bitmapTemp;
CBitmap* pBitmap = &m_LedBitmap;
CDC srcDC;
CDC dcMask;
CDC TempDC;
TempDC.CreateCompatibleDC(pDC);
srcDC.CreateCompatibleDC(pDC);
dcMask.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = srcDC.SelectObject(pBitmap);
CBitmap* pOldMaskbitmap = dcMask.SelectObject(&TransBitmap);
bitmapTemp.CreateCompatibleBitmap(pDC,LED_SIZE,LED_SIZE); //
// Work with tempDC and bitmapTemp to reduce flickering
//
CBitmap *pOldBitmapTemp = TempDC.SelectObject(&bitmapTemp);
TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, pDC, rect.left, rect.top, SRCCOPY); //
// Create mask
//
COLORREF OldBkColor = srcDC.SetBkColor(RGB(255,0,255));
dcMask.BitBlt(0, 0, LED_SIZE, LED_SIZE,&srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCCOPY);
TempDC.SetBkColor(OldBkColor); //
// Using the IDB_LEDS bitmap, index into the bitmap for the appropriate
// LED. By using the mask color (RGB(255,0,255)) a mask has been created
// so the bitmap will appear transparent.
//
TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, &srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCINVERT);
TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE,&dcMask, 0, 0, SRCAND);
TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, &srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCINVERT); //
// Since the actual minipulation is done to tempDC so there is minimal
// flicker, it is now time to draw the result to the screen.
//
pDC->BitBlt(rect.left, rect.top, LED_SIZE, LED_SIZE, &TempDC, 0, 0, SRCCOPY);
//
// House cleaning
//
srcDC.SelectObject(pOldBitmap);
dcMask.SelectObject(pOldMaskbitmap);
TempDC.SelectObject(pOldBitmapTemp);
VERIFY(srcDC.DeleteDC());
VERIFY(dcMask.DeleteDC());
VERIFY(TempDC.DeleteDC());
VERIFY(TransBitmap.DeleteObject());
VERIFY(bitmapTemp.DeleteObject());
}///////////////////////////////////////////////////////////////////////////////
// Name: SetLed
// Description: This method will draw and set led parameters.
//
// Entry: int iLedColor - Where color is defined by:
// LED_COLOR_RED
// LED_COLOR_GREEN
// LED_COLOR_YELLOW
// LED_COLOR_BLUE
//
// int iMode - where mode is defined by:
// LED_ON
// LED_OFF
// LED_DISABLED
//
// int iShape - where shape is defined by:
// LED_ROUND
// LED_SQUARE
///////////////////////////////////////////////////////////////////////////////
void CLed::SetLed(int nLedColor, int nMode, int nShape)
{
m_nLedColor = nLedColor;
m_nLedMode = nMode;
m_nLedShape = nShape; CDC *pDC;
pDC = GetDC();
DrawLed(pDC,m_nLedColor,m_nLedMode,m_nLedShape);
ReleaseDC(pDC);
}///////////////////////////////////////////////////////////////////////////////
// Name: Ping
// Description: This method will turn the led on for dwTimeout milliseconds and
// then turn it off.
//
// Entry: DWORD dwTimeout - Time out in milliseconds
///////////////////////////////////////////////////////////////////////////////
void CLed::Ping(DWORD dwTimeout)
{
// Return if pinging
if(m_bPingEnabled == TRUE)
{
KillTimer(PING_TIMER);
} m_bPingEnabled = TRUE;
SetLed(m_nLedColor,CLed::LED_ON,m_nLedShape);
SetTimer(PING_TIMER,dwTimeout,NULL);
}
**************************************
led.h
**************************************
#if !defined(AFX_LED_H__BABC9426_EF66_49D5_B54F_EA10DD7A0344__INCLUDED_)
#define AFX_LED_H__BABC9426_EF66_49D5_B54F_EA10DD7A0344__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Led.h : header file
//
#define LED_SIZE 16 // Led are 16 X 16 pixels
#define PING_TIMER 10
/////////////////////////////////////////////////////////////////////////////
// CLed windowclass CLed : public CStatic
{
protected:
int m_nLedColor, m_nLedMode, m_nLedShape;
DWORD m_dwPingTimeout;
BOOL m_bPingEnabled;
CBitmap m_LedBitmap;public:
enum {
LED_ROUND = 0, // Circle starts at row 0
LED_SQUARE = LED_SIZE * 4, // squares start at row 4
};
enum {
LED_COLOR_RED = 0 * LED_SIZE, // Row 0
LED_COLOR_GREEN = 1 * LED_SIZE, // Row 1
LED_COLOR_YELLOW = 2 * LED_SIZE, // Row 2
LED_COLOR_BLUE = 3 * LED_SIZE, // Row 3
};
enum {
LED_ON = 0, // Column 0
LED_OFF = 1, // Column 1
LED_DISABLED = 2, // Column 2
};
// Construction
public:
CLed();// Attributes
public:// Operations
public:
void SetLed(int nLedColor, int nMode, int nShape);
int GetLedMode(){return m_nLedMode;}
void Ping(DWORD dwTimeout = 1000);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLed)
//}}AFX_VIRTUAL// Implementation
public:
void DrawLed( CDC *pDC, int nLEDColor, int nMode, int nShape );
virtual ~CLed(); // Generated message map functions
protected:
//{{AFX_MSG(CLed)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_LED_H__BABC9426_EF66_49D5_B54F_EA10DD7A0344__INCLUDED_)********************
调用方法
********************
CLed m_led1;
// 设为红色
m_led1.SetLed(CLed::LED_COLOR_RED,CLed::LED_ON,CLed::LED_SQUARE);
// 设为绿色
m_led1.SetLed(CLed::LED_COLOR_GREEN,CLed::LED_ON,CLed::LED_SQUARE);