请问如何在mfc界面上添加一个倒计时器?

请问如何在mfc界面上添加一个倒计时器?,第1张

http://icese.net/icetool/other/iceCal24.rar

vc6代码

// iceCal24Dlg.h : header file

//

class CIceCal24Dlg : public CDialog

{

public:

int timeValue

int timeID

// Construction

public:

CIceCal24Dlg(CWnd* pParent = NULL)// standard constructor

// Dialog Data

//{{AFX_DATA(CIceCal24Dlg)

enum { IDD = IDD_ICECAL24_DIALOG }

CString m_times

CString m_jishi

//}}AFX_DATA

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CIceCal24Dlg)

protected:

virtual void DoDataExchange(CDataExchange* pDX)// DDX/DDV support

//}}AFX_VIRTUAL

// Implementation

protected:

HICON m_hIcon

// Generated message map functions

//{{AFX_MSG(CIceCal24Dlg)

virtual BOOL OnInitDialog()

afx_msg void OnPaint()

afx_msg HCURSOR OnQueryDragIcon()

virtual void OnOK()

virtual void OnCancel()

afx_msg void OnButton1()

afx_msg void OnTimer(UINT nIDEvent)

afx_msg void OnButton2()

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

}

// iceCal24Dlg.cpp : implementation file

//

#include "stdafx.h"

#include "iceCal24.h"

#include "iceCal24Dlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__

#endif

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

// CIceCal24Dlg dialog

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

: CDialog(CIceCal24Dlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CIceCal24Dlg)

m_times = _T("60")

m_jishi = _T("0")

//}}AFX_DATA_INIT

// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME)

}

void CIceCal24Dlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX)

//{{AFX_DATA_MAP(CIceCal24Dlg)

DDX_Text(pDX, IDC_EDIT1, m_times)

DDV_MaxChars(pDX, m_times, 4)

DDX_Text(pDX, IDC_EDIT2, m_jishi)

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CIceCal24Dlg, CDialog)

//{{AFX_MSG_MAP(CIceCal24Dlg)

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_BN_CLICKED(IDC_BUTTON1, OnButton1)

ON_WM_TIMER()

ON_BN_CLICKED(IDC_BUTTON2, OnButton2)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

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

// CIceCal24Dlg message handlers

BOOL CIceCal24Dlg::OnInitDialog()

{

CDialog::OnInitDialog()

// Set the icon for this dialog. The framework does this automatically

// when the application's main window is not a dialog

SetIcon(m_hIcon, TRUE) // Set big icon

SetIcon(m_hIcon, FALSE) // Set small icon

// TODO: Add extra initialization here

timeID=0

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

}

// If you add a minimize button to your dialog, you will need the code below

// to draw the icon. For MFC applications using the document/view model,

// this is automatically done for you by the framework.

void CIceCal24Dlg::OnPaint()

{

if (IsIconic())

{

CPaintDC dc(this)// device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0)

// Center icon in client rectangle

int cxIcon = GetSystemMetrics(SM_CXICON)

int cyIcon = GetSystemMetrics(SM_CYICON)

CRect rect

GetClientRect(&rect)

int x = (rect.Width() - cxIcon + 1) / 2

int y = (rect.Height() - cyIcon + 1) / 2

// Draw the icon

dc.DrawIcon(x, y, m_hIcon)

}

else

{

CDialog::OnPaint()

}

}

// The system calls this to obtain the cursor to display while the user drags

// the minimized window.

HCURSOR CIceCal24Dlg::OnQueryDragIcon()

{

return (HCURSOR) m_hIcon

}

void CIceCal24Dlg::OnOK()

{

// TODO: Add extra validation here

}

void CIceCal24Dlg::OnCancel()

{

// TODO: Add extra cleanup here

}

void CIceCal24Dlg::OnButton1()

{

// TODO: Add your control notification handler code here

if(timeID >0)

KillTimer(timeID)

CDialog::OnOK()

}

void CIceCal24Dlg::OnTimer(UINT nIDEvent)

{

// TODO: Add your message handler code here and/or call default

if(nIDEvent == 6)

{

UpdateData(TRUE)

timeValue = atoi(m_jishi)

timeValue--

if(timeValue <= 0)

{

KillTimer(timeID)

timeID=0

}

m_jishi.Format("%d",timeValue)

UpdateData(FALSE)

}

CDialog::OnTimer(nIDEvent)

}

void CIceCal24Dlg::OnButton2()

{

// TODO: Add your control notification handler code here

UpdateData(TRUE)

timeID = SetTimer(6, 1000, NULL)

m_jishi=m_times

UpdateData(FALSE)

}

如果你是用VC6.0的话,多线程也不好解决,得把工具栏窗口放到另一个线程里。这个问题产生的原因在于VC6.0产生的MFC程序只有一个线程,因此所有的窗口的消息处理都在一个线程里,但是倘若要同时让两个窗口都各自响应自己的消息,那么它们各自的处理程序就不能在同一线程里。当鼠标拖着不放的时候,只让工具栏的窗口工作,主窗口被阻塞就算有消息也不能处理。在VS2010中,MFC提供了5个线程,没有出现这种情况。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11945451.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存