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);
}
//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);
}
相关文章推荐
- MFC模态对话框和非模态对话框及副窗口调用主窗口控件 自定义消息发送
- VC++2005 MFC 编程-对话框的菜单操作
- MFC如何在自定义对话框中添加系统图标?
- MFC 非模态,无内存泄漏对话框应该是怎么创建
- MFC模态对话框和非模态对话框
- (delphi)程序运行过程中等待对话框的自定义组件源码(包含两个提示,进度条,AVI)
- 手把手教你建立一个模态对话框(MFC)
- 手把手教你建立一个模态对话框(MFC)
- MFC模态对话与非模态对话框
- MFC中CWnd类及其派生类对话框、消息处理、窗口操作
- MFC应用程序中处理消息,创建窗口的过程,关闭窗口(非模态窗口),打开模式对话框等的函数调用顺序 .
- MFC中回车,空格,ESC,F1键盘操作对对话框的影响处理
- 在iPhone中创建模态(modal)等待对话框
- 在MFC中怎样添加一个非模态的对话框
- MFC 非模态,无内存泄漏对话框应该是怎么创建
- 建立一个模态对话框(MFC)
- 用MFC ODBC操作Access数据库 (怎么添加数据源,单文档和对话框实例)
- MFC 模态对话框dlg.DoModal()返回值
- MFC学习笔记之五—————对话框编程及控件初步操作
- MFC中CWnd类及其派生类对话框、消息处理、窗口操作