我成功画了Van Koch Snowflake,但是在我画完后它没有立刻显示出来。我必须最大化,还原或移动窗口以显示它。 input是带有“F”,“+”,“ - ”,“[”,“]”的字符串。
//VanKoch Koch Snowflake
CString VanKochSnowflake("F++F++F");
CString VanKochSnowflakeRule("F-F++F-F");
//alpha Van Kochk
double alphaVanKockSnowflake =0;
double alphaVanKockSnowflake1 =PI/3;
Line dt;
void DrawLine(DiemThuc &p,double alpha, double d)
{
DiemThuc pNew;
pNew.x=p.x+cos(alpha)*d;
pNew.y=p.y+sin(alpha)*d;
dt.Draw(p.x,p.y,pNew.x,pNew.y);
p.x=pNew.x;
p.y=pNew.y;
}
void DrawFractalString(CString str)
{
stack<double> Posx;
stack<double> Posy;
stack<double> Stacka;
for(int i=0;i<str.GetLength();i++)
{
switch (str[i])
{
case 'F':case'L':case'R':
DrawLine(p,alpha,d);break;
case '+':
alpha+=alpha1;break;
case '-':
alpha-=alpha1;break;
case '[':
{
Posx.push(p.x);
Posy.push(p.y);
Stacka.push(alpha);
}break;
case ']':
{
p.x=Posx.top();Posx.pop();
p.y=Posy.top();Posy.pop();
alpha=Stacka.top();Stacka.pop();
}break;
default:
break;
}
}
}
void Cbaitap1View::ThayTheChuoi()
{
for(int i=1;i<=n;i++)
{
CString new_str("");
for(int j=0;j<str.GetLength();j++)
{
sub = str[j];
if(sub =='F' && state!=4 && state!=5)
new_str += str_Rule;
else
if(sub=='L')
new_str+=str_Rule1;
else
if(sub=='R')
new_str+=str_Rule2;
else
new_str += sub;
}
str=new_str;
}
}
void Cbaitap1View::OnDraw(CDC* /*pDC*/)
{
Cbaitap1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
wglMakeCurrent(m_hDC,m_hRC);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,1,1,0);
glColor3f(0,1,0);
glPointSize(1);
switch (state)
{
case 1:
{
n=D.SoN;
d=5;
str = VanKochSnowflake;
str_Rule = VanKochSnowflakeRule;
alpha = alphaVanKockSnowflake;
alpha1 = alphaVanKockSnowflake1;
ThayTheChuoi();
if(p.x!=0 && p.y!=0)
DrawFractalString(str);
}break;
default:
break;
}
glEnd();
glFlush();
SwapBuffers(::GetDC(GetSafeHwnd()));
wglMakeCurrent(NULL,NULL);
}
void Cbaitap1View::OnVedt()
{
D.DoModal();
state=1;
// TODO: Add your command handler code here
}
我觉得OnDraw错了,但我无法解决它@@
答案 0 :(得分:1)
您可能想在绘图后重新绘制窗口。
当你最小化和最大化时会发生这种情况。
你可以在这里阅读abotu mfc重绘: http://msdn.microsoft.com/en-us/library/0fdz8ey6(v=vs.90).aspx
如果您想尝试重新绘制正在更改的控件,可以在控件上调用invalidate()并重绘。