📄 circuedlg.cpp
字号:
// circueDlg.cpp : implementation file
//
#include "stdafx.h"
#include "circue.h"
#include "circueDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CCircueDlg dialog
CCircueDlg::CCircueDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCircueDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCircueDlg)
m_avNum = 0;
m_circum = 0.0;
m_doNum = 0;
m_err = 0.0;
m_number = 2000;
//m_stat = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCircueDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCircueDlg)
DDX_Control(pDX, IDC_PRO, m_pro);
DDX_Control(pDX, IDC_PICTURE, m_picture);
//DDX_Text(pDX, IDC_STAT, m_stat);
DDX_Text(pDX, IDC_AVNUM, m_avNum);
DDX_Text(pDX, IDC_CirCUM, m_circum);
DDX_Text(pDX, IDC_DONUM, m_doNum);
DDX_Text(pDX, IDC_ERR, m_err);
DDX_Text(pDX, IDC_NUMBER, m_number);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCircueDlg, CDialog)
//{{AFX_MSG_MAP(CCircueDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_AgAIN, OnAgAIN)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCircueDlg message handlers
BOOL CCircueDlg::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
//get the max_rand/2
max = RAND_MAX/2.0;
//m_number = 200000;
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CCircueDlg::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 CCircueDlg::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 CCircueDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCircueDlg::Count()
{
double a; //临时变量,用来计算随机数的半径
this->DrawPicture ( TRUE, TRUE ); //显示图象
this->UpdateData ( TRUE ); //得到数据,用当前框中的数据,来计算概率
m_pro.SetRange32 (0,m_number); //设置进度条的范围
m_avNum = 0; //将命中的次数设置为0
for ( int i=1; i<=m_number; i++ ) //开始循环
{
randy = ( max - rand() )/max; //得到随机数,具体的方法,是利用计算机中存在的rand()
randx = ( max - rand() )/max; //得到随机数
a = randy*randy+randx*randx; //得到随机数的半径
if ( a<=1 ) //判断
{
m_avNum = m_avNum++; //符合条件 命中点+1
this->DrawPicture ( TRUE, FALSE ); //显示图象
}
else
this->DrawPicture ( FALSE, FALSE ); //不符合条件,显示图象
m_pro.SetPos ( i ); //设置进度条当前位置
}
m_doNum = m_number; //运行次数为设置的次数
m_circum = (double)(m_avNum); //改变整数为小数型
m_circum = 4*m_circum / m_doNum; //计算圆周率
m_err = 3.1415926 - m_circum; //圆周率与现在的圆周率的误差
this->UpdateData ( FALSE ); //显示运算的结果
}
void CCircueDlg::DrawPicture( BOOL av, BOOL first )
{
if ( first ) //如果是第一次,做图象数据处理的初始工作
{
hdc = ::GetDC ( m_picture.m_hWnd ); //得到句柄
::GetClientRect ( m_picture.m_hWnd, &rect ); //得到图象区的大小
rect.bottom = rect.bottom -20;
rect.top = rect.top +20;
rect.left = rect.left + 20;
rect.right = rect.right - 20; //以上为不让图象区走初边界
c_x = ( rect.left + rect.right )/2;
c_y = ( rect.bottom + rect.top )/2; //以上为设置图象区的中点
if ( (rect.bottom-rect.top)<(rect.right-rect.left) )
{
dir = rect.bottom - rect.top;
rect.left = c_x - dir/2;
rect.right = c_x + dir/2;
}
else
{
dir = rect.right - rect.left;
rect.bottom = c_y + dir/2;
rect.top = c_y - dir/2;
}//设置显示四方区的边界
::Rectangle ( hdc, rect.left, rect.top, rect.right, rect.bottom );//画出图象显示区
return;//返回
}
if ( av )
::SetPixel ( hdc, c_x + randx*dir/2, c_y + randy*dir/2, RGB ( 0, 0, 255 ) );//如果在圆半径内,显示为兰色点
else
::SetPixel ( hdc, c_x + randx*dir/2, c_y + randy*dir/2, RGB ( 255, 0, 0 ) );//如果不在圆的半径内,显示为红色点
}
void CCircueDlg::OnAgAIN()
{
// TODO: Add your control notification handler code here
this->Count();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -