?? sportsportdlg.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 + -