弹出对话框的代码 :

主要是弹出对话框的时候可以在后台做某些动作:

与直接弹出对话框前开启线程,在线程中做某些动作效果相同。

1
2
3
4
5
MsgBoxThread mbt;
mbt.MesgBoxPopup(10, "do something");
// do something
// do something
mbt.WaitFor(); // 等待线程退出

使用到的MsgThread和对话框类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#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);
}