void CProgressDlg::UpdatePercentText()                    //上面是进度条的设置
{
  int nPos=m_Progress.GetPos();
  int nLow,nUp;
m_Progress.GetRange(nLow,nUp);
m_strPercent.Format("%4.0f%",(float)nPos/(float)(nUp-nLow)*100.0);
UpdateData(FALSE);
}BOOL CProgressDlg::OnInitDialog()  
{
CDialog::OnInitDialog();  m_Progress.SetRange(0,100);
m_Progress.SetStep(5);
m_Progress.SetPos(30);
UpdatePercentText();
// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

解决方案 »

  1.   

    估计你的消息循环被阻塞了
    可以启动一个定时器,在定时器里StepIt,时间长的操作用后台线程来完成
      

  2.   

    进度条自己不会刷新,你是如何定时调用UpdatePercentText函数的?
    加定时器了吗?
      

  3.   

    定义 UpdatePercentText函数
     
    int nPos=m_Progress.GetPos();
      int nLow,nUp;
    m_Progress.GetRange(nLow,nUp);
    m_strPercent.Format("%4.0f%",(float)nPos/(float)(nUp-nLow)*100.0);
    UpdateData(FALSE);我是一点也不懂啊,编译没有错误,就是不显示
      

  4.   

    UpdatePercentText函数来设置进度条的位置,你只在OnInitDialog函数中调用一次,进度条的位置自然不会动了,给CProgressDlg加一个WM_TIMER消息响应函数OnTimer(UINT nIDEvent),再在OnTimer函数中调用UpdatePercentText。 另外在OnInitDialog函数中调用SetTimer(1,1000,0);启动定时器,这样才能定时刷新进度条
      

  5.   

    谢谢你的指教,我添加完了
    void CProgressDlg::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    if(m_Progress.GetPos()==100)
    KillTimer(1);
    m_Progress.StepIt();
    CDialog::OnTimer(nIDEvent);
    }
    //给CProgressDlg加一个WM_TIMER消息响应函数OnTimer(UINT nIDEvent),再在OnTimer函数中调用UpdatePercentText
    BOOL CProgressDlg::OnInitDialog() 
    {
    CDialog::OnInitDialog();    m_Progress.SetRange(0,100);
        m_Progress.SetStep(5);
        m_Progress.SetPos(30);
        m_Progress.SetTimer(1,1000,0);
        UpdatePercentText();


    return TRUE;  
    }
    在OnInitDialog函数中调用SetTimer(1,1000,0)编译没有出错误,就好似进度条那个对话框根本没有出来,
    我的目的就是在我的一个文件转换程序里加上进度条来显示进度,现在是文件能转换,就是没有弹出进度条,刚接触VC不到一个月,请多多指教。
      

  6.   

    把UpdatePercentText()函数放入OnTimer(UINT nIDEvent)中去调用,这样才能定时刷新界面,
    另外可打断点看看程序进入OnTimer函数中没有
      

  7.   

    这句 m_Progress.SetTimer(1,1000,0);
    改为 m_Progress.SetTimer(1,1000,0);
      

  8.   

    应改为
     SetTimer(1,1000,0);
    就可以看到效果了
      

  9.   

    整个程序如下,没有加进度条之前,程序还能运行,加了之后就运行不了,请大家帮忙看看,谢谢了 
    #include "stdafx.h"
    #include "Test2.h"
    #include "TestDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    struct Mydata{char time[16];char value[16];};
     
    /////////////////////////////////////////////////////////////////////////////
    // CTestDlg dialog
    CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CTestDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CTestDlg)
    m_strPercent = _T("");
    //}}AFX_DATA_INIT
    }
    void CTestDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CTestDlg)
    DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
    DDX_Text(pDX, IDC_STATIC_TEXT, m_strPercent);
    //}}AFX_DATA_MAP
    }
    BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
    //{{AFX_MSG_MAP(CTestDlg)
    ON_WM_TIMER()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CTestDlg message handlersvoid CTestDlg::OnOK() 
    {
    // TODO: Add extra validation here
    CFile m_File; 
    CFileDialog dlg(TRUE,0,0,0,"XML Files(*.xml)|*.xml|All Files(*.*)|*.*||");
    if (dlg.DoModal()!=IDOK)
    return;
    if ( !m_File.Open( dlg.GetPathName(), CFile::modeRead) ){
    MessageBox("无法打开文件");
    return;
    CDialog::OnOK();
    }
    int i,j;
    int length,num,location_temp1,location_temp2,location_temp3,location_temp4;
    int location_Power_start,location_Power_end;
    int location_WindSpeed_start,location_WindSpeed_end;
    int location_Rpm_start,location_Rpm_end;
    int location_Torque_start,location_Torque_end;

    length=m_File.GetLength();
    char* c=new char[length+1]; m_File.Read(c,length);
    c[length]=0;

    CString str(c);

    m_File.Close();
    delete c;    location_Power_start     = str.Find("AI_GridRealPower" , 0);
    location_WindSpeed_start = str.Find("AI_WindSpeed" , 0);
    location_Rpm_start       = str.Find("V_GeneratorRpm1Sec" , 0);
    location_Torque_start    = str.Find("V_TorqueSPNm" , 0);

    location_Power_start  = str.Find("<valueitem" , location_Power_start);
    location_Power_end      = str.Find("</channel>" , location_Power_start);

    location_WindSpeed_start = str.Find("<valueitem" , location_WindSpeed_start);
    location_WindSpeed_end  = str.Find("</channel>" , location_WindSpeed_start); location_Rpm_start      = str.Find("<valueitem" , location_Rpm_start);
    location_Rpm_end      = str.Find("</channel>" , location_Rpm_start); location_Torque_start  = str.Find("<valueitem" , location_Torque_start);
    location_Torque_end      = str.Find("</channel>" , location_Torque_start);



    CString str_Power(str.Mid(location_Power_start , location_Power_end - location_Power_start));
    CString str_WindSpeed(str.Mid(location_WindSpeed_start , location_WindSpeed_end - location_WindSpeed_start));
    CString str_Rpm(str.Mid(location_Rpm_start , location_Rpm_end - location_Rpm_start));
    CString str_Torque(str.Mid(location_Torque_start , location_Torque_end - location_Torque_start));




    for(num=0,location_temp1=0;   ;num++,location_temp1++){
    location_temp1=str_Power.Find('<',location_temp1);
    if(location_temp1==-1)
    break;
    }



    struct Mydata *Power,*Power1,*WindSpeed,*WindSpeed1,*Rpm,*Rpm1,*Torque,*Torque1;

    Power1 = Power = new Mydata[num];
    WindSpeed1 = WindSpeed = new Mydata[num];
    Rpm1 = Rpm = new Mydata[num];
    Torque1 = Torque = new Mydata[num];


    location_temp1 = 0;
    location_temp2 = 0;
    location_temp3 = 0;
    location_temp4 = 0; for(i=0;i<num;i++,Power1++){
    location_temp1 = str_Power.Find('<' , location_temp4);
    location_temp4 = str_Power.Find('>' , location_temp1);
    location_temp2 = str_Power.Find('"' , location_temp1) + 1;
    location_temp3 = str_Power.Find('"' , location_temp2 + 1) - 1;

    if((location_temp2<location_temp4)&(location_temp3<location_temp4)){
    strcpy(Power1->time , str_Power.Mid(location_temp2 , 16));
        for(j=location_temp3-location_temp2+1;j<16;j++)
    Power1->time[j]=' ';
    }

    location_temp2 = str_Power.Find('"' , location_temp3+2) + 1;
    location_temp3 = str_Power.Find('"' , location_temp2+1) - 1;

    if((location_temp2<location_temp4)&(location_temp3<location_temp4)){
    strcpy(Power1->value , str_Power.Mid(location_temp2 , 16));
    for(j=location_temp3-location_temp2+1;j<16;j++)
    Power1->value[j]=' ';

    }
    }



    location_temp1 = 0;
    location_temp2 = 0;
    location_temp3 = 0;
    location_temp4 = 0; for(i=0;i<num;i++,WindSpeed1++){
    location_temp1 = str_WindSpeed.Find('<' , location_temp4);
    location_temp4 = str_WindSpeed.Find('>' , location_temp1);
    location_temp2 = str_WindSpeed.Find('"' , location_temp1) + 1;
    location_temp3 = str_WindSpeed.Find('"' , location_temp2 + 1) - 1;

    if((location_temp2<location_temp4)&(location_temp3<location_temp4)){
    strcpy(WindSpeed1->time , str_WindSpeed.Mid(location_temp2 , 16));
        for(j=location_temp3-location_temp2+1;j<16;j++)
    WindSpeed1->time[j]=' ';
    }

    location_temp2 = str_WindSpeed.Find('"' , location_temp3+2) + 1;
    location_temp3 = str_WindSpeed.Find('"' , location_temp2+1) - 1;

    if((location_temp2<location_temp4)&(location_temp3<location_temp4)){
    strcpy(WindSpeed1->value , str_WindSpeed.Mid(location_temp2 , 16));
    for(j=location_temp3-location_temp2+1;j<16;j++)
    WindSpeed1->value[j]=' ';

    }
    }



    location_temp1 = 0;
    location_temp2 = 0;
    location_temp3 = 0;
    location_temp4 = 0; for(i=0;i<num;i++,Rpm1++){
    location_temp1 = str_Rpm.Find('<' , location_temp4);
    location_temp4 = str_Rpm.Find('>' , location_temp1);
    location_temp2 = str_Rpm.Find('"' , location_temp1) + 1;
    location_temp3 = str_Rpm.Find('"' , location_temp2 + 1) - 1;

    if((location_temp2<location_temp4)&(location_temp3<location_temp4)){
    strcpy(Rpm1->time , str_Rpm.Mid(location_temp2 , 16));
        for(j=location_temp3-location_temp2+1;j<16;j++)
    Rpm1->time[j]=' ';
    }

    location_temp2 = str_Rpm.Find('"' , location_temp3+2) + 1;
    location_temp3 = str_Rpm.Find('"' , location_temp2+1) - 1;

    if((location_temp2<location_temp4)&(location_temp3<location_temp4)){
    strcpy(Rpm1->value , str_Rpm.Mid(location_temp2 , 16));
    for(j=location_temp3-location_temp2+1;j<16;j++)
    Rpm1->value[j]=' ';

    }
    }


    location_temp1 = 0;
    location_temp2 = 0;
    location_temp3 = 0;
    location_temp4 = 0; for(i=0;i<num;i++,Torque1++){
    location_temp1 = str_Torque.Find('<' , location_temp4);
    location_temp4 = str_Torque.Find('>' , location_temp1);
    location_temp2 = str_Torque.Find('"' , location_temp1) + 1;
    location_temp3 = str_Torque.Find('"' , location_temp2 + 1) - 1;

    if((location_temp2<location_temp4)&(location_temp3<location_temp4)){
    strcpy(Torque1->time , str_Torque.Mid(location_temp2 , 16));
        for(j=location_temp3-location_temp2+1;j<16;j++)
    Torque1->time[j]=' ';
    }

    location_temp2 = str_Torque.Find('"' , location_temp3+2) + 1;
    location_temp3 = str_Torque.Find('"' , location_temp2+1) - 1;

    if((location_temp2<location_temp4)&(location_temp3<location_temp4)){
    strcpy(Torque1->value , str_Torque.Mid(location_temp2 , 16));
    for(j=location_temp3-location_temp2+1;j<16;j++)
    Torque1->value[j]=' ';

    }
    }
       CString str1;
    str1.Format("转换完成,提取了%d个数据",num);
    MessageBox(str1); CFileDialog dlg1(FALSE,"csv",0,OFN_OVERWRITEPROMPT,"CSV Files(*.csv)|*.csv|All Files(*.*)|*.*||");
    if (dlg1.DoModal()!=IDOK){
    //delete Power,WindSpeed,Rpm,Torque;
    return;
    }
    if ( !m_File.Open( dlg1.GetPathName(), CFile::modeCreate | CFile::modeWrite) ){
    MessageBox("无法保存文件");
    //delete Power,WindSpeed,Rpm,Torque;
    return;
    }
    i=0;
    Power1     = Power;
    WindSpeed1 = WindSpeed;
    Rpm1       = Rpm;
    Torque1    = Torque;

        #define TITLE "Time,Grid Real Power(KW),Wind speed(m/s),Gen. rpm(RPM),Torque(Nm)\r\n"

    m_File.Write(TITLE,strlen(TITLE));

    while(i<num){
    m_File.Write(Power1->time,16);
    m_File.Write(",",1);
    m_File.Write(Power1->value,16);
    m_File.Write(",",1);
    m_File.Write(WindSpeed1->value,16);
    m_File.Write(",",1);
    m_File.Write(Rpm1->value,16);
    m_File.Write(",",1);
    m_File.Write(Torque1->value,16);
    m_File.Write("\r\n",2);

    i++;
    Power1++;
    WindSpeed1++;
    Rpm1++;
    Torque1++;
    }

    m_File.Close();
    }
    void CTestDlg::UpdatePercentText()
    {
             int nPos=m_Progress.GetPos();
             int nLow,nUp;
     m_Progress.GetRange(nLow,nUp);
     m_strPercent.Format("%4.0f%",(float)nPos/(float)(nUp-nLow)*100.0);
     UpdateData(FALSE);
    }CScrollBar* CTestDlg::GetScrollBarCtrl(int nBar) const
    {
    // TODO: Add your specialized code here and/or call the base class

    return CDialog::GetScrollBarCtrl(nBar);
    }BOOL CTestDlg::OnInitDialog() 
    {
    CDialog::OnInitDialog();
        m_Progress.SetRange(0,100);
    m_Progress.SetStep(5);
    m_Progress.SetPos(30);
        SetTimer(1,1000,0);
    UpdatePercentText();

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
    }void CTestDlg::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default

    if(m_Progress.GetPos()==100)
    KillTimer(1);
            UpdatePercentText();
    CDialog::OnTimer(nIDEvent);
    }