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

📄 sportsportdlg.cpp

📁 ppWizard has created this SportSport application for you. This application not only demonstrates t
💻 CPP
字号:
// SportSportDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SportSport.h"
#include "SportSportDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

struct EditAndNO
{
	CEdit *pEdit;		//编辑框对象指针
	int iNO;			//编辑框值
};

HANDLE *g_hThread=NULL;				//保存所有线程
HANDLE g_hEvent;					//事件对象
int *g_iRand=NULL;					//保存随机数
EditAndNO *g_pEAN=NULL;				//保存所有EditAndNO结构对象
int *g_piNumNum=NULL;				//编辑框个数

DWORD CALLBACK GenerateRand(LPVOID pParam);
DWORD CALLBACK NumberChange(LPVOID pParam);

/////////////////////////////////////////////////////
//生成随机数的线程
DWORD CALLBACK GenerateRand(LPVOID pParam)
{
	int l_iNum=*(int *)pParam,i;
	while(WaitForSingleObject(g_hEvent,5)==WAIT_TIMEOUT)
	{
		//生成随机数
		srand(rand());
		for(i=0;i<l_iNum;i++)
			*(g_iRand+i)=rand()%10;
	}
	return 1;
}
//更新编辑框数字的线程
DWORD CALLBACK NumberChange(LPVOID pParam)
{
	EditAndNO *l_pEAN=(EditAndNO *)pParam;
	CEdit *l_pEdit=l_pEAN->pEdit;
	int l_iNO=l_pEAN->iNO;
	char l_cNum[2];

	memset(l_cNum,0,sizeof(l_cNum));
	l_cNum[0]='0';
	while(WaitForSingleObject(g_hEvent,10)==WAIT_TIMEOUT)
	{
		itoa(g_iRand[l_iNO]%10,l_cNum,10);

		//改变文本框数字
		l_pEdit->SetWindowText((LPCTSTR)(l_cNum));
		l_pEdit->UpdateData(false);
	}
	return 1;
}
/////////////////////////////////////////////////////////////////////////////
// CSportSportDlg dialog

CSportSportDlg::CSportSportDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSportSportDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSportSportDlg)
	m_1 = 0;
	m_2 = 0;
	m_3 = 0;
	m_4 = 0;
	m_5 = 0;
	m_6 = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSportSportDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSportSportDlg)
	DDX_Text(pDX, IDC_EDIT1, m_1);
	DDX_Text(pDX, IDC_EDIT2, m_2);
	DDX_Text(pDX, IDC_EDIT3, m_3);
	DDX_Text(pDX, IDC_EDIT4, m_4);
	DDX_Text(pDX, IDC_EDIT5, m_5);
	DDX_Text(pDX, IDC_EDIT6, m_6);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSportSportDlg, CDialog)
	//{{AFX_MSG_MAP(CSportSportDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTONSTOP, OnButtonstop)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSportSportDlg message handlers

BOOL CSportSportDlg::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

	//设置“停止”按钮无效
	GetDlgItem(IDC_BUTTONSTOP)->EnableWindow(false);

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

//开始选号
void CSportSportDlg::OnOK() 
{
	CEdit *l_pEdit[6];
	unsigned long id;

	//创建事件对象
	g_hThread=new HANDLE[7];
	g_hEvent=CreateEvent(NULL,true,false,NULL);

	g_pEAN=new EditAndNO[6];
	g_iRand=new int[6];
	
	//开始生成随机数的线程
	g_piNumNum=new int;
	*g_piNumNum=6;
	g_hThread[6]=CreateThread(NULL,0,GenerateRand,(LPVOID)g_piNumNum,0,&id);

	//分别开始更新每个编辑框的线程
	l_pEdit[0]=(CEdit *)GetDlgItem(IDC_EDIT1);
	g_pEAN[0].pEdit=l_pEdit[0];
	g_pEAN[0].iNO=0;
	g_hThread[0]=CreateThread(NULL,0,NumberChange,(LPVOID)g_pEAN,0,&id);

	l_pEdit[1]=(CEdit *)GetDlgItem(IDC_EDIT2);
	g_pEAN[1].pEdit=l_pEdit[1];
	g_pEAN[1].iNO=1;
	g_hThread[1]=CreateThread(NULL,0,NumberChange,(LPVOID)(g_pEAN+1),0,&id);

	l_pEdit[2]=(CEdit *)GetDlgItem(IDC_EDIT3);
	g_pEAN[2].pEdit=l_pEdit[2];
	g_pEAN[2].iNO=2;
	g_hThread[2]=CreateThread(NULL,0,NumberChange,(LPVOID)(g_pEAN+2),0,&id);

	l_pEdit[3]=(CEdit *)GetDlgItem(IDC_EDIT4);
	g_pEAN[3].pEdit=l_pEdit[3];
	g_pEAN[3].iNO=3;
	g_hThread[3]=CreateThread(NULL,0,NumberChange,(LPVOID)(g_pEAN+3),0,&id);

	l_pEdit[4]=(CEdit *)GetDlgItem(IDC_EDIT5);
	g_pEAN[4].pEdit=l_pEdit[4];
	g_pEAN[4].iNO=4;
	g_hThread[4]=CreateThread(NULL,0,NumberChange,(LPVOID)(g_pEAN+4),0,&id);

	l_pEdit[5]=(CEdit *)GetDlgItem(IDC_EDIT6);
	g_pEAN[5].pEdit=l_pEdit[5];
	g_pEAN[5].iNO=5;
	g_hThread[5]=CreateThread(NULL,0,NumberChange,(LPVOID)(g_pEAN+5),0,&id);

	//更新按钮状态
	GetDlgItem(IDC_BUTTONSTOP)->EnableWindow(true);
	GetDlgItem(IDOK)->EnableWindow(false);
	GetDlgItem(IDCANCEL)->EnableWindow(false);
}
//停止选号
void CSportSportDlg::OnButtonstop() 
{
	//关闭所有线程
	SetEvent(g_hEvent);
	CloseHandle(g_hThread[0]);
	CloseHandle(g_hThread[1]);
	CloseHandle(g_hThread[2]);
	CloseHandle(g_hThread[3]);
	CloseHandle(g_hThread[4]);
	CloseHandle(g_hThread[5]);
	CloseHandle(g_hEvent);
	delete [] g_hThread;
	delete [] g_iRand;
	delete [] g_pEAN;
	delete [] g_piNumNum;

	//更新按钮状态
	GetDlgItem(IDC_BUTTONSTOP)->EnableWindow(false);
	GetDlgItem(IDOK)->EnableWindow(true);
	GetDlgItem(IDCANCEL)->EnableWindow(true);
}

⌨️ 快捷键说明

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