程序功能点击发送消息按钮进度条逐步显示,每0.1秒增加1%,10秒后达到100%。使用定时器实现每100毫秒(0.1秒)更新一次进度,投递一个消息显示进度%数。
1、rc文件相关代码
cpp
IDD_MYWPARAM_DIALOG DIALOGEX 0, 0, 300, 200
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "wParam示例"
FONT 9, "宋体"
BEGIN
DEFPUSHBUTTON "发送消息",IDC_BUTTON_SEND,100,160,100,20
CONTROL "",IDC_PROGRESS1,"msctls_progress32",WS_BORDER,20,50,260,20
LTEXT "状态信息将显示在这里",IDC_STATIC_INFO,20,100,260,40
END

2、MyWParamDlg.h中代码
cpp
#if !defined(AFX_MYWPARAMDLG_H__)
#define AFX_MYWPARAMDLG_H__
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMyWParamDlg : public CDialog
{
public:
CMyWParamDlg(CWnd* pParent = NULL);
enum { IDD = IDD_MYWPARAM_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
virtual BOOL OnInitDialog();
protected:
DECLARE_MESSAGE_MAP()
public:
// 控件变量
CProgressCtrl m_Progress;
CButton m_btnSend;
CStatic m_staticInfo;
int m_nCurrentProgress;
// 消息处理函数
afx_msg void OnBnClickedButtonSend();
afx_msg LRESULT OnMyWParamMessage(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnStartProgress(WPARAM wParam, LPARAM lParam);
afx_msg void OnTimer(UINT_PTR nIDEvent);
};
#endif
3、MyWParamDlg.cpp中代码
cpp
#include "stdafx.h"
#include "resource.h"
#include "MyWParamDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// 定义自定义消息
#define WM_MY_WPARAM_MSG (WM_USER + 100)
#define WM_START_PROGRESS (WM_USER + 101) // 新增:启动进度更新的消息
BEGIN_MESSAGE_MAP(CMyWParamDlg, CDialog)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnBnClickedButtonSend)
ON_MESSAGE(WM_MY_WPARAM_MSG, OnMyWParamMessage)
ON_MESSAGE(WM_START_PROGRESS, OnStartProgress) // 新增:处理启动进度消息
ON_WM_TIMER() // 新增:处理WM_TIMER消息
END_MESSAGE_MAP()
CMyWParamDlg::CMyWParamDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyWParamDlg::IDD, pParent)
{
m_nCurrentProgress = 0; // 新增:当前进度值成员变量
}
void CMyWParamDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
DDX_Control(pDX, IDC_BUTTON_SEND, m_btnSend);
DDX_Control(pDX, IDC_STATIC_INFO, m_staticInfo);
}
BOOL CMyWParamDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置窗口标题
SetWindowText(_T("wParam消息传递示例"));
// 初始化进度条
m_Progress.SetRange(0, 100);
m_Progress.SetPos(0);
m_nCurrentProgress = 0; // 初始化当前进度
return TRUE;
}
void CMyWParamDlg::OnBnClickedButtonSend()
{
// 禁用按钮,防止重复点击
m_btnSend.EnableWindow(FALSE);
// 重置进度
m_nCurrentProgress = 0;
m_Progress.SetPos(0);
// 投递启动进度更新的消息,而不是一次性投递所有消息
PostMessage(WM_START_PROGRESS, 0, 0);
}
// 新增:处理启动进度更新的消息
LRESULT CMyWParamDlg::OnStartProgress(WPARAM wParam, LPARAM lParam)
{
if (m_nCurrentProgress < 100)
{
// 增加进度
m_nCurrentProgress++;
// 投递更新进度的消息,wParam传递当前进度值
PostMessage(WM_MY_WPARAM_MSG, (WPARAM)m_nCurrentProgress, 0);
// 100毫秒后再次投递启动进度消息,实现逐步增加
SetTimer(1, 100, NULL); // 使用定时器实现延迟
}
else
{
// 进度完成,重新启用按钮
m_btnSend.EnableWindow(TRUE);
MessageBox(_T("进度完成!\n10秒内逐步达到100%。"), _T("完成"), MB_OK | MB_ICONINFORMATION);
}
return 0;
}
// 新增:定时器处理函数
void CMyWParamDlg::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1)
{
KillTimer(1); // 停止定时器
PostMessage(WM_START_PROGRESS, 0, 0); // 继续进度更新
}
CDialog::OnTimer(nIDEvent);
}
LRESULT CMyWParamDlg::OnMyWParamMessage(WPARAM wParam, LPARAM lParam)
{
// 从wParam获取进度值
int nProgress = (int)wParam;
// 更新进度条
m_Progress.SetPos(nProgress);
// 显示状态信息
CString strInfo;
strInfo.Format(_T("当前进度: %d%%\n已用时间: %.1f秒"),
nProgress, nProgress * 0.1f); // 每1%对应0.1秒
m_staticInfo.SetWindowText(strInfo);
return 0;
}
运行程序
