⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp2.cpp

📁 基本的MFC算法
💻 CPP
字号:
// test8Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "test8.h"
#include "test8Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "afxmt.h"
CCriticalSection criticalSection;
int pos=0;
int pos2=0;
int timer;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest8Dlg dialog

CTest8Dlg::CTest8Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTest8Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTest8Dlg)
	m_nEdit = 0;
	m_nEdit2 = 0;
	m_nTimer1 = -1;
	m_nTimer = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTest8Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTest8Dlg)
	DDX_Control(pDX, IDC_PROGRESS2, m_Progress2);
	DDX_Control(pDX, IDC_PROGRESS, m_Progress);
	DDX_Text(pDX, IDC_EDIT, m_nEdit);
	DDX_Text(pDX, IDC_EDIT2, m_nEdit2);
	DDX_CBIndex(pDX, IDC_TIMER1, m_nTimer1);
	DDX_Text(pDX, IDC_TIMER, m_nTimer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTest8Dlg, CDialog)
	//{{AFX_MSG_MAP(CTest8Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON, OnButton)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_CBN_SELCHANGE(IDC_TIMER1, OnSelchangeTimer1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest8Dlg message handlers

BOOL CTest8Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	m_Progress.SetRange(0,100);
	m_Progress2.SetRange(0,10);
	((CComboBox*)GetDlgItem(IDC_TIMER1))->SetCurSel(0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTest8Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CTest8Dlg::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 CTest8Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

UINT CriticalThread1(LPVOID pParam)
{
	/*CWnd *pMainWnd=AfxGetMainWnd();
	if(pParam==NULL)
	{
		::MessageBox(pMainWnd->m_hWnd,"Param error","Thread error",MB_OK);
		AfxEndThread(2);
	}
	CProgressCtrl *pPro=(CProgressCtrl *)pParam;
	criticalSection.Lock();
	while(pos<=100)
	{
		pPro->SetPos(pos);
		pos++;
		Sleep(50);
	}
	criticalSection.Unlock();*/
	return 0;
}

UINT CriticalThread2(LPVOID pParam)
{
	CWnd *pMainWnd=AfxGetMainWnd();
	if(pParam==NULL)
	{
		::MessageBox(pMainWnd->m_hWnd,"Param error","Thread error",MB_OK);
		AfxEndThread(2);
	}
	pMainWnd->SetTimer(1,timer,NULL);
	
	return 0;
}
void CTest8Dlg::OnButton() 
{
	// TODO: Add your control notification handler code here
	pos=0;
	HWND hWnd=GetSafeHwnd();
	CProgressCtrl *pPro;
	pPro=&m_Progress;
	int *edit;
	edit=&m_nEdit;
	timer=(m_nTimer1+1)*100;
	//AfxBeginThread(CriticalThread1,(LPVOID)pPro);
		AfxBeginThread(CriticalThread2,(LPVOID)edit);
		
	
	
}

void CTest8Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==1)
	{
		UpdateData(TRUE);
		m_Progress.SetPos(pos);
		m_nEdit=pos;
		pos++;
		if(pos==101)
		{KillTimer(1);
			MessageBox("Mission1 Complete","Progress1",MB_OK);
		}
		UpdateData(FALSE);
	}
	if(nIDEvent==2)
	{
		UpdateData(TRUE);
		m_Progress2.SetPos(pos2);
		m_nEdit2=pos2;
		pos2++;
		if(pos2==11)
		{
			KillTimer(2);
			MessageBox("Mission2 Complete","Progress2",MB_OK);
		}
			
		UpdateData(FALSE);
	}
	
	
	CDialog::OnTimer(nIDEvent);
}

void CTest8Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	pos2=0;
	int timer;
	timer=(m_nTimer1+1)*100;
	SetTimer(2,timer,NULL);
}

void CTest8Dlg::OnSelchangeTimer1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_nTimer=(m_nTimer1+1)*100;
	UpdateData(FALSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -