void CPlatform::OnPaint() //CPlatform,自定义的类!
{
CPaintDC MainPaint(this); // device context for painting
// 获得控件区域
GetClientRect (&m_PlatformArea);
//m_PlatformArea.
//绘制边框和底色!
CPen Frame(PS_SOLID ,0,RGB(6,6,220));//边框线!
CBrush Background(RGB(252,249,220));//仿真平台底色
MainPaint.SelectObject(&Frame);
MainPaint.SelectObject(Background);
MainPaint.Rectangle(&m_PlatformArea);
// Do not call CStatic::OnPaint() for painting messages
}请问为什么会出现闪烁?该如何解决啊?
我看些东西,有什么双缓冲的,该怎么用,怎么解决啊?
还有什么用CMemDC的,怎么解决啊?
谢谢!!
{
CPaintDC MainPaint(this); // device context for painting
// 获得控件区域
GetClientRect (&m_PlatformArea);
//m_PlatformArea.
//绘制边框和底色!
CPen Frame(PS_SOLID ,0,RGB(6,6,220));//边框线!
CBrush Background(RGB(252,249,220));//仿真平台底色
MainPaint.SelectObject(&Frame);
MainPaint.SelectObject(Background);
MainPaint.Rectangle(&m_PlatformArea);
// Do not call CStatic::OnPaint() for painting messages
}请问为什么会出现闪烁?该如何解决啊?
我看些东西,有什么双缓冲的,该怎么用,怎么解决啊?
还有什么用CMemDC的,怎么解决啊?
谢谢!!
用CreateCompatibleDC创建与绘图窗口兼容的内存DC,在内存DC中输出文本或绘图,
在OnPaint()时再用BitBlt拷到窗口DC
{
return true;
}
#if !defined(AFX_PIC_H__2668110A_9713_4413_B602_4CAB63F4F23E__INCLUDED_)
#define AFX_PIC_H__2668110A_9713_4413_B602_4CAB63F4F23E__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Pic.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CPic windowclass CPic : public CStatic
{
// Construction
public:
CPic();
int GetPicWidth() {return m_Width;};
int GetPicHeight() {return m_Height;};
// Attributes
public:
CBitmap m_Bmp;
protected:
CDC m_MemDC;//绘图用的内存DC
CRect m_RectPic;//绘图区域矩形
CBrush m_Brush;//填充刷子
CPen m_BlackSolidPen;//黑色笔
CPen m_RedSolidPen;//红色笔
CPen m_GreenSolidPen;//绿色笔
CFont SmallFont;//小字体
int m_Width;//绘图区域的长度
int m_Height;//绘图区域的高度
// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPic)
//}}AFX_VIRTUAL// Implementation
public:
void Init();
virtual ~CPic(); // Generated message map functions
protected:
//{{AFX_MSG(CPic)
afx_msg void OnPaint();
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_PIC_H__2668110A_9713_4413_B602_4CAB63F4F23E__INCLUDED_)
// Pic.cpp : implementation file
//#include "stdafx.h"
#include "Pic.h"
#include "resource.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static const COLORREF BLACK = RGB(0, 0, 0);
static const COLORREF WHITE = RGB(255, 255, 255);
static const COLORREF GREEN = RGB(0, 255, 0);
static const COLORREF BLUE = RGB(0, 0, 255);
static const COLORREF RED = RGB(255,100, 0);/////////////////////////////////////////////////////////////////////////////
// CPicCPic::CPic()
{
m_Width = 0;
m_Height = 0;
m_Brush.CreateSolidBrush(BLACK);
m_GreenSolidPen.CreatePen(PS_SOLID, 1, GREEN);
m_RedSolidPen.CreatePen(PS_SOLID, 1, RED);
m_BlackSolidPen.CreatePen(PS_SOLID, 1, BLACK);
SmallFont.CreatePointFont(120, "宋体");
}CPic::~CPic()
{
if (&m_Brush)
m_Brush.DeleteObject();
if (&m_GreenSolidPen)
m_GreenSolidPen.DeleteObject();
if (&m_RedSolidPen)
m_RedSolidPen.DeleteObject();
if (&m_BlackSolidPen)
m_BlackSolidPen.DeleteObject();
if (&SmallFont)
SmallFont.DeleteObject();
if (&m_Bmp)
m_Bmp.DeleteObject();
}
BEGIN_MESSAGE_MAP(CPic, CStatic)
//{{AFX_MSG_MAP(CPic)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CPic message handlersvoid CPic::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.BitBlt(0, 0, m_RectPic.Width(), m_RectPic.Height(), &m_MemDC, 0, 0, SRCCOPY);
ReleaseDC(&dc);
}//图形控件初始化,须先调用一次
void CPic::Init()
{
CDC *pDc = GetDC();//启用内存DC,所有绘图操作将在内存DC上完成
m_MemDC.CreateCompatibleDC(pDc);
GetClientRect(&m_RectPic);
m_Width = m_RectPic.Width();
m_Height = m_RectPic.Height();
m_Bmp.CreateCompatibleBitmap(pDc, m_Width, m_Height);
CBitmap *oldBmp = m_MemDC.SelectObject(&m_Bmp);
CBrush *oldBrush = m_MemDC.SelectObject(&m_Brush);
CPen *oldPen = m_MemDC.SelectObject(&m_GreenSolidPen);
CFont *oldFont = m_MemDC.SelectObject(&SmallFont);
m_MemDC.SetBkMode(TRANSPARENT);
m_MemDC.SetTextColor(GREEN);
m_MemDC.SetTextAlign(TA_RIGHT| TA_BOTTOM);
{//画。
m_MemDC.Rectangle(m_RectPic.left, m_RectPic.top, m_RectPic.right, m_RectPic.bottom);
m_MemDC.MoveTo(0, 100);
m_MemDC.LineTo(200, 200);
}
m_MemDC.SelectObject(&oldBmp);
m_MemDC.SelectObject(&oldBrush);
m_MemDC.SelectObject(&oldPen);
m_MemDC.SelectObject(&oldFont);
ReleaseDC(pDc);
}
重载OnEraseBkgnd(CDC* pDC)
{
return true;
}
同意,把这个加上。
可以看这个
http://www.albertsong.com/read-25.html
{
/************************************************************************/
/* 正式代码开始! */
/************************************************************************/
//定义变量!
CDC *MainPaint=this->GetDC(); // device context for painting
CBitmap m_Canvas;//画布!
CDC MemoryDC;//内存缓冲!
// 获得控件区域!
GetClientRect (&m_PlatformArea);
//创建内存缓冲!
MemoryDC.CreateCompatibleDC(MainPaint);
m_Canvas.CreateCompatibleBitmap(MainPaint,m_PlatformArea.Width(),m_PlatformArea.Height());
//绘制边框和底色!
MemoryDC.SelectObject(&m_Canvas);
PaintBackground(&MemoryDC);
MemoryDC.SetPixel(m_Num++,m_Num,RGB(255,0,0));m_Num开始为0!
/*4*/
MainPaint->BitBlt(0, 0, m_PlatformArea.Width(), m_PlatformArea.Height(), &MemoryDC, 0, 0, SRCCOPY);
MainPaint->DeleteDC();
m_Canvas.DeleteObject(); // Do not call CStatic::OnPaint() for painting messages
}
]为什么那个红点走得特别快呢?我的定时器才是1000毫秒啊!是不是没有定义标尺啊?
怎么定义呢?