急啊,MFC这个双缓存用的好像不对,烦请指点一二。。因为做的时钟老是不停的闪,在网上看到那段双缓存的代码,拿来自己试试,我是初级MFC学者,因为是时钟,绘图在o
这个背景绘制的有点笨笨的,不要介意哦,运行出错:user breakpoint called from code at 0x751c194b,下面是我的双缓存的一部分代码: void CMy1View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
CPen pen1(PS_SOLID,3,RGB(0,0,0));
CPen pen2(PS_SOLID,2,RGB(0,0,255));
CPen pen3(PS_SOLID,1,RGB(255,0,255)); //三支彩笔显示三个时针
CBrush myBrush[7];
for(int i=1;i<=6;i++)
{
switch(i)
{
case 1:
myBrush[i].CreateSolidBrush(m_clBack);
break;
case 2:
myBrush[i].CreateSolidBrush(RGB(255,255,0));
break;
case 3:
myBrush[i].CreateSolidBrush(RGB(0,255,0));
break;
case 4:
myBrush[i].CreateSolidBrush(liquan);
break;
case 5:
myBrush[i].CreateSolidBrush(RGB(255,0,255));
break;
case 6:
myBrush[i].CreateSolidBrush(RGB(0,0,0));
break;
}
};
CRgn rgn[201];
int n=1;
for(i=100;i<300;)
{
rgn[n].CreateEllipticRgn(i, i, 600-i, 600-i);
n++;
i=i+1;
};
CDC MemDC;
CDC *pDC=new CDC;
CBitmap MemBitmap;
CMy1Doc*pDoc=GetDocument(); //网上说这样初始化pDC,这儿不太明白
ASSERT_VALID(pDoc);
MemDC.CreateCompatibleDC(NULL);
MemBitmap.CreateCompatibleBitmap(pDC,400,400);
CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
MemDC.FillSolidRect(100,100,500,500,RGB(255,0,255));
switch(nIDEvent)
{
case 1: //显示指针
MemDC.MoveTo(300,300);
int a,b;
MemDC.SelectObject(pen1);
a=(int)(300+100*cos(du1));
b=(int)(300+100*sin(du1));
MemDC.LineTo(a,b); //显示时针,这是我用数学中的圆的参数方程算出来的,du1是全局变量,初始值为-3.141592653/2,下面分别是分针,秒针,du2,du3和du1一样
MemDC.MoveTo(300,300);
int c,d;
MemDC.SelectObject(pen2);
c=(int)(300+150*cos(du2));
d=(int)(300+150*sin(du2));
MemDC.LineTo(c,d);
MemDC.MoveTo(300,300);
int x,y;
MemDC.SelectObject(pen3);
x=(int)(300+200*cos(du3));
y=(int)(300+200*sin(du3));
MemDC.LineTo(x,y);
if(du3<du4+2*3.141592653) //这部分是显示时钟表盘周围的点,秒针走一下打出一个
{
int e,f;
e=(int)(300+205*cos(du3));
f=(int)(300+205*sin(du3));
double a1=(du3+3.141592653/2)*(30/3.141592653);
CString str1;
str1.Format("%lf\n", a1);
TRACE(str1);
a1=a1+0.000005;
q1=(int)a1;
CString str;
str.Format("%ld\n", q1);
TRACE(str);
if(q1%5!=0)
{
MemDC.TextOut(e,f-10,".");
}
};
du1=du1+3.141592653/21600;
du2=du2+3.141592653/1800;
du3=du3+3.141592653/30;
MemDC.TextOut(285,290,"呵呵");
pDC->BitBlt(100,100,400,400,&MemDC,0,0,SRCCOPY);
break;
case 2: // 刷新表盘
for(int i=1;i<=200;i++)
{
if(i<=20)
MemDC.FillRgn(&rgn[i],&myBrush[1]);
if((i>20)&&(i<=40))
{
if((i%2)==0)
MemDC.FillRgn(&rgn[i],&myBrush[1]);
else
MemDC.FillRgn(&rgn[i],&myBrush[2]);
}
if((i>=41)&&(i<=60))
MemDC.FillRgn(&rgn[i],&myBrush[2]);
if((i>60)&&(i<=80))
{
if((i%2)==0)
MemDC.FillRgn(&rgn[i],&myBrush[2]);
else
MemDC.FillRgn(&rgn[i],&myBrush[3]);
}
if((i>80)&&(i<=100))
MemDC.FillRgn(&rgn[i],&myBrush[3]);
if((i>100)&&(i<=120))
{
if((i%2)==0)
MemDC.FillRgn(&rgn[i],&myBrush[3]);
else
MemDC.FillRgn(&rgn[i],&myBrush[4]);
}
if((i>120)&&(i<=140))
MemDC.FillRgn(&rgn[i],&myBrush[4]);
if((i>140)&&(i<=160))
{
if((i%2)==0)
MemDC.FillRgn(&rgn[i],&myBrush[4]);
else
MemDC.FillRgn(&rgn[i],&myBrush[5]);
}
if((i>160)&&(i<=180))
MemDC.FillRgn(&rgn[i],&myBrush[5]);
if((i>180)&&(i<=200))
{
if((i%2)==0)
MemDC.FillRgn(&rgn[i],&myBrush[5]);
else
MemDC.FillRgn(&rgn[i],&myBrush[6]);
}
};
pDC->BitBlt(100,100,400,400,&MemDC,0,0,SRCCOPY);
break;
}
CView::OnTimer(nIDEvent);
}
[ 本帖最后由 b584310818 于 2010-8-28 12:28 编辑 ]