我在对话框中画图,就是把一个运算过程的收敛曲线表示出来,有三条线。每次重画的时候都是强制刷新,结果有的时候程序运行很正常,有的时候计算到一半或者计算结束之后,窗口就自己最大化了,也就是对整个屏幕在刷新,请问是怎么回事?
补充一下,大部分时间程序不会出现这种情况

解决方案 »

  1.   

    void CVaneDlg2::OnPaint() 
    {
    CPaintDC dc(this); // device context for painting
    //曲线重画
    unsigned int i;
    CRect rectClip;if(dc.GetClipBox(&rectClip)!=NULLREGION&&mflag==1)
    {
    myvane.initreport(myvane.maxgen,myvane.bmax,myvane.bmin);
    for(i=0;i<myvane.gen;i++){
    myvane.report(myvane.maxPoint[i],myvane.maxPoint[i+1],RGB(255,0,0));
    myvane.report(myvane.minPoint[i],myvane.minPoint[i+1],RGB(0,255,0));
    myvane.report(myvane.avgPoint[i],myvane.avgPoint[i+1],RGB(0,0,255));
    }
    }
     
    //my code 
      //文本重画CRect rect;
      dc.GetClipBox(&rectClip);
      CSize size;
     CStringList m_strList;
      CString m_strLastLine;
    TEXTMETRIC tm;
    dc.GetTextMetrics(&tm);
    int nLineHeight=tm.tmHeight+tm.tmExternalLeading;
    CString str;
    int line=0;
    POSITION pos=m_strList.GetHeadPosition();
      for(;pos!=NULL;m_strList.GetNext(pos))
      {
      str=m_strList.GetAt(pos);
      size=dc.GetTextExtent(str);
      rect.SetRect(0,line*nLineHeight,size.cx,line*nLineHeight+size.cy);
      if(!(rect&rectClip).IsRectEmpty())
     dc.TextOut(0,line*nLineHeight,str);
      line++;
      }
      str=m_strLastLine;
      size=dc.GetTextExtent(str);
      rect.SetRect(0,line*nLineHeight,size.cx,line*nLineHeight+size.cy);
      if(!(rect&rectClip).IsRectEmpty())
     dc.TextOut(0,line*nLineHeight,str);}/*初始数据输出*/
    void CVane::initreport(unsigned int seed,double a,double b)
    {
    CVaneApp *app = (CVaneApp *)AfxGetApp();
    CVaneSheet* myp=(CVaneSheet*)app->pWnd;
    CVaneDlg2* myp2=(CVaneDlg2*)myp->m_page3.p;
       
    drawfig(40,20,440,380,a,b,seed);//画刻度表CWnd* p=(CWnd*)myp2->GetDlgItem(IDC_STATIC21);
    p->ShowWindow(SW_SHOW);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATIC22);
    p->ShowWindow(SW_SHOW);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATIC23);
    p->ShowWindow(SW_SHOW);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATIC24);
    p->ShowWindow(SW_SHOW);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATIC25);
    p->ShowWindow(SW_SHOW);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATIC26);
    p->ShowWindow(SW_SHOW);myp2->m_tcolor22.SetTextColor(RGB(0,0,255));
    myp2->m_tcolor21.SetTextColor(RGB(255,0,0));
    myp2->m_tcolor23.SetTextColor(RGB(0,255,0));
    myp2->m_tcolor25.SetTextColor(RGB(0,0,255));
    myp2->m_tcolor24.SetTextColor(RGB(255,0,0));
    myp2->m_tcolor26.SetTextColor(RGB(0,255,0));}/*图文输出以及数据存储*/
    void CVane::report(CPoint point1,CPoint point2,COLORREF m_colors)
    {
    int i;
    CVaneApp *app = (CVaneApp *)AfxGetApp();
    CVaneSheet* myp=(CVaneSheet*)app->pWnd;
    CVaneDlg2* myp2=(CVaneDlg2*)myp->m_page3.p;
        HWND hwnd=myp2->GetSafeHwnd();
    mypen.DeleteObject();
    mypen.CreatePen(PS_SOLID,1,m_colors);CDC dc;
     CMemDC  dc1(&dc);
    dc1.Attach(::GetDC(hwnd));
    dc1.SelectObject(&mypen); if(point2.y>(2*bmax-bmin)){
    do{bmax=2*bmax-bmin;}while((2*bmax-bmin)<=point2.y);
    CRect rect;
    rect.SetRect(40,20,440,380);
    myp2->InvalidateRect(&rect);
    myp2->UpdateWindow();
    initreport(maxgen,bmax,bmin);for(i=0;i<unsigned long(point2.x);i++){
    report(minPoint[i],minPoint[i+1],GREEN);
    report(avgPoint[i],avgPoint[i+1],BLUE);
    report(maxPoint[i],maxPoint[i+1],RED);}
    } else{if(len==1){dc1.MoveTo(40+unsigned long((float(400))/(5*fenshu)*point1.x),380-unsigned long(float(180)/(pow(10,len1-1)*fenshu1)*(point1.y-start)));
    dc1.LineTo(40+unsigned long((float(400))/(5*fenshu)*point2.x),380-unsigned long(float(180)/(pow(10,len1-1)*fenshu1)*(point2.y-start)));}
    else {dc1.MoveTo(40+unsigned long((float(400)/(((long(pow(10,len-1))/2)*fenshu)))*point1.x),380-unsigned long(float(180)/(pow(10,len1-1)*fenshu1)*(point1.y-start)));
    dc1.LineTo(40+unsigned long((float(400)/(((long(pow(10,len-1))/2)*fenshu)))*point2.x),380-unsigned long(float(180)/(pow(10,len1-1)*fenshu1)*(point2.y-start)));
    }}
    }还有我在主画图函数中每次画完都强制刷新
    代码如下:
    {
     report(point[0],point[1],RED);
    report(point1[0],point1[1],GREEN);
    report(point2[0],point2[1],BLUE);
    CRect rect;
    dc1.GetClipBox(&rect);
    myp2->InvalidateRect(&rect);
    myp2->UpdateWindow();
    }
      

  2.   

    不从一下drawfig函数
    void CVane::drawfig(int left,int top,int right ,int bottom,double max,double min,int n)
    {int i;
    CVaneApp *app = (CVaneApp *)AfxGetApp();
    CVaneSheet* myp=(CVaneSheet*)app->pWnd;
    CVaneDlg2* myp2=(CVaneDlg2*)myp->m_page3.p;
        HWND hwnd=myp2->GetSafeHwnd();
    mypen.DeleteObject();
    mypen.CreatePen(PS_SOLID,1,RGB(0,0,0));
        CDC dc;
     CMemDC  dc1(&dc);
    // CString string;
    // string="the SGA For Vane Setting";
    dc1.Attach(::GetDC(hwnd));
    dc1.SelectObject(&mypen);/* RECT rc = { 20, 20, 440, 420 };
    m_hBlack=GetSysColorBrush(COLOR_BTNFACE);
    Rectangle(dc1.m_hDC, rc.left, rc.top, rc.right, rc.bottom);// The Win32 call to FillRect requires an HBRUSH.
    // The HBRUSH operator used here makes a temporary
    // CBrush object which is cast to the required type.
    FillRect(dc.m_hDC, &rc, m_hBlack);
    */
    //画表格边框
    dc1.MoveTo(left,top);
    dc1.LineTo(left,bottom);
    dc1.MoveTo(left,bottom);
    dc1.LineTo(right,bottom); dc1.MoveTo(right,bottom);
    dc1.LineTo(right,top);
    dc1.MoveTo(right,top);
    dc1.LineTo(left,top);
    //画横坐标刻度
    CString str1;
    str1.Format("%d",n);
     len=str1.GetLength();
    if(len==1){if(n<=5)fenshu=1;
    else fenshu=2;}
    else {if(n%(long(pow(10,len-1))/2)==0)
    fenshu=n/(long(pow(10,len-1))/2);
    else fenshu=n/(long(pow(10,len-1))/2)+1;
    } for(i=0;i<5*fenshu;i++)
    {
    dc1.MoveTo(left+unsigned long((float(right-left)/fenshu)*(i+1)/5),bottom);
    dc1.LineTo(left+unsigned long((float(right-left)/fenshu)*(i+1)/5),bottom-2);}
    for(i=0;i<fenshu;i++)
    {
    dc1.MoveTo(left+unsigned long((float(right-left)/fenshu)*(i+1)),bottom);
    dc1.LineTo(left+unsigned long((float(right-left)/fenshu)*(i+1)),bottom-4);}
    for(i=0;i<fenshu/2;i++)
    {
    dc1.MoveTo(left+unsigned long(2*(float(right-left)/fenshu)*(i+1)),bottom);
    dc1.LineTo(left+unsigned long(2*(float(right-left)/fenshu)*(i+1)),bottom-7);}//设置横坐标数值
    CString str;
    //设置字体
     CFont font;
    font.CreateFont(-12,           //字体字符的逻辑高度
                 0,                //字符平均宽度取默认值
     0,                //文本行角度为0,水平
     0,                //字符角度为0,正立
     FW_NORMAL,        //正常字体
     FALSE,            //不倾斜 
     FALSE,            //不加下划线
     FALSE,            //不加删除线
     ANSI_CHARSET,       
     OUT_DEFAULT_PRECIS,
     CLIP_DEFAULT_PRECIS,
     DEFAULT_QUALITY,
     DEFAULT_PITCH|FF_MODERN,
     "Times New Roman");
    CFont *oldFont=dc1.SelectObject (&font);
    // dc1.SelectObject(oldFont);COLORREF crColor=GetSysColor(COLOR_BTNFACE); 
    dc1.SetBkColor(crColor);
    int x,y;
    x=36;y=388;
    CString str3;
    if(fenshu==1){
    dc1.TextOut(x,y,"0");
    x+=float(right-left);
    dc1.TextOut(x,y,"5");}
    else if(fenshu==2&&n<10){dc1.TextOut(x,y,"0");
    x+=float(right-left)/2;
    dc1.TextOut(x,y,"5");
    x+=float(right-left)/2;
    dc1.TextOut(x,y,"10");}
    else {for(i=0;i<=fenshu/2;i++){
    str3.Format("%d",long(pow(10,len-1))*i);
    dc1.TextOut(x,y,str3);
    x+=2*(float(right-left)/fenshu);
    }}/*str.Format("%d",n/4);
    p->SetWindowText(str);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC3);
    str.Format("%d",n/2);
    p->SetWindowText(str);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC4);
    str.Format("%d",3*n/4);
    p->SetWindowText(str);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC5);
    str.Format("%d",n);
    p->SetWindowText(str);
    */
    /*for(i=0;i<39;i++)
    {
    dc1.MoveTo(left+unsigned long(float(right-left)/40)*(i+1),bottom);
    dc1.LineTo(left+unsigned long(float(right-left)/40)*(i+1),bottom-3);}dc1.MoveTo(left+unsigned long(float(right-left)/40)*10,bottom);
    dc1.LineTo(left+unsigned long(float(right-left)/40)*10,bottom-7);
    dc1.MoveTo(left+unsigned long(float(right-left)/40)*20,bottom);
    dc1.LineTo(left+unsigned long(float(right-left)/40)*20,bottom-7);
    dc1.MoveTo(left+unsigned long(float(right-left)/40)*30,bottom);
    dc1.LineTo(left+unsigned long(float(right-left)/40)*30,bottom-7);
    //设置横坐标数值
     p=(CWnd*)myp2->GetDlgItem(IDC_STATICC1);
    p->SetWindowText("0");
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC2);
    CString str;
    str.Format("%d",n/4);
    p->SetWindowText(str);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC3);
    str.Format("%d",n/2);
    p->SetWindowText(str);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC4);
    str.Format("%d",3*n/4);
    p->SetWindowText(str);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC5);
    str.Format("%d",n);
    p->SetWindowText(str);*/
    int hight=bottom-top;
    int chazhi=max-min;
    //画纵坐标刻度
    CString str2;
    str2.Format("%d",chazhi);
        len1=str2.GetLength();
    if(len1==1){fenshu1=1;}
    else {if(chazhi%(long(pow(10,len1-1)))==0)
    fenshu1=chazhi/(long(pow(10,len1-1)));
    else fenshu1=chazhi/(long(pow(10,len1-1)))+1;
    }
    //找到起点
    start=(long(min)/long(pow(10,len1-1)))*long(pow(10,len1-1)); for(i=0;i<2*fenshu1;i++)
    {
    dc1.MoveTo(left,bottom-long(float(180)/fenshu1*(i+1)));
    dc1.LineTo(left+7,bottom-long(float(180)/fenshu1*(i+1)));}
    for(i=0;i<10*fenshu1;i++)
    {
    dc1.MoveTo(left,bottom-long(float(180)/(5*fenshu1)*(i+1)));
    dc1.LineTo(left+2,bottom-long(float(180)/(5*fenshu1)*(i+1)));}/*for(i=0;i<(39);i++)
    {
    dc1.MoveTo(left,bottom-kedu*(i+1));
    dc1.LineTo(left+3,bottom-kedu*(i+1));}dc1.MoveTo(left,bottom-kedu*10);
    dc1.LineTo(left+7,bottom-kedu*10);
    dc1.MoveTo(left,bottom-kedu*20);
    dc1.LineTo(left+7,bottom-kedu*20);
    dc1.MoveTo(left,bottom-kedu*30);
    dc1.LineTo(left+7,bottom-kedu*30);
    */
    //设置纵坐标数值
    int x1,y1;
    x1=0;y1=373;
    CString str4;
    for(i=0;i<=2*fenshu1;i++){
    str4.Format("%d",start+long(pow(10,len1-1))*2*i);
    dc1.TextOut(x1,y1,str4);
    y1-=(float((bottom-top))/fenshu1);
    }
    /*if((2*max-min)>100000.0){max=max/10;min=min/10;}
    unsigned long x1,x2,x3,x4,x5;
    x1=unsigned long(min);
    x2=unsigned long((max+min)/2);
    x3=2*x2-x1;
    x4=x2-x1+x3;
    x5=x4+x2-x1;CWnd* p=(CWnd*)myp2->GetDlgItem(IDC_STATICC6);
    str.Format("%d",x1);
    p->SetWindowText(str);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC7);
    str.Format("%d",x2);
    p->SetWindowText(str);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC8);
    str.Format("%d",x3);
    p->SetWindowText(str);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC9);
    str.Format("%d",x4);
    p->SetWindowText(str);
    p=(CWnd*)myp2->GetDlgItem(IDC_STATICC10);
    str.Format("%d",x5);
    p->SetWindowText(str);*/}