您的位置:首页 > 其它

MFC 自定义模态等待对话框(费时操作等待)

2016-09-20 15:03 295 查看
////////////////////////////////////随手的一个简单代码

//CMainDlg.cpp 简化代码如下。

CWaitDlg *Waitdalg=NULL;

CWinThread *WaitmapThread =NULL;

UINT DoWaitMapModal(LPVOID lparam)

{

    CMainDlg* mydlg = (CMainDlg*)(lparam);

        while(1)

        {

            mydlg->MyDoMoalShow();

             WaitmapThread->SuspendThread();

        }

    return 0;

}

void  CMainDlg::MyDoMoalShow()

{

        

            Waitdalg = new CWaitDlg();

            Waitdalg->DoModal();

            delete Waitdalg;

            Waitdalg =NULL;

    

}

void CMainDlg:WaitDlgShow(CWnd* whind)

{

 if(NULL == WaitmapThread)

  {

    WaitmapThread = AfxBeginThread(DoWaitMapModal, this);

 }else

 {

     WaitmapThread->ResumeThread();

 }

  Sleep(20);

}

void CMainDlg::WaitDlgHide()  

{    

    if(Waitdalg &&WaitmapThread)

    {

    

        Waitdalg->Out =true;

    }else

    {

        for(int i= 0;i < 10;i++)//等待其他线程先执行

        {

            Sleep(50);

            if(Waitdalg &&WaitmapThread)

            {

                Waitdalg->Out =true;

                break;

            }

        }

    }    

}

//调用方式如下:

void CMainDlg:Work()

{

   WaitDlgShow();

....

工作费时代码区

.....

   WaitDlgHide();
}

/////////////////////////////////////////////////////////////////////////////////等待对话框绘制方式cpp如下

// WaitDlg.cpp : 实现文件

//

#include "stdafx.h"

#include "WaitDlg.h"

#include "afxdialogex.h"

// CWaitDlg 对话框

IMPLEMENT_DYNAMIC(CWaitDlg, CDialogEx)

CWaitDlg::CWaitDlg(CWnd* pParent /*=NULL*/)

: CDialogEx(CWaitDlg::IDD, pParent)

{

m_nowimage =1;

Out =false;

}

CWaitDlg::~CWaitDlg()

{

}

void CWaitDlg::DoDataExchange(CDataExchange* pDX)

{

CDialogEx::DoDataExchange(pDX);

}

BEGIN_MESSAGE_MAP(CWaitDlg, CDialogEx)

ON_WM_PAINT()

ON_WM_TIMER()

ON_MESSAGE(ID_RTMAPWAIT,&CWaitDlg::OnWaitOk)

ON_WM_ERASEBKGND()

END_MESSAGE_MAP()

// CWaitDlg 消息处理程序

LRESULT CWaitDlg::OnWaitOk(WPARAM wparam,LPARAM lparam)//响应健康大数据按钮点击事件

{

this->OnCancel();

return 0;

}

void CWaitDlg::OnPaint()//绘制加载图片

{

// TODO: 在此处添加消息处理程序代码

// 不为绘图消息调用 CDialogEx::OnPaint()

CPaintDC dc(this); // device context for painting

CDC memdc;

CBitmap membmp;

memdc.CreateCompatibleDC(&dc);

membmp.CreateCompatibleBitmap(&dc,m_cx,m_cy);

memdc.SelectObject(&membmp);

memdc.FillSolidRect(0,0,m_cx,m_cy,RGB(0xff,0xff,0xff));//m_cx m_cy

Graphics graphics(memdc.GetSafeHdc());

graphics.SetSmoothingMode(SmoothingModeDefault);

memdc.SetBkMode(TRANSPARENT);

Image bgimg(L"Images\\wait\\bk4.png");//背景图

graphics.DrawImage(&bgimg,0,0,m_cx,m_cy);

CString imagepath=L"";

if(m_nowimage >12)//只有12张

{

m_nowimage =1;

}

imagepath.Format(L"Images\\wait\\%d.png",m_nowimage);//形成加载动态图

Image change(imagepath);

m_changerct.left = (m_cx - change.GetWidth())/2;

m_changerct.top = (m_cy- change.GetHeight())/2;

m_changerct.bottom = m_changerct.top+ change.GetHeight();

m_changerct.right = m_changerct.left + change.GetWidth();

graphics.DrawImage(&change,m_changerct.left,m_changerct.top,change.GetWidth(),change.GetHeight());

CString data;

CRect myrect;

myrect.left =60;

myrect.right =m_changerct.right +100;

myrect.top =m_changerct.top +10;

myrect.bottom =m_changerct.bottom -10;

data= L"拼命加载中";

memdc.DrawText(data,myrect,DT_SINGLELINE|DT_LEFT|DT_VCENTER);

dc.BitBlt(0,0,m_cx,m_cy,&memdc,0,0,SRCCOPY);

memdc.DeleteDC();

membmp.DeleteObject();

}

void CWaitDlg::SetMyWind(CWnd* whind)

{

m_cind =whind;

}

BOOL CWaitDlg::OnInitDialog()

{

CDialogEx::OnInitDialog();

Image therctimg(L"Images\\wait\\bk4.png");

m_cx =therctimg.GetWidth();

m_cy = therctimg.GetHeight();

MoveWindow((1920 -m_cx)/2,(1080-m_cy)/2,m_cx,m_cy);

this->SetTimer(100,100,NULL);

/////////////////////////////////////////////////////////////

return TRUE; // return TRUE unless you set the focus to a control

// 异常: OCX 属性页应返回 FALSE

}

void CWaitDlg::SetIsOut(bool isout)

{

Out = isout;

}

void CWaitDlg::OnTimer(UINT_PTR nIDEvent)

{

// TODO: 在此添加消息处理程序代码和/或调用默认值

if(nIDEvent == 100)

{

if(Out)

{

OnCancel();

}

if(m_nowimage < 1)

{

m_nowimage =1;

}

else

{

m_nowimage++;

}

this->InvalidateRect(&m_changerct); //定时器更新绘制区域

SetTimer(100,100,NULL);

}

CDialogEx::OnTimer(nIDEvent);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: