弹出对话框的代码 :
主要是弹出对话框的时候可以在后台做某些动作:
与直接弹出对话框前开启线程,在线程中做某些动作效果相同。
MsgBoxThread mbt;
mbt.MesgBoxPopup(10, "请按手指!");
// do something
// do something
mbt.WaitFor(); // 等待线程退出
使用到的MsgThread和对话框类
#include "StdAfx.h"
#include "MsgBoxThread.h"
#include "YF_GCM.h"
#include "DlgMesgBox.h"
unsigned int __stdcall ThreadBox(LPVOID);
MsgBoxThread::MsgBoxThread(void)
{
}
MsgBoxThread::~MsgBoxThread(void)
{
}
int MsgBoxThread::MesgBoxPopup(int time, const CString& szText)
{
m_Mesg.text = szText;
m_Mesg.time = time;
m_handle[0] = (HANDLE)_beginthreadex(NULL, 0, ThreadBox, (LPVOID)&m_Mesg, 0, NULL);
return 0;
}
unsigned int __stdcall ThreadBox(LPVOID lp)
{
_mesg *pTmpMesg = (_mesg *)lp;
CDlgMesgBox box;
box.SetDlgLifetime(pTmpMesg->time);
box.SetBoxText(pTmpMesg->text);
box.DoModal();
//
return 0;
}
int MsgBoxThread::WaitFor(void)
{
WaitForMultipleObjects(1, m_handle, TRUE, INFINITE);
return 0;
}
#pragma once
struct _mesg{
CString text;
int time;
};
class MsgBoxThread
{
public:
MsgBoxThread(void);
virtual ~MsgBoxThread(void);
int MesgBoxPopup(int time, const CString& szText);
private:
_mesg m_Mesg;
HANDLE m_handle[1];
public:
int WaitFor(void);
};
#pragma once
// CDlgMesgBox 对话框
class CDlgMesgBox : public CDialog
{
DECLARE_DYNAMIC(CDlgMesgBox)
public:
CDlgMesgBox(CWnd* pParent = NULL); // 标准构造函数
virtual ~CDlgMesgBox();
// 对话框数据
enum { IDD = IDD_DIALOG_MESGBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
int SetDlgLifetime(int secTime);
private:
enum{BoxLifetime = 1, BoxUpdate = 2};
int m_Lifetime;
public:
virtual BOOL OnInitDialog();
afx_msg void OnTimer(UINT_PTR nIDEvent);
void UpdateBox(void);
void SetBoxText(const CString& szText);
private:
CString m_szMsgText;
public:
afx_msg void OnBnClickedBoxButtonExit();
};
// DlgMesgBox.cpp : 实现文件
//
#include "stdafx.h"
#include "YF_GCM.h"
#include "DlgMesgBox.h"
// CDlgMesgBox 对话框
IMPLEMENT_DYNAMIC(CDlgMesgBox, CDialog)
CDlgMesgBox::CDlgMesgBox(CWnd* pParent /*=NULL*/)
: CDialog(CDlgMesgBox::IDD, pParent)
, m_Lifetime(10000)
, m_szMsgText(_T(""))
{
}
CDlgMesgBox::~CDlgMesgBox()
{
}
void CDlgMesgBox::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CDlgMesgBox, CDialog)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BOX_BUTTON_EXIT, &CDlgMesgBox::OnBnClickedBoxButtonExit)
END_MESSAGE_MAP()
// CDlgMesgBox 消息处理程序
int CDlgMesgBox::SetDlgLifetime(int secTime)
{
m_Lifetime = secTime;
return 0;
}
BOOL CDlgMesgBox::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetTimer(BoxUpdate, 1000, NULL);
SetDlgItemText(IDC_BOX_STATIC_MSG, m_szMsgText);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CDlgMesgBox::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
switch (nIDEvent)
{
case BoxUpdate:
UpdateBox();
if (0 == m_Lifetime)
{
SendMessage(WM_CLOSE);
}
else
{
--m_Lifetime;
}
break;
default:break;
}
CDialog::OnTimer(nIDEvent);
}
void CDlgMesgBox::UpdateBox(void)
{
CString szText;
szText.Format(_T("关闭(%d)"), m_Lifetime);
SetDlgItemText(IDC_BOX_BUTTON_EXIT, szText); // 在按钮上显示关闭倒计时
}
void CDlgMesgBox::SetBoxText(const CString& szText)
{
m_szMsgText = szText;
}
void CDlgMesgBox::OnBnClickedBoxButtonExit()
{
// TODO: 在此添加控件通知处理程序代码
SendMessage(WM_CLOSE);
}