MFC点击按钮,弹出进度条并且自动滚动以及进度条对话框背景色、按钮颜色设置

来源:互联网 发布:mac谷歌浏览器安装 编辑:程序博客网 时间:2024/04/20 07:08

首先,当某个函数的处理需要一定时间时,而我们又不需要用户进行其他操作时,我们就需要一个进度条来提醒用户,此时的进度是通过sleep函数控制,并非和实际函数处理进度相关联。

本人在这里经过对网上资料的收集,在这里做个比较全面的总结,本人使用的是VS2012。

1.在资源管理视图添加一个对话框,并且添加一个进度条控件。

 

2.给对话框添加类。右键点击对话框,选择添加类,自己在类名处填写一个类名,.h和.cpp文件会自动生成

3.添加进度条控件,右键点击进度条,选择添加变量

4.右键点击对话框,选择类向导,在消息内添加,WM_Timer、WM_CtlColor和WM_DrawItem,VS会自动生成三个函数。其中WM_Timer用来设置定时器函数,WM_CtlColor用来设置对话框内各个控件的颜色(BUTTON按钮控件除外),WM_DrawItem是用来设置按钮颜色的。

 

 

5.在类向导内自己添加OnInitDialog(),并在函数内设置一定时器,以及进度条的基本参数

DialogEx::OnInitDialog();
 GetDlgItem(IDOK)->EnableWindow(FALSE);
 m_progress = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS1);
 m_progress ->SetRange(0,100);
 m_progress-> SetPos(0);
 SetTimer(1,0,NULL);
 //m_progress-> SendMessage(PBM_SETBKCOLOR, 0, RGB(0, 0, 0));//背景色

 //m_progress-> SendMessage(PBM_SETBARCOLOR, 0, RGB(0, 255, 0));//前景色


 return TRUE; 

6.在OnTimer(UINT_PTR nIDEvent)函数内进行具体设置,这里自己需要添加一静态文本框,用来显示进度,ID我这里设为了IDC_PROPERCENT

switch (Timer)
 {
 case 1:
  {
   int pos = 0;
   CString  str;
   for (int i=0; i<10; i++)
   {
    pos = pos + 10;
    Sleep(500);
    str.Format(_T("%d%%"),pos);
    SetDlgItemText(IDC_PROPERCENT,str);
    m_PatentProCtrl.SetPos(pos);
   }

   /* if (pos>100)
   {
   pos = 0;
   }*/
   if (pos == 100)
   {
    KillTimer(1);
    OnOK();
    AfxMessageBox(_T("文件替换完成!"));
   }
   //m_progress ->SetPos(pos);

  }
 default:
  break;
 }

 CDialogEx::OnTimer(nIDEvent);

7.在OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)进行控件颜色设置

HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 if(nCtlColor==CTLCOLOR_BTN)          //更改按钮颜色
 {
  //pDC->SetBkMode(TRANSPARENT);
  pDC->SetTextColor(RGB(0,0,0));
  pDC->SetBkColor(RGB(146,211,255));   
  HBRUSH b=CreateSolidBrush(RGB(146,211,255));
  return b;
 }
 else if(nCtlColor==CTLCOLOR_SCROLLBAR)  //
 {
  //pDC->SetBkMode(TRANSPARENT);
  pDC->SetTextColor(RGB(0,0,0));
  pDC->SetBkColor(RGB(233,233,220));
  HBRUSH b=CreateSolidBrush(RGB(233,233,220));
  return b;
 }
 else if(nCtlColor==CTLCOLOR_EDIT)   //更改编辑框
 {
  //pDC->SetBkMode(TRANSPARENT);
  pDC->SetTextColor(RGB(0,0,0));
  pDC->SetBkColor(RGB(165,254,236));
  HBRUSH b=CreateSolidBrush(RGB(165,254,236));
  return b;
 }
 else if(nCtlColor==CTLCOLOR_STATIC)  //更改静态文本
 {
  pDC->SetTextColor(RGB(0,0,0));
  pDC->SetBkColor(RGB(207,221,238));
  HBRUSH b=CreateSolidBrush(RGB(207,221,238));
  return b;
 }
 else if(nCtlColor==CTLCOLOR_DLG)   //更改对话框背景色
 {
  pDC->SetTextColor(RGB(0,0,0));
  pDC->SetBkColor(RGB(207, 221, 238));
  HBRUSH b=CreateSolidBrush(RGB(207, 221, 238));
  return b;
 }

 return hbr;

8.在OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)函数内根据按钮ID进行单个按钮设置

if(nIDCtl==IDOK)         //checking for the button
 {
  CDC dc;
  RECT rect;
  dc.Attach(lpDrawItemStruct ->hDC);   // Get the Button DC to CDC
  rect = lpDrawItemStruct->rcItem;     //Store the Button rect to our local rect.
  dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));
  dc.FillSolidRect(&rect,RGB(146,211,255));//Here you can define the required color to appear on the Button.
  
  UINT state=lpDrawItemStruct->itemState; //This defines the state of the Push button either pressed or not.

  if((state & ODS_SELECTED))
  {
   dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);
  
  }
  else
  {
   dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
  }

  //********************修改背景和前景色********************
  dc.SetBkColor(RGB(146,211,255));   //Setting the Text Background color
  dc.SetTextColor(RGB(0,0,0));     //Setting the Text Color

  TCHAR buffer[MAX_PATH];           //To store the Caption of the button.
  ZeroMemory(buffer,MAX_PATH );     //Intializing the buffer to zero
  ::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH); //Get the Caption of Button Window
  dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);//Redraw the Caption of Button Window
  dc.Detach(); // Detach the Button DC
 }
 else if(nIDCtl==IDCANCEL)         //checking for the button
 {
  CDC dc;
  RECT rect;
  dc.Attach(lpDrawItemStruct ->hDC);   // Get the Button DC to CDC
  rect = lpDrawItemStruct->rcItem;     //Store the Button rect to our local rect.
  dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));
  dc.FillSolidRect(&rect,RGB(146,211,255));//Here you can define the required color to appear on the Button.

  UINT state=lpDrawItemStruct->itemState; //This defines the state of the Push button either pressed or not.

  if((state & ODS_SELECTED))
  {
   dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);
  }
  else
  {
   dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
  }

  //********************修改背景和前景色********************
  dc.SetBkColor(RGB(146,211,255));   //Setting the Text Background color
  dc.SetTextColor(RGB(0,0,0));     //Setting the Text Color

  TCHAR buffer[MAX_PATH];           //To store the Caption of the button.
  ZeroMemory(buffer,MAX_PATH );     //Intializing the buffer to zero
  ::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH); //Get the Caption of Button Window
  dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);//Redraw the Caption of Button Window
  dc.Detach(); // Detach the Button DC
 }

 

0 0
原创粉丝点击